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

handle non-merge aliasing #416

Merged
merged 4 commits into from
Jun 30, 2024
Merged

handle non-merge aliasing #416

merged 4 commits into from
Jun 30, 2024

Conversation

pmeier
Copy link
Contributor

@pmeier pmeier commented Jun 30, 2024

@TomWright my patch in #415 is incorrect and is missing a piece:

  1. Regular alias dereferencing, e.g. foo: *bar was not handled, only merging an alias into a map.

  2. Merging an alias into a map is incorrect in add support for resolving YAML aliases #415 as it should only merge the children of the aliased map rather then the aliased map itself. For example,

    foo: &foo
      bar: 1
      baz: "baz"
    
    spam:
      <<: *foo

    currently decodes into

    foo: &foo
      bar: 1
      baz: "baz"
    
    spam:
      foo: foo
        bar: 1
        baz: "baz"

    but should decode into

    foo: &foo
      bar: 1
      baz: "baz"
    
    spam:
      bar: 1
      baz: "baz"

break
}

keyNode, valueNode, content = content[0], content[1], content[2:]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we need to merge the content of the alias into the current content of the map, I found it easiest to turn this into a queue rather than trying to manipulate the index.

@@ -98,12 +98,16 @@ key2: value6
})

t.Run("YamlAliases", func(t *testing.T) {
b := []byte(`foo: &foo
b := []byte(`foo: &foofoo
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The anchor and the key do not need to have the same value. This makes sure we don't actually use the anchor value for anything.

bar: 1
baz: "baz"
baz: &baz "baz"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using a top-level anchor, I used one inside a map to make sure this case is also covered.

Comment on lines +106 to +108
bar: 0
<<: *foofoo
baz: "bazbaz"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes sure that the *foofoo dereferencing

  1. overrides the bar: 0 value
  2. does not override the baz: "bazbaz" value

<<: *foofoo
baz: "bazbaz"

baz: *baz
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This adds a test for regular dereferencing.

Comment on lines +129 to +136
Set("foo", dencoding.NewMap().
Set("bar", int64(1)).
Set("baz", "baz")).
Set("spam", dencoding.NewMap().
Set("ham", "eggs").
Set("bar", int64(1)).
Set("baz", "bazbaz")).
Set("baz", "baz")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the new override tests, I found it easiest to not use the fooMap and rather be explicit about the result.

@TomWright
Copy link
Owner

Thanks for picking this up @pmeier, I'll release it under v2.8.1 shortly

@TomWright TomWright merged commit e96e173 into TomWright:master Jun 30, 2024
13 checks passed
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

Successfully merging this pull request may close these issues.

None yet

2 participants