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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 20 additions & 12 deletions dencoding/yaml_decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ func (decoder *YAMLDecoder) getNodeValue(node *yaml.Node) (any, error) {
return decoder.getSequenceNodeValue(node)
case yaml.ScalarNode:
return decoder.getScalarNodeValue(node)
case yaml.AliasNode:
return decoder.getNodeValue(node.Alias)
default:
return nil, fmt.Errorf("unhandled node kind: %v", node.Kind)
}
Expand All @@ -72,20 +74,26 @@ func (decoder *YAMLDecoder) getNodeValue(node *yaml.Node) (any, error) {
func (decoder *YAMLDecoder) getMappingNodeValue(node *yaml.Node) (any, error) {
res := NewMap()

for i := 0; i < len(node.Content); i += 2 {
keyNode := node.Content[i]
valueNode := node.Content[i+1]
content := make([]*yaml.Node, 0)
content = append(content, node.Content...)

var keyNode *yaml.Node
var valueNode *yaml.Node
for {
if len(content) == 0 {
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.


var keyValue any
if keyNode.ShortTag() == yamlTagMerge {
keyValue = valueNode.Value
valueNode = valueNode.Alias
} else {
var err error
keyValue, err = decoder.getNodeValue(keyNode)
if err != nil {
return nil, err
}
content = append(valueNode.Alias.Content, content...)
continue
}

keyValue, err := decoder.getNodeValue(keyNode)
if err != nil {
return nil, err
}

value, err := decoder.getNodeValue(valueNode)
Expand Down
27 changes: 15 additions & 12 deletions dencoding/yaml_decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.

spam:
ham: "eggs"
<<: *foo
bar: 0
<<: *foofoo
baz: "bazbaz"
Comment on lines +106 to +108
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


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.

`)

dec := dencoding.NewYAMLDecoder(bytes.NewReader(b))
Expand All @@ -121,16 +125,15 @@ spam:
got = append(got, v)
}

fooMap := dencoding.NewMap().
Set("bar", int64(1)).
Set("baz", "baz")
spamMap := dencoding.NewMap().
Set("ham", "eggs").
Set("foo", fooMap)

exp := dencoding.NewMap().
Set("foo", fooMap).
Set("spam", spamMap)
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")
Comment on lines +129 to +136
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.


if len(got) != 1 {
t.Errorf("expected result len of %d, got %d", 1, len(got))
Expand Down
18 changes: 11 additions & 7 deletions internal/command/select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,21 +254,25 @@ octal: 8`)),

t.Run("Issue285 - YAML alias on read", runTest(
[]string{"-r", "yaml", "-w", "yaml"},
[]byte(`foo: &foo
[]byte(`foo: &foofoo
bar: 1
baz: baz
baz: &baz "baz"
spam:
ham: eggs
<<: *foo
ham: "eggs"
bar: 0
<<: *foofoo
baz: "bazbaz"

baz: *baz
`),
[]byte(`foo:
bar: 1
baz: baz
spam:
ham: eggs
foo:
bar: 1
baz: baz
bar: 1
baz: bazbaz
baz: baz
`),
nil,
nil,
Expand Down
Loading