Fixed delete command for arrays

This commit is contained in:
Mike Farah 2018-06-15 09:03:42 +10:00
parent 297522cbdd
commit 1b22e1d812
2 changed files with 12 additions and 10 deletions

12
yq.go
View File

@ -362,7 +362,7 @@ func deleteProperty(cmd *cobra.Command, args []string) error {
}
func deleteYaml(args []string) (interface{}, error) {
var parsedData yaml.MapSlice
var parsedData interface{}
var deletePath string
if len(args) < 2 {
@ -372,17 +372,11 @@ func deleteYaml(args []string) (interface{}, error) {
deletePath = args[1]
if err := readData(args[0], 0, &parsedData); err != nil {
var generalData interface{}
if err = readData(args[0], 0, &generalData); err != nil {
return nil, err
}
item := yaml.MapItem{Key: "thing", Value: generalData}
parsedData = yaml.MapSlice{item}
deletePath = "thing." + deletePath
}
path := parsePath(deletePath)
return deleteMap(parsedData, path), nil
paths := parsePath(deletePath)
return deleteChildValue(parsedData, paths), nil
}
func mergeProperties(cmd *cobra.Command, args []string) error {

View File

@ -106,3 +106,11 @@ func TestDeleteYaml(t *testing.T) {
"[{a Easy! as one two three} {b [{d [3 4]} {e [[{name fred} {value 3}] [{name sam} {value 4}]]}]}]",
formattedResult)
}
func TestDeleteYamlArray(t *testing.T) {
result, _ := deleteYaml([]string{"examples/sample_array.yaml", "[1]"})
formattedResult := fmt.Sprintf("%v", result)
assertResult(t,
"[1 3]",
formattedResult)
}