Can delete splat!

Fixes https://github.com/mikefarah/yq/issues/175
This commit is contained in:
Mike Farah 2019-05-13 09:48:05 +10:00
parent c4e9516aa6
commit 774badfef4
2 changed files with 77 additions and 14 deletions

View File

@ -868,6 +868,70 @@ b:
assertResult(t, expectedOutput, result.Output) assertResult(t, expectedOutput, result.Output)
} }
func TestDeleteSplatYaml(t *testing.T) {
content := `a: 2
b:
hi:
c: things
d: something else
there:
c: more things
d: more something else
`
filename := writeTempYamlFile(content)
defer removeTempYamlFile(filename)
cmd := getRootCommand()
result := runCmd(cmd, fmt.Sprintf("delete -v %s b.*.c", filename))
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `a: 2
b:
hi:
d: something else
there:
d: more something else
`
assertResult(t, expectedOutput, result.Output)
}
func TestDeleteSplatPrefixYaml(t *testing.T) {
content := `a: 2
b:
hi:
c: things
d: something else
there:
c: more things
d: more something else
there2:
c: more things also
d: more something else also
`
filename := writeTempYamlFile(content)
defer removeTempYamlFile(filename)
cmd := getRootCommand()
result := runCmd(cmd, fmt.Sprintf("delete -v %s b.there*.c", filename))
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `a: 2
b:
hi:
c: things
d: something else
there:
d: more something else
there2:
d: more something else also
`
assertResult(t, expectedOutput, result.Output)
}
func TestDeleteYamlArray(t *testing.T) { func TestDeleteYamlArray(t *testing.T) {
content := `- 1 content := `- 1
- 2 - 2

View File

@ -241,20 +241,20 @@ func deleteMap(context interface{}, paths []string) yaml.MapSlice {
return mapSlice return mapSlice
} }
var found bool
var index int var index int
var child yaml.MapItem var child yaml.MapItem
for index, child = range mapSlice { for index, child = range mapSlice {
if child.Key == paths[0] { if matchesKey(paths[0], child.Key) {
found = true log.Debugf("\tMatched [%v] with [%v] at index %v", paths[0], child.Key, index)
break mapSlice = deleteEntryInMap(mapSlice, child, index, paths)
} }
} }
if !found { return mapSlice
return mapSlice
}
}
func deleteEntryInMap(original yaml.MapSlice, child yaml.MapItem, index int, paths []string) yaml.MapSlice {
remainingPaths := paths[1:] remainingPaths := paths[1:]
var newSlice yaml.MapSlice var newSlice yaml.MapSlice
@ -262,9 +262,9 @@ func deleteMap(context interface{}, paths []string) yaml.MapSlice {
newChild := yaml.MapItem{Key: child.Key} newChild := yaml.MapItem{Key: child.Key}
newChild.Value = deleteChildValue(child.Value, remainingPaths) newChild.Value = deleteChildValue(child.Value, remainingPaths)
newSlice = make(yaml.MapSlice, len(mapSlice)) newSlice = make(yaml.MapSlice, len(original))
for i := range mapSlice { for i := range original {
item := mapSlice[i] item := original[i]
if i == index { if i == index {
item = newChild item = newChild
} }
@ -272,12 +272,11 @@ func deleteMap(context interface{}, paths []string) yaml.MapSlice {
} }
} else { } else {
// Delete item from slice at index // Delete item from slice at index
newSlice = append(mapSlice[:index], mapSlice[index+1:]...) newSlice = append(original[:index], original[index+1:]...)
log.Debugf("\tDeleted item index %d from mapSlice", index) log.Debugf("\tDeleted item index %d from original", index)
} }
log.Debugf("\t\tlen: %d\tcap: %d\tslice: %v", len(mapSlice), cap(mapSlice), mapSlice) log.Debugf("\tReturning original %v\n", original)
log.Debugf("\tReturning mapSlice %v\n", mapSlice)
return newSlice return newSlice
} }