diff --git a/cmd/commands_test.go b/cmd/commands_test.go index 271eb52c..7a0e7199 100644 --- a/cmd/commands_test.go +++ b/cmd/commands_test.go @@ -282,6 +282,47 @@ func TestReadMergeAnchorsOriginalCmd(t *testing.T) { test.AssertResult(t, "original", result.Output) } +func TestReadMergeAnchorsExplodeJsonCmd(t *testing.T) { + cmd := getRootCommand() + result := test.RunCmd(cmd, "read -j ../examples/merge-anchor.yaml") + if result.Error != nil { + t.Error(result.Error) + } + expectedOutput := `{"bar":{"b":2,"c":"oldbar","thing":"coconut"},"foo":{"a":"original","thing":"coolasdf","thirsty":"yep"},"foobar":{"a":"original","c":3,"thing":"ice","thirsty":"yep","thirty":"well beyond"},"foobarList":{"a":"original","b":2,"c":"newbar","thing":"coconut","thirsty":"yep"}} +` + test.AssertResult(t, expectedOutput, result.Output) +} + +func TestReadMergeAnchorsExplodeCmd(t *testing.T) { + cmd := getRootCommand() + result := test.RunCmd(cmd, "read -X ../examples/merge-anchor.yaml") + if result.Error != nil { + t.Error(result.Error) + } + expectedOutput := `foo: + a: original + thing: coolasdf + thirsty: yep +bar: + b: 2 + thing: coconut + c: oldbar +foobarList: + c: newbar + b: 2 + thing: coconut + a: original + thirsty: yep +foobar: + 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/cmd/utils.go b/cmd/utils.go index d6982527..791be2d3 100644 --- a/cmd/utils.go +++ b/cmd/utils.go @@ -103,6 +103,7 @@ func writeString(writer io.Writer, txt string) error { } func explode(matchingNodes []*yqlib.NodeContext) error { + for _, nodeContext := range matchingNodes { var targetNode = yaml.Node{Kind: yaml.MappingNode} explodedNodes, errorRetrieving := lib.Get(nodeContext.Node, "**") @@ -112,7 +113,10 @@ func explode(matchingNodes []*yqlib.NodeContext) error { for _, matchingNode := range explodedNodes { mergePath := lib.MergePathStackToString(matchingNode.PathStack, appendFlag) updateCommand := yqlib.UpdateCommand{Command: "update", Path: mergePath, Value: matchingNode.Node, Overwrite: overwriteFlag} - lib.Update(&targetNode, updateCommand, true) + errorUpdating := lib.Update(&targetNode, updateCommand, true) + if errorUpdating != nil { + return errorUpdating + } } nodeContext.Node = &targetNode } @@ -126,7 +130,10 @@ func printResults(matchingNodes []*yqlib.NodeContext, writer io.Writer) error { //always explode anchors when printing json if explodeAnchors || outputToJSON { - explode(matchingNodes) + errorExploding := explode(matchingNodes) + if errorExploding != nil { + return errorExploding + } } bufferedWriter := bufio.NewWriter(writer)