diff --git a/commands_test.go b/commands_test.go index 7ff5efb4..8efe05b3 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 293c731e..eca4e0a0 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 }