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

multiply (merge) operator on map with multiple files results in empty map #886

Open
timoreimann opened this issue Jul 9, 2021 · 2 comments
Labels

Comments

@timoreimann
Copy link

Hello 👋

Describe the bug
I am trying to merge a YAML file containing a map (dictionary) into another YAML file using the *? operator (merge on existing keys). Instead of merging in the map items, however, an empty map is produced.

Version of yq: 4.9.8
Operating system: Mac OS X (Big Sur)
Installed via: brew

Input Yaml
1.yaml:

thing: one
cat: frog

2.yaml:

thing:
  field1: one
  field2: two

Command

yq eval-all 'select(fileIndex == 0) *? select(fileIndex == 1)' 1.yaml 2.yaml

Actual behavior

thing: {}
cat: frog

Expected behavior

thing:
  field1: one
  field2: two
cat: frog

Additional context
The * works fine per se, but of course ignores the existing key constraint which is what I need for my specific case. (Omitted from my examples above to keep the reproduction as simple as possible.)

@mikefarah
Copy link
Owner

Yeah I see, this is a bit tricky - merge runs recursively, and the ? flag means it shouldn't create new keys. As it recurses, it will first encounter thing it will switch the type from a scalar to a map, making it empty. Next it will encounter field1 and try to update that in thing - however, thing is empty and does not have that field, so it skips over it.

Have to think about this one a little more

@ybizeul
Copy link

ybizeul commented Apr 16, 2022

I think this exposes the same behavior :

Input File:

a:
 image: image1
b:
 image: image2
c:
 key: value
---

---

Command:

❯ cat test.yaml|yq e 'select(.a.image).a.image = "test/"+.a.image'
a:
  image: test/image1
b:
  image: image2
c:
  key: value
---
{}
---
{}

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

3 participants