mirror of
https://github.com/mikefarah/yq.git
synced 2025-01-26 00:15:36 +00:00
Fixed updating array bug (Issue 3 in git)
This commit is contained in:
parent
037314af8a
commit
0d75edfb67
@ -9,11 +9,25 @@ func write(context map[interface{}]interface{}, head string, tail []string, valu
|
|||||||
if len(tail) == 0 {
|
if len(tail) == 0 {
|
||||||
context[head] = value
|
context[head] = value
|
||||||
} else {
|
} else {
|
||||||
// e.g. if updating a.b.c, we need to get the 'b' map...
|
// e.g. if updating a.b.c, we need to get the 'b', this could be a map or an array
|
||||||
toUpdate := readMap(context, head, tail[0:len(tail)-1]).(map[interface{}]interface{})
|
var parent = readMap(context, head, tail[0:len(tail)-1])
|
||||||
// and then set the 'c' key.
|
switch parent.(type) {
|
||||||
key := (tail[len(tail)-1])
|
case map[interface{}]interface{}:
|
||||||
toUpdate[key] = value
|
toUpdate := parent.(map[interface{}]interface{})
|
||||||
|
// b is a map, update the key 'c' to the supplied value
|
||||||
|
key := (tail[len(tail)-1])
|
||||||
|
toUpdate[key] = value
|
||||||
|
case []interface{}:
|
||||||
|
toUpdate := parent.([]interface{})
|
||||||
|
// b is an array, update it at index 'c' to the supplied value
|
||||||
|
rawIndex := (tail[len(tail)-1])
|
||||||
|
index, err := strconv.ParseInt(rawIndex, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
die("Error accessing array: %v", err)
|
||||||
|
}
|
||||||
|
toUpdate[index] = value
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -119,6 +119,18 @@ b:
|
|||||||
assertResult(t, "4", b["c"].(string))
|
assertResult(t, "4", b["c"].(string))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestWrite_array(t *testing.T) {
|
||||||
|
var data = parseData(`
|
||||||
|
b:
|
||||||
|
- aa
|
||||||
|
`)
|
||||||
|
|
||||||
|
write(data, "b", []string{"0"}, "bb")
|
||||||
|
|
||||||
|
b := data["b"].([]interface{})
|
||||||
|
assertResult(t, "bb", b[0].(string))
|
||||||
|
}
|
||||||
|
|
||||||
func TestWrite_with_no_tail(t *testing.T) {
|
func TestWrite_with_no_tail(t *testing.T) {
|
||||||
var data = parseData(`
|
var data = parseData(`
|
||||||
b:
|
b:
|
||||||
|
Loading…
Reference in New Issue
Block a user