From 108b5cb093d0770fc5691426795918281e420485 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Fri, 7 Feb 2020 09:08:52 +1100 Subject: [PATCH] Fixed explode for simple anchors --- cmd/commands_test.go | 14 ++++++++++++++ examples/simple-anchor.yaml | 3 ++- pkg/yqlib/data_navigator.go | 5 ++--- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/cmd/commands_test.go b/cmd/commands_test.go index 7a0e7199..925f45ec 100644 --- a/cmd/commands_test.go +++ b/cmd/commands_test.go @@ -293,6 +293,20 @@ func TestReadMergeAnchorsExplodeJsonCmd(t *testing.T) { test.AssertResult(t, expectedOutput, result.Output) } +func TestReadMergeAnchorsExplodeSimpleCmd(t *testing.T) { + cmd := getRootCommand() + result := test.RunCmd(cmd, "read -X ../examples/simple-anchor.yaml") + if result.Error != nil { + t.Error(result.Error) + } + expectedOutput := `foo: + a: 1 +foobar: + a: 1 +` + test.AssertResult(t, expectedOutput, result.Output) +} + func TestReadMergeAnchorsExplodeCmd(t *testing.T) { cmd := getRootCommand() result := test.RunCmd(cmd, "read -X ../examples/merge-anchor.yaml") diff --git a/examples/simple-anchor.yaml b/examples/simple-anchor.yaml index c83b2dcf..35218bbd 100644 --- a/examples/simple-anchor.yaml +++ b/examples/simple-anchor.yaml @@ -1,4 +1,5 @@ foo: &foo a: 1 -foobar: *foo \ No newline at end of file +foobar: + <<: *foo \ No newline at end of file diff --git a/pkg/yqlib/data_navigator.go b/pkg/yqlib/data_navigator.go index 0a053b7c..92da9a59 100644 --- a/pkg/yqlib/data_navigator.go +++ b/pkg/yqlib/data_navigator.go @@ -98,7 +98,6 @@ func (n *navigator) recurseMap(value *yaml.Node, head string, tail []string, pat log.Debug("recurseMap: visitMatchingEntries for %v", contents[indexInMap].Value) n.navigationStrategy.DebugVisitedNodes() newPathStack := append(pathStack, contents[indexInMap].Value) - n.navigationStrategy.DebugVisitedNodes() log.Debug("should I traverse? head: %v, path: %v", head, pathStackToString(newPathStack)) DebugNode(value) if n.navigationStrategy.ShouldTraverse(NewNodeContext(contents[indexInMap+1], head, tail, newPathStack), contents[indexInMap].Value) { @@ -172,10 +171,10 @@ func (n *navigator) visitAliases(contents []*yaml.Node, head string, tail []stri // a node can either be // an alias to one other node (e.g. <<: *blah) // or a sequence of aliases (e.g. <<: [*blah, *foo]) - log.Debug("checking for aliases") + log.Debug("checking for aliases, head: %v, pathstack: %v", head, pathStackToString(pathStack)) for index := len(contents) - 2; index >= 0; index = index - 2 { - if contents[index+1].Kind == yaml.AliasNode { + if contents[index+1].Kind == yaml.AliasNode && contents[index].Value == "<<" { valueNode := contents[index+1] log.Debug("found an alias") DebugNode(contents[index])