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

Different behavior between . = . + and shorthand . += append assign operator #1174

Closed
drdaeman opened this issue Apr 11, 2022 · 2 comments
Closed
Labels

Comments

@drdaeman
Copy link

Describe the bug
I've discovered that contrary to the documentation it seems that yq has somewhat different behavior between . += something and . = . + something. The shorthand operator uses last RHS value everywhere, even if this value should be different across the modified nodes.

Version of yq: 4.23.1
Operating system: Debian GNU/Linux 11 (bullseye)
Installed via: binary release, downloaded from GitHub

Input Yaml
Concise yaml document(s) (as simple as possible to show the bug, please keep it to 10 lines or less)
data.yml:

items:
  foo:
    is: foo
  bar:
    is: bar
  baz:
    is: baz

Command
The command you ran:

yq eval 'with(.items[]; . += {"key": . | key})' data.yml

Note that with(.items[]; . = . + {"key": . | key}) works, producing the expected result.

Also, I totally realize that this is a weird expression that should've been just .key = key - this is unfortunate result of distilling the issue straight to the point. In real-world situation I have a complex data structure where I'm appending an item to an array nested inside a map with part of this item dependent on the top-level map key.

Actual behavior

items:
  foo:
    is: foo
    key: baz
  bar:
    is: bar
    key: baz
  baz:
    is: baz
    key: baz

Expected behavior

items:
  foo:
    is: foo
    key: foo
  bar:
    is: bar
    key: bar
  baz:
    is: baz
    key: baz

And last but not least - thank you for a very useful tool! I've recently discovered yq when I was looking for something to preprocess huge CI configuration document and yq is truly a lifesaver.

@mikefarah
Copy link
Owner

Yep - that's definitely a bug, thanks for the clear example.

mikefarah added a commit that referenced this issue Apr 14, 2022
@mikefarah
Copy link
Owner

Fixed in 4.24.5

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