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

Cannot get the merge alias of a map #1942

Closed
mikefarah opened this issue Feb 9, 2024 Discussed in #1937 · 4 comments
Closed

Cannot get the merge alias of a map #1942

mikefarah opened this issue Feb 9, 2024 Discussed in #1937 · 4 comments

Comments

@mikefarah
Copy link
Owner

Discussed in #1937

Originally posted by qaisjp February 6, 2024
I have the following YAML:

foo: &foo
  hello: world
bar:
  <<: *foo

And would like to compute the following values based on the "parent" of each object

foo: &foo
  parent: null
bar:
  parent: foo   # bar inherits from "foo", so the value of bar.parent should be "foo"

I've tried the following filters, but can't seem to figure it out!

filter attempt 1

the following queries:

with_entries(.value |= {
  "parent": . | alias
})
with_entries(.value |= {
  "parent": alias
})

produces

foo: &foo
  parent: ""
bar:
  parent: ""  # expected "foo"

filter attempt 2

with_entries(.value |= {
  "parent": .["<<"]
})

produces

foo: &foo
  parent: null
bar:
  parent: null

filter attempt 3

with_entries(.value |= {
  "parent": (.["<<"] | alias)
})

produces

foo: &foo
  parent: "null"
bar:
  parent: "null"

Note that it works fine with anchors

with_entries(.value |= {
  "anchor_name": . | anchor
})

produces

foo: &foo
  anchor_name: foo
bar:
  anchor_name: ""

<<: * is parsed correctly

When I just pipe my yaml through with the . filter, I get

foo: &foo
  hello: world
bar:
  !!merge <<: *foo

Which is correct — according to the docs it says:

yq supports merge aliases (like <<: *blah) however this is no longer in the standard yaml spec (1.2) and so yq will automatically add the !!merge tag to these nodes as it is effectively a custom tag.

(also mentioned at #680 (comment))

but why can't I read the merge alias at .["<<"]?


Any pointers? 🙁 Thanks!

@mikefarah
Copy link
Owner Author

Will fix in the next release :)

@mikefarah
Copy link
Owner Author

Fwiw - when fixed you can get the alias name by:

yq '.bar["<<"] | alias' file.yaml

@mikefarah
Copy link
Owner Author

Fixed in 4.41.1

@qaisjp
Copy link

qaisjp commented Feb 20, 2024

Thanks!

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

No branches or pull requests

2 participants