mirror of
https://github.com/taigrr/yq
synced 2025-01-18 04:53:17 -08:00
Improved handling of numeric keys
When there is no match at a given path, numeric keys are assumed to be strings. To create an array '+' must be used. e.g: yq n thing[+].cat fred will create an array under thing, whereas yq n thing[0].cat fred will create a map under thing, with a key '0'
This commit is contained in:
parent
c7f5261036
commit
84de9c078d
@ -385,12 +385,11 @@ func TestPrefixCmdArray(t *testing.T) {
|
|||||||
defer removeTempYamlFile(filename)
|
defer removeTempYamlFile(filename)
|
||||||
|
|
||||||
cmd := getRootCommand()
|
cmd := getRootCommand()
|
||||||
result := runCmd(cmd, fmt.Sprintf("prefix %s [0].d.[1]", filename))
|
result := runCmd(cmd, fmt.Sprintf("prefix %s [+].d.[+]", filename))
|
||||||
if result.Error != nil {
|
if result.Error != nil {
|
||||||
t.Error(result.Error)
|
t.Error(result.Error)
|
||||||
}
|
}
|
||||||
expectedOutput := `- d:
|
expectedOutput := `- d:
|
||||||
- null
|
|
||||||
- b:
|
- b:
|
||||||
c: 3
|
c: 3
|
||||||
`
|
`
|
||||||
|
@ -89,14 +89,15 @@ func updatedChildValue(child interface{}, remainingPaths []string, value interfa
|
|||||||
}
|
}
|
||||||
log.Debugf("updatedChildValue for child %v with path %v to set value %v", child, remainingPaths, value)
|
log.Debugf("updatedChildValue for child %v with path %v to set value %v", child, remainingPaths, value)
|
||||||
log.Debugf("type of child is %v", reflect.TypeOf(child))
|
log.Debugf("type of child is %v", reflect.TypeOf(child))
|
||||||
_, nextIndexErr := strconv.ParseInt(remainingPaths[0], 10, 64)
|
|
||||||
arrayCommand := nextIndexErr == nil || remainingPaths[0] == "+" || remainingPaths[0] == "*"
|
|
||||||
switch child := child.(type) {
|
switch child := child.(type) {
|
||||||
case nil:
|
case nil:
|
||||||
if arrayCommand {
|
if remainingPaths[0] == "+" || remainingPaths[0] == "*" {
|
||||||
return writeArray(child, remainingPaths, value)
|
return writeArray(child, remainingPaths, value)
|
||||||
}
|
}
|
||||||
case []interface{}:
|
case []interface{}:
|
||||||
|
_, nextIndexErr := strconv.ParseInt(remainingPaths[0], 10, 64)
|
||||||
|
arrayCommand := nextIndexErr == nil || remainingPaths[0] == "+" || remainingPaths[0] == "*"
|
||||||
if arrayCommand {
|
if arrayCommand {
|
||||||
return writeArray(child, remainingPaths, value)
|
return writeArray(child, remainingPaths, value)
|
||||||
}
|
}
|
||||||
|
@ -260,7 +260,7 @@ a: apple
|
|||||||
b:
|
b:
|
||||||
- c: "4"`
|
- c: "4"`
|
||||||
|
|
||||||
updated := writeMap(data, []string{"b", "0", "c"}, "4")
|
updated := writeMap(data, []string{"b", "+", "c"}, "4")
|
||||||
got, _ := yamlToString(updated)
|
got, _ := yamlToString(updated)
|
||||||
assertResult(t, expected, got)
|
assertResult(t, expected, got)
|
||||||
}
|
}
|
||||||
@ -275,7 +275,7 @@ b:
|
|||||||
d:
|
d:
|
||||||
- "4"`
|
- "4"`
|
||||||
|
|
||||||
updated := writeMap(data, []string{"b", "d", "0"}, "4")
|
updated := writeMap(data, []string{"b", "d", "+"}, "4")
|
||||||
got, _ := yamlToString(updated)
|
got, _ := yamlToString(updated)
|
||||||
assertResult(t, expected, got)
|
assertResult(t, expected, got)
|
||||||
}
|
}
|
||||||
|
@ -1,2 +1,2 @@
|
|||||||
b.c: cat
|
b.c: cat
|
||||||
b.e[0].name: Mike Farah
|
b.e[+].name: Mike Farah
|
||||||
|
Loading…
x
Reference in New Issue
Block a user