mikefarah / yq

yq is a portable command-line YAML, JSON, XML, CSV, TOML and properties processor
https://mikefarah.gitbook.io/yq/
MIT License
12.36k stars 602 forks source link

`with_entries` doesn't work with `with` #2066

Closed jancespivo closed 5 months ago

jancespivo commented 5 months ago

Describe the bug

yq 'with_entries(.key |= "KEY_" + .)' sample.yml according to the https://mikefarah.gitbook.io/yq/operators/entries#use-with_entries-to-update-keys works however nested one using with does not: yq 'with(.nested; with_entries(.key |= "KEY_" + .))' sample.yml

Version of yq: 4.44.1 Operating system: linux Installed via: pacman

Input Yaml

sample.yml:

nested:
  a: 1
  b: 2

Command The command you ran:

yq 'with(.nested; with_entries(.key |= "KEY_" + .))' sample.yml

Actual behavior

nested:
  a: 1
  b: 2

Expected behavior

nested:
  KEY_a: 1
  KEY_b: 2

Additional context Related to https://github.com/mikefarah/yq/issues/210

mikefarah commented 5 months ago

Huh now that I look at that example in #210; it's actually wrong. It's not assigning the with_entries result to anything. Not sure how it was working before - probably a bug 😓

It should be either:

yq '.nested |= with_entries(.key |= "KEY_" + .)' data1.yaml

or; if you need to use with:

yq 'with(.nested; . |= with_entries(.key |= "KEY_" + .))' data1.yaml

This also follows with what I documented for with: https://mikefarah.gitbook.io/yq/operators/with