Prefix now supports arrays

This commit is contained in:
Mike Farah 2018-11-20 09:47:17 +11:00
parent 8a3fb32f36
commit 16bde80334
2 changed files with 22 additions and 3 deletions

View File

@ -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

5
yq.go
View File

@ -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
}