From 16bde80334346a60856bca7cd4f82a567d27bb8f Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Tue, 20 Nov 2018 09:47:17 +1100 Subject: [PATCH] Prefix now supports arrays --- commands_test.go | 20 ++++++++++++++++++++ yq.go | 5 ++--- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/commands_test.go b/commands_test.go index 7ff5efb..8efe05b 100644 --- a/commands_test.go +++ b/commands_test.go @@ -360,6 +360,26 @@ func TestPrefixCmd(t *testing.T) { assertResult(t, expectedOutput, result.Output) } +func TestPrefixCmdArray(t *testing.T) { + content := `b: + c: 3 +` + filename := writeTempYamlFile(content) + defer removeTempYamlFile(filename) + + cmd := getRootCommand() + result := runCmd(cmd, fmt.Sprintf("prefix %s [0].d.[1]", filename)) + if result.Error != nil { + t.Error(result.Error) + } + expectedOutput := `- d: + - null + - b: + c: 3 +` + assertResult(t, expectedOutput, result.Output) +} + func TestPrefixCmd_MultiLayer(t *testing.T) { content := `b: c: 3 diff --git a/yq.go b/yq.go index 293c731..eca4e0a 100644 --- a/yq.go +++ b/yq.go @@ -440,9 +440,8 @@ func prefixProperty(cmd *cobra.Command, args []string) error { log.Debugf("Prefixing %v to doc %v", paths, currentIndex) var mapDataBucket = dataBucket for _, key := range paths { - nestedBucket := make(map[string]interface{}) - nestedBucket[key] = mapDataBucket - mapDataBucket = nestedBucket + singlePath := []string{key} + mapDataBucket = updatedChildValue(nil, singlePath, mapDataBucket) } return mapDataBucket, nil }