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

No way to add a quoted numeric key (which is not a number despite its appearance) #1247

Closed
notorand-it opened this issue Jun 21, 2022 · 10 comments
Labels

Comments

@notorand-it
Copy link

No way to add a quoted numeric key (which is not a number despite its appearance) as in:

  Something:
    "4242":
      Name: myID

yq -i '.Something.4242."Name" = "myID"' file.yaml
produces

Something:
  4242:
    Name: myID

yq -i '.Something."4242"."Name" = "myID"' file.yaml
produces

Something:
  4242:
    Name: myID

yq -i ".Something.'4242'."Name" = "myID"" file.yaml
produces

Something:
  '''4242''':
    Name: myID

Version of yq: 4.25.2
Operating system: ArchLinux x86_64
Installed via: repository (community/go-yq 4.25.2-1)

@mikefarah
Copy link
Owner

You need to modify the 'tag' of the key node to force it to be a string:

yq -n '.123 = "cat" | (.123 | key) tag="!!str"'
"123": cat

@mikefarah
Copy link
Owner

or for your example: yq -i '.Something.4242.Name = "MyId" | (.Something.4242 | key) tag="!!str"' file.yaml

@notorand-it
Copy link
Author

notorand-it commented Jun 23, 2022

There are a number of issues with the "!!str" in bash.

As the numeric key is generated via variable, the command string needs to be put in double quotes and the !! is interfering with the shell itself. There seems to be no way around this: your suggestions only work with single quoted commands.

I still think that more straightforward approaches make software more usable.

Results from my second and third example, IMHO, make no sense at all: I put some quoting in a key and it either gets ignored altogether or mangled (triple quotes as the result of single quoting!). But this is just MHO.

@notorand-it
Copy link
Author

Not even this works:

#!/usr/bin/env bash

VAR=42

yq -n '.Mappings.AccountSettings.'${VAR}'."Name" = "pippo" (.Mappings.AccountSettings.'${VAR}' | key) tag="!!str"'

Your suggestion only works with static strings in single quotes. Variables are not expanded within single-quoted strings but are withing double-quoted ones.

@mikefarah
Copy link
Owner

Yeah - there's a env and strenv operators to pull out variables for exactly this quoting issue (and others) https://mikefarah.gitbook.io/yq/operators/env-variable-operators#dynamic-key-lookup-with-environment-variable

I highly recommend using single quotes for the expression, and these operators - otherwise you quickly end up in different quoting hells depending on your shell.

That said, when I tried out strenv for your scenario it doesn't work - it thinks it's an array index :( I'll have a fix in the next release

@mikefarah
Copy link
Owner

Ok next release you will be able to:

yq -n '.Something["12"] = "cat"'
Something:
  "12": cat

as well as:

VAR=12 yq -n '.Something[strenv(VAR)] = "cat"'
Something:
  "12": cat

@mikefarah
Copy link
Owner

Just released the fix in v4.25.3 - let me know how it goes!

@notorand-it
Copy link
Author

notorand-it commented Jun 23, 2022

Cool! Great idea! Will wait for the build in ArchLinux and test it.

@notorand-it
Copy link
Author

Weird results in ArchLinux:
Version of yq: 4.25.3
Operating system: ArchLinux x86_64
Installed via: repository (community/go-yq 4.25.3-1)

$ yq -n '.Something.4242."Name" = "myID"'
Something:
  "4242":
    Name: myID

and

$ yq -n '.Something."4242"."Name" = "myID"'
Something:
  "4242":
    Name: myID

Second example makes sense, first one none at all as I get the quotes around a numeric key anyway.

I think this could break come backward compatibility.

@mikefarah
Copy link
Owner

Hmm that was unintended...but I don't think it's necessarily incorrect. Hopefully won't cause anyone issues with backwards compatibility :|

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

No branches or pull requests

2 participants