Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds "tobool" builtin function for converting strings to booleans #2098

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

brahmlower
Copy link

This PR adds a builtin tobool function which converts string values to booleans, similar to tonumber.

This was motivated by the need to create a boolean value based on a value provided through an environment variable. All environment variables are strings, so I needed some mechanism to convert from a string to a boolean. A contrived example of what I wanted (and what this PR implements) is:

$ TEST=true jq --null-input '{"mytest": env.TEST | tobool}'
{
  "mytest": true
}

This boolean conversion is already achievable using a function defined in the filter:

$ TEST=true jq --null-input 'def tobool: {"true":true, "false":false}[ . | tostring ]; {"mytest": env.TEST | tobool}'

The inline function definition is rather unwieldily for my use case, making the availability of the builtin function more appealing.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.02%) to 84.149% when pulling 5074537 on bplower:tobool-builtin into ccc79e5 on stedolan:master.

brahmlower added a commit to brahmlower/jq-config-example-service that referenced this pull request Apr 14, 2020
Had to add a custom jq function to handle the conversion from string to booleans. Opened a PR to jq which would add the `tobool` as a builtin function: jqlang/jq#2098 Looks like releases aren't cut very often, so don't hold your breath.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants