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

@ -201,6 +201,49 @@ func TestReadArrayCmd(t *testing.T) {
test.AssertResult(t, "b.e.[1].name: sam\n", result.Output)
}
func TestReadArrayBackwardsCmd(t *testing.T) {
content := `- one
- two
- three`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("read -p pv %s [-1]", filename))
if result.Error != nil {
t.Error(result.Error)
}
test.AssertResult(t, "'[-1]': three\n", result.Output)
}
func TestReadArrayBackwardsNegative0Cmd(t *testing.T) {
content := `- one
- two
- three`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("read -p pv %s [-0]", filename))
if result.Error != nil {
t.Error(result.Error)
}
test.AssertResult(t, "'[0]': one\n", result.Output)
}
func TestReadArrayBackwardsPastLimitCmd(t *testing.T) {
content := `- one
- two
- three`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("read -p pv %s [-4]", filename))
expectedOutput := "Error reading path in document index 0: Index [-4] out of range, array size is 3"
test.AssertResult(t, expectedOutput, result.Error.Error())
}
func TestReadArrayLengthCmd(t *testing.T) {
content := `- things
- whatever

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))
}

View File

@ -1,4 +1,4 @@
#!/bin/sh
set -e
wget -O- -nv https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s v1.23.1
wget -O- -nv https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s v1.24.0
go get golang.org/x/tools/cmd/goimports