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

Casting yes/no to string does not produce a string value #341

Closed
dsharp-pivotal opened this issue Feb 4, 2020 · 7 comments
Closed

Casting yes/no to string does not produce a string value #341

dsharp-pivotal opened this issue Feb 4, 2020 · 7 comments
Milestone

Comments

@dsharp-pivotal
Copy link

"yes" and "no" are boolean values in yaml when unquoted. If these strings are left unquoted by yq, then it's not possible to create the values "yes" and "no" as strings.

Here's the (large!) list of values that YAML treats as booleans:
https://yaml.org/type/bool.html

y|Y|yes|Yes|YES|n|N|no|No|NO
|true|True|TRUE|false|False|FALSE
|on|On|ON|off|Off|OFF

Actual

Taken from the doc on casting booleans, but replace "true" with "yes":

$ yq new --tag '!!str' key yes
key: yes

Expected

yq new --tag '!!str' key yes
key: "yes"
@mikefarah
Copy link
Owner

mikefarah commented Feb 4, 2020

I was reading up on yaml - the 1.1 spec allowed for those values - however that had a whole bunch of issues in different applications so in the 1.2 spec the dropped back to just true/false (https://yaml.org/spec/1.2/spec.html#id2803629, #go-yaml/yaml#214)

@mikefarah
Copy link
Owner

For the record, v3.x of yq uses the v3 yaml parser, so this applies https://github.com/go-yaml/yaml/tree/v3#compatibility

@dsharp-pivotal
Copy link
Author

I see. Good to know. However, it seems that since there are still many apps that consume YAML 1.1 booleans (Kubernetes included, it seems, as that is the consumer in our case), the most compatible thing to do would be to output "yes", "no", etc with quotes when it is requested that they be strings (--tag "!!str"). That would work correctly for both 1.1 and 1.2 parsers.

@mikefarah
Copy link
Owner

yeah good idea - I'll put in a special condition for --tag '!!str' to force quotes around values.

@dsharp-pivotal
Copy link
Author

@mikefarah I just made a PR to go-yaml. If accepted, I think you may not need to add a condition.

go-yaml/yaml#574

@mikefarah
Copy link
Owner

I think this will have a workaround in the upcoming --style flag, whereby you can force a double or single quotes with the tag flag.

@mikefarah mikefarah added this to the 3.2.2 milestone Apr 17, 2020
@dsharp-pivotal
Copy link
Author

I tested it with yq 3.3.0, and that'll work for us. Thanks!

$ yq new --style double key yes
key: "yes"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants