diff --git a/cmd/commands_test.go b/cmd/commands_test.go index ba0cd91..9aeb7c1 100644 --- a/cmd/commands_test.go +++ b/cmd/commands_test.go @@ -307,6 +307,21 @@ foobar: test.AssertResult(t, expectedOutput, result.Output) } +func TestReadMergeAnchorsExplodeSimpleValueCmd(t *testing.T) { + content := `value: &value-pointer the value +pointer: *value-pointer` + filename := test.WriteTempYamlFile(content) + defer test.RemoveTempYamlFile(filename) + + cmd := getRootCommand() + result := test.RunCmd(cmd, fmt.Sprintf("read -X %s pointer", filename)) + if result.Error != nil { + t.Error(result.Error) + } + expectedOutput := `the value` + test.AssertResult(t, expectedOutput, result.Output) +} + func TestReadMergeAnchorsExplodeCmd(t *testing.T) { cmd := getRootCommand() result := test.RunCmd(cmd, "read -X ../examples/merge-anchor.yaml") @@ -337,6 +352,21 @@ foobar: test.AssertResult(t, expectedOutput, result.Output) } +func TestReadMergeAnchorsExplodeDeepCmd(t *testing.T) { + cmd := getRootCommand() + result := test.RunCmd(cmd, "read -X ../examples/merge-anchor.yaml foobar") + if result.Error != nil { + t.Error(result.Error) + } + expectedOutput := `thirty: well beyond +thing: ice +c: 3 +a: original +thirsty: yep +` + test.AssertResult(t, expectedOutput, result.Output) +} + func TestReadMergeAnchorsOverrideCmd(t *testing.T) { cmd := getRootCommand() result := test.RunCmd(cmd, "read ../examples/merge-anchor.yaml foobar.thing") diff --git a/pkg/yqlib/data_navigator.go b/pkg/yqlib/data_navigator.go index 92da9a5..d0c5f3f 100644 --- a/pkg/yqlib/data_navigator.go +++ b/pkg/yqlib/data_navigator.go @@ -63,7 +63,7 @@ func (n *navigator) getOrReplace(original *yaml.Node, expectedKind yaml.Kind) *y } func (n *navigator) recurse(value *yaml.Node, head string, tail []string, pathStack []interface{}) error { - log.Debug("recursing, processing %v", head) + log.Debug("recursing, processing %v, pathStack %v", head, pathStackToString(pathStack)) switch value.Kind { case yaml.MappingNode: log.Debug("its a map with %v entries", len(value.Content)/2) @@ -83,8 +83,14 @@ func (n *navigator) recurse(value *yaml.Node, head string, tail []string, pathSt log.Debug("its an alias!") DebugNode(value.Alias) if n.navigationStrategy.FollowAlias(NewNodeContext(value, head, tail, pathStack)) { - log.Debug("following the alias") - return n.recurse(value.Alias, head, tail, pathStack) + + if value.Alias.Kind == yaml.ScalarNode { + log.Debug("alias to a scalar") + return n.navigationStrategy.Visit(NewNodeContext(value.Alias, head, tail, pathStack)) + } else { + log.Debug("following the alias") + return n.recurse(value.Alias, head, tail, pathStack) + } } return nil default: