1
0
mirror of https://github.com/taigrr/yq synced 2025-01-18 04:53:17 -08:00

Added negative index capability

This commit is contained in:
Mike Farah
2020-04-13 10:36:46 +10:00
parent a06320f13c
commit 1f9a3f5f6c
3 changed files with 57 additions and 4 deletions

View File

@@ -260,11 +260,21 @@ func (n *navigator) appendArray(value *yaml.Node, head interface{}, tail []inter
}
func (n *navigator) recurseArray(value *yaml.Node, index int64, head interface{}, tail []interface{}, pathStack []interface{}) error {
for int64(len(value.Content)) <= index {
var contentLength = int64(len(value.Content))
for contentLength <= index {
value.Content = append(value.Content, &yaml.Node{Kind: guessKind(head, tail, 0)})
}
var indexToUse = index
value.Content[index] = n.getOrReplace(value.Content[index], guessKind(head, tail, value.Content[index].Kind))
if indexToUse < 0 {
indexToUse = contentLength + indexToUse
}
return n.doTraverse(value.Content[index], head, tail, append(pathStack, index))
if indexToUse < 0 {
return fmt.Errorf("Index [%v] out of range, array size is %v", index, contentLength)
}
value.Content[indexToUse] = n.getOrReplace(value.Content[indexToUse], guessKind(head, tail, value.Content[indexToUse].Kind))
return n.doTraverse(value.Content[indexToUse], head, tail, append(pathStack, index))
}