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

Bug: del will not delete a map key named key when applied to the output of to_entries #1886

Closed
mikepilat opened this issue Nov 29, 2023 · 2 comments
Labels

Comments

@mikepilat
Copy link

mikepilat commented Nov 29, 2023

Describe the bug

... | map(del(.key)) works on collections of records in general, except when applied to the output of to_entries. You can delete other keys, including .value, but specifically not .key. Odder still, if the input file has the same structure as what to_entries creates, you CAN delete the .key record. Examples below.

This behavior changed somewhere between v4.35.2 and v4.40.3.

Version of yq: 4.40.3
Operating system: mac, linux
Installed via: homebrew, asdf, github runner

Input Yaml

input.yml:

test1:
  bar: bar1
  foo: foo1
test2:
  bar: bar2
  foo: foo2

input_to_entries.yml:

- key: test1
  value:
    bar: bar1
    foo: foo1
- key: test2
  value:
    bar: bar2
    foo: foo2

Command
The command you ran:

yq 'to_entries | map(del(.key))' input.yaml

Actual behavior

- key: test1
  value:
    bar: bar1
    foo: foo1
- key: test2
  value:
    bar: bar2
    foo: foo2

Expected behavior

- value:
    bar: bar1
    foo: foo1
- value:
    bar: bar2
    foo: foo2

Additional context

It appears to be something specific about applying to the output of to_entries. If you apply the same transformation two steps via an intermediate file, you can delete the key:

yq 'to_entries' input.yaml > input_to_entries.yaml
yq 'map(del(.key))' input_to_entries.yaml

Gives the expected:

- value:
    bar: bar1
    foo: foo1
- value:
    bar: bar2
    foo: foo2

This appears to be specific to keys named key, as you can delete the value key from to_entries output:

yq 'to_entries | map(del(.value))' input.yaml

... gives:

- key: test1
- key: test2

Was working as expected in v4.35.2:

$yq -V
yq (https://github.com/mikefarah/yq/) version v4.35.2

$yq 'to_entries | map(del(.key))' input.yaml
- value:
    bar: bar1
    foo: foo1
- value:
    bar: bar2
    foo: foo2
@mikefarah
Copy link
Owner

Nice catch! Thanks for raising, will fix in the next release

@mikefarah
Copy link
Owner

Fixed in 4.40.4

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