mirror of
https://github.com/mikefarah/yq.git
synced 2025-03-10 11:15:36 +00:00
fixing delete splat
This commit is contained in:
parent
774badfef4
commit
b2fe3e6738
@ -874,6 +874,9 @@ b:
|
|||||||
hi:
|
hi:
|
||||||
c: things
|
c: things
|
||||||
d: something else
|
d: something else
|
||||||
|
hello:
|
||||||
|
c: things2
|
||||||
|
d: something else2
|
||||||
there:
|
there:
|
||||||
c: more things
|
c: more things
|
||||||
d: more something else
|
d: more something else
|
||||||
@ -891,12 +894,41 @@ b:
|
|||||||
b:
|
b:
|
||||||
hi:
|
hi:
|
||||||
d: something else
|
d: something else
|
||||||
|
hello:
|
||||||
|
d: something else2
|
||||||
there:
|
there:
|
||||||
d: more something else
|
d: more something else
|
||||||
`
|
`
|
||||||
assertResult(t, expectedOutput, result.Output)
|
assertResult(t, expectedOutput, result.Output)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDeleteSplatArrayYaml(t *testing.T) {
|
||||||
|
content := `a: 2
|
||||||
|
b:
|
||||||
|
hi:
|
||||||
|
- thing: item1
|
||||||
|
name: fred
|
||||||
|
- thing: item2
|
||||||
|
name: sam
|
||||||
|
`
|
||||||
|
filename := writeTempYamlFile(content)
|
||||||
|
defer removeTempYamlFile(filename)
|
||||||
|
|
||||||
|
cmd := getRootCommand()
|
||||||
|
result := runCmd(cmd, fmt.Sprintf("delete -v %s b.hi[*].thing", filename))
|
||||||
|
if result.Error != nil {
|
||||||
|
t.Error(result.Error)
|
||||||
|
}
|
||||||
|
|
||||||
|
expectedOutput := `a: 2
|
||||||
|
b:
|
||||||
|
hi:
|
||||||
|
- name: fred
|
||||||
|
- name: sam
|
||||||
|
`
|
||||||
|
assertResult(t, expectedOutput, result.Output)
|
||||||
|
}
|
||||||
|
|
||||||
func TestDeleteSplatPrefixYaml(t *testing.T) {
|
func TestDeleteSplatPrefixYaml(t *testing.T) {
|
||||||
content := `a: 2
|
content := `a: 2
|
||||||
b:
|
b:
|
||||||
|
@ -232,13 +232,13 @@ func calculateValue(value interface{}, tail []string) (interface{}, error) {
|
|||||||
return value, nil
|
return value, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func deleteMap(context interface{}, paths []string) yaml.MapSlice {
|
func deleteMap(context interface{}, paths []string) (yaml.MapSlice, error) {
|
||||||
log.Debugf("deleteMap for %v for %v\n", paths, context)
|
log.Debugf("deleteMap for %v for %v\n", paths, context)
|
||||||
|
|
||||||
mapSlice := getMapSlice(context)
|
mapSlice := getMapSlice(context)
|
||||||
|
|
||||||
if len(paths) == 0 {
|
if len(paths) == 0 {
|
||||||
return mapSlice
|
return mapSlice, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var index int
|
var index int
|
||||||
@ -246,21 +246,29 @@ func deleteMap(context interface{}, paths []string) yaml.MapSlice {
|
|||||||
for index, child = range mapSlice {
|
for index, child = range mapSlice {
|
||||||
if matchesKey(paths[0], child.Key) {
|
if matchesKey(paths[0], child.Key) {
|
||||||
log.Debugf("\tMatched [%v] with [%v] at index %v", paths[0], child.Key, index)
|
log.Debugf("\tMatched [%v] with [%v] at index %v", paths[0], child.Key, index)
|
||||||
mapSlice = deleteEntryInMap(mapSlice, child, index, paths)
|
var badDelete error
|
||||||
|
mapSlice, badDelete = deleteEntryInMap(mapSlice, child, index, paths)
|
||||||
|
if badDelete != nil {
|
||||||
|
return nil, badDelete
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return mapSlice
|
return mapSlice, nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func deleteEntryInMap(original yaml.MapSlice, child yaml.MapItem, index int, paths []string) yaml.MapSlice {
|
func deleteEntryInMap(original yaml.MapSlice, child yaml.MapItem, index int, paths []string) (yaml.MapSlice, error) {
|
||||||
remainingPaths := paths[1:]
|
remainingPaths := paths[1:]
|
||||||
|
|
||||||
var newSlice yaml.MapSlice
|
var newSlice yaml.MapSlice
|
||||||
if len(remainingPaths) > 0 {
|
if len(remainingPaths) > 0 {
|
||||||
newChild := yaml.MapItem{Key: child.Key}
|
newChild := yaml.MapItem{Key: child.Key}
|
||||||
newChild.Value = deleteChildValue(child.Value, remainingPaths)
|
var errorDeleting error
|
||||||
|
newChild.Value, errorDeleting = deleteChildValue(child.Value, remainingPaths)
|
||||||
|
if errorDeleting != nil {
|
||||||
|
return nil, errorDeleting
|
||||||
|
}
|
||||||
|
|
||||||
newSlice = make(yaml.MapSlice, len(original))
|
newSlice = make(yaml.MapSlice, len(original))
|
||||||
for i := range original {
|
for i := range original {
|
||||||
@ -277,26 +285,38 @@ func deleteEntryInMap(original yaml.MapSlice, child yaml.MapItem, index int, pat
|
|||||||
}
|
}
|
||||||
|
|
||||||
log.Debugf("\tReturning original %v\n", original)
|
log.Debugf("\tReturning original %v\n", original)
|
||||||
return newSlice
|
return newSlice, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func deleteArray(context interface{}, paths []string, index int64) interface{} {
|
func deleteArraySplat(array []interface{}, tail []string) (interface{}, error) {
|
||||||
log.Debugf("deleteArray for %v for %v\n", paths, context)
|
log.Debugf("deleteArraySplat for %v for %v\n", tail, array)
|
||||||
|
var newArray = make([]interface{}, len(array))
|
||||||
array, ok := getArray(context)
|
for index, value := range array {
|
||||||
if !ok {
|
val, err := deleteChildValue(value, tail)
|
||||||
// did not get an array
|
if err != nil {
|
||||||
return context
|
return nil, err
|
||||||
|
}
|
||||||
|
newArray[index] = val
|
||||||
}
|
}
|
||||||
|
return newArray, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func deleteArray(array []interface{}, paths []string, index int64) (interface{}, error) {
|
||||||
|
log.Debugf("deleteArray for %v for %v\n", paths, array)
|
||||||
|
|
||||||
if index >= int64(len(array)) {
|
if index >= int64(len(array)) {
|
||||||
return array
|
return array, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
remainingPaths := paths[1:]
|
remainingPaths := paths[1:]
|
||||||
if len(remainingPaths) > 0 {
|
if len(remainingPaths) > 0 {
|
||||||
// Recurse into the array element at index
|
// Recurse into the array element at index
|
||||||
array[index] = deleteMap(array[index], remainingPaths)
|
var errorDeleting error
|
||||||
|
array[index], errorDeleting = deleteMap(array[index], remainingPaths)
|
||||||
|
if errorDeleting != nil {
|
||||||
|
return nil, errorDeleting
|
||||||
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// Delete the array element at index
|
// Delete the array element at index
|
||||||
array = append(array[:index], array[index+1:]...)
|
array = append(array[:index], array[index+1:]...)
|
||||||
@ -304,19 +324,25 @@ func deleteArray(context interface{}, paths []string, index int64) interface{} {
|
|||||||
}
|
}
|
||||||
|
|
||||||
log.Debugf("\tReturning array: %v\n", array)
|
log.Debugf("\tReturning array: %v\n", array)
|
||||||
return array
|
return array, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func deleteChildValue(child interface{}, remainingPaths []string) interface{} {
|
func deleteChildValue(child interface{}, remainingPaths []string) (interface{}, error) {
|
||||||
log.Debugf("deleteChildValue for %v for %v\n", remainingPaths, child)
|
log.Debugf("deleteChildValue for %v for %v\n", remainingPaths, child)
|
||||||
|
var head = remainingPaths[0]
|
||||||
idx, nextIndexErr := strconv.ParseInt(remainingPaths[0], 10, 64)
|
var tail = remainingPaths[1:]
|
||||||
if nextIndexErr != nil {
|
switch child := child.(type) {
|
||||||
// must be a map
|
case yaml.MapSlice:
|
||||||
log.Debugf("\tdetected a map, invoking deleteMap\n")
|
|
||||||
return deleteMap(child, remainingPaths)
|
return deleteMap(child, remainingPaths)
|
||||||
|
case []interface{}:
|
||||||
|
if head == "*" {
|
||||||
|
return deleteArraySplat(child, tail)
|
||||||
|
}
|
||||||
|
index, err := strconv.ParseInt(head, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error accessing array: %v", err)
|
||||||
|
}
|
||||||
|
return deleteArray(child, remainingPaths, index)
|
||||||
}
|
}
|
||||||
|
return child, nil
|
||||||
log.Debugf("\tdetected an array, so traversing element with index %d\n", idx)
|
|
||||||
return deleteArray(child, remainingPaths, idx)
|
|
||||||
}
|
}
|
||||||
|
@ -330,7 +330,7 @@ b: 456
|
|||||||
b: 456
|
b: 456
|
||||||
`)
|
`)
|
||||||
|
|
||||||
result := deleteMap(data, []string{"a"})
|
result, _ := deleteMap(data, []string{"a"})
|
||||||
assertResult(t, fmt.Sprintf("%v", expected), fmt.Sprintf("%v", result))
|
assertResult(t, fmt.Sprintf("%v", expected), fmt.Sprintf("%v", result))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -339,7 +339,7 @@ func TestDelete_index_to_string(t *testing.T) {
|
|||||||
var data = parseData(`
|
var data = parseData(`
|
||||||
a: mystring
|
a: mystring
|
||||||
`)
|
`)
|
||||||
result := deleteMap(data, []string{"a", "0"})
|
result, _ := deleteMap(data, []string{"a", "0"})
|
||||||
assertResult(t, fmt.Sprintf("%v", data), fmt.Sprintf("%v", result))
|
assertResult(t, fmt.Sprintf("%v", data), fmt.Sprintf("%v", result))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -350,7 +350,7 @@ a: [3, 4]
|
|||||||
var expected = parseData(`
|
var expected = parseData(`
|
||||||
a: [3]
|
a: [3]
|
||||||
`)
|
`)
|
||||||
result := deleteMap(data, []string{"a", "1"})
|
result, _ := deleteMap(data, []string{"a", "1"})
|
||||||
assertResult(t, fmt.Sprintf("%v", expected), fmt.Sprintf("%v", result))
|
assertResult(t, fmt.Sprintf("%v", expected), fmt.Sprintf("%v", result))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -358,7 +358,7 @@ func TestDelete_list_index_beyond_bounds(t *testing.T) {
|
|||||||
var data = parseData(`
|
var data = parseData(`
|
||||||
a: [3, 4]
|
a: [3, 4]
|
||||||
`)
|
`)
|
||||||
result := deleteMap(data, []string{"a", "5"})
|
result, _ := deleteMap(data, []string{"a", "5"})
|
||||||
assertResult(t, fmt.Sprintf("%v", data), fmt.Sprintf("%v", result))
|
assertResult(t, fmt.Sprintf("%v", data), fmt.Sprintf("%v", result))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -366,7 +366,7 @@ func TestDelete_list_index_out_of_bounds_by_1(t *testing.T) {
|
|||||||
var data = parseData(`
|
var data = parseData(`
|
||||||
a: [3, 4]
|
a: [3, 4]
|
||||||
`)
|
`)
|
||||||
result := deleteMap(data, []string{"a", "2"})
|
result, _ := deleteMap(data, []string{"a", "2"})
|
||||||
assertResult(t, fmt.Sprintf("%v", data), fmt.Sprintf("%v", result))
|
assertResult(t, fmt.Sprintf("%v", data), fmt.Sprintf("%v", result))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -376,7 +376,7 @@ a: [3, 4]
|
|||||||
b:
|
b:
|
||||||
- name: test
|
- name: test
|
||||||
`)
|
`)
|
||||||
result := deleteMap(data, []string{})
|
result, _ := deleteMap(data, []string{})
|
||||||
assertResult(t, fmt.Sprintf("%v", data), fmt.Sprintf("%v", result))
|
assertResult(t, fmt.Sprintf("%v", data), fmt.Sprintf("%v", result))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -394,6 +394,6 @@ b:
|
|||||||
- name: john
|
- name: john
|
||||||
value: test
|
value: test
|
||||||
`)
|
`)
|
||||||
result := deleteMap(data, []string{"b", "0", "name"})
|
result, _ := deleteMap(data, []string{"b", "0", "name"})
|
||||||
assertResult(t, fmt.Sprintf("%v", expected), fmt.Sprintf("%v", result))
|
assertResult(t, fmt.Sprintf("%v", expected), fmt.Sprintf("%v", result))
|
||||||
}
|
}
|
||||||
|
2
yq.go
2
yq.go
@ -503,7 +503,7 @@ func deleteProperty(cmd *cobra.Command, args []string) error {
|
|||||||
var updateData = func(dataBucket interface{}, currentIndex int) (interface{}, error) {
|
var updateData = func(dataBucket interface{}, currentIndex int) (interface{}, error) {
|
||||||
if updateAll || currentIndex == docIndexInt {
|
if updateAll || currentIndex == docIndexInt {
|
||||||
log.Debugf("Deleting path in doc %v", currentIndex)
|
log.Debugf("Deleting path in doc %v", currentIndex)
|
||||||
return deleteChildValue(dataBucket, paths), nil
|
return deleteChildValue(dataBucket, paths)
|
||||||
}
|
}
|
||||||
return dataBucket, nil
|
return dataBucket, nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user