From 96a4161a92d1c4da9b3959b1d465adfcf2a49f5e Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Wed, 12 Feb 2020 11:03:40 +1100 Subject: [PATCH] Fixed explode anchors for array roots --- cmd/commands_test.go | 30 ++++++++++++++++++++++++++++++ cmd/utils.go | 5 +++-- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/cmd/commands_test.go b/cmd/commands_test.go index 77fe3a59..71df5f99 100644 --- a/cmd/commands_test.go +++ b/cmd/commands_test.go @@ -322,6 +322,36 @@ pointer: *value-pointer` test.AssertResult(t, expectedOutput, result.Output) } +func TestReadMergeAnchorsExplodeSimpleArrayCmd(t *testing.T) { + content := `- things` + filename := test.WriteTempYamlFile(content) + defer test.RemoveTempYamlFile(filename) + + cmd := getRootCommand() + result := test.RunCmd(cmd, fmt.Sprintf("read -X %s", filename)) + if result.Error != nil { + t.Error(result.Error) + } + expectedOutput := `- things +` + test.AssertResult(t, expectedOutput, result.Output) +} + +func TestReadMergeAnchorsExplodeSimpleArrayJsonCmd(t *testing.T) { + content := `- things` + filename := test.WriteTempYamlFile(content) + defer test.RemoveTempYamlFile(filename) + + cmd := getRootCommand() + result := test.RunCmd(cmd, fmt.Sprintf("read -j %s", filename)) + if result.Error != nil { + t.Error(result.Error) + } + expectedOutput := `["things"] +` + test.AssertResult(t, expectedOutput, result.Output) +} + func TestReadMergeAnchorsExplodeSimpleValueForValueCmd(t *testing.T) { content := `value: &value-pointer the value pointer: *value-pointer` diff --git a/cmd/utils.go b/cmd/utils.go index 2283e576..3f5c3959 100644 --- a/cmd/utils.go +++ b/cmd/utils.go @@ -115,9 +115,9 @@ func writeString(writer io.Writer, txt string) error { } func explode(matchingNodes []*yqlib.NodeContext) error { - + log.Debug("exploding nodes") for _, nodeContext := range matchingNodes { - var targetNode = yaml.Node{Kind: yaml.MappingNode} + var targetNode = yaml.Node{Kind: nodeContext.Node.Kind} explodedNodes, errorRetrieving := lib.Get(nodeContext.Node, "**", true) if errorRetrieving != nil { return errorRetrieving @@ -132,6 +132,7 @@ func explode(matchingNodes []*yqlib.NodeContext) error { } nodeContext.Node = &targetNode } + log.Debug("done exploding nodes") return nil }