Skip to content

Cannot get the merge alias of a map #1942

Closed
@mikefarah

Description

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!

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions