mirror of
https://github.com/mikefarah/yq.git
synced 2024-11-13 22:38:04 +00:00
Splat array is now the fallback instead of parsing int
This commit is contained in:
parent
4a5bd0ff5b
commit
4dbe3636c2
@ -438,21 +438,21 @@ true`
|
|||||||
func TestReadCmd_ArrayYaml_ErrorBadPath(t *testing.T) {
|
func TestReadCmd_ArrayYaml_ErrorBadPath(t *testing.T) {
|
||||||
cmd := getRootCommand()
|
cmd := getRootCommand()
|
||||||
result := test.RunCmd(cmd, "read ../examples/array.yaml [x].gather_facts")
|
result := test.RunCmd(cmd, "read ../examples/array.yaml [x].gather_facts")
|
||||||
if result.Error == nil {
|
if result.Error != nil {
|
||||||
t.Error("Expected command to fail due to missing arg")
|
t.Error(result.Error)
|
||||||
}
|
}
|
||||||
expectedOutput := `Error reading path in document index 0: Error parsing array index 'x' for '': strconv.ParseInt: parsing "x": invalid syntax`
|
expectedOutput := ``
|
||||||
test.AssertResult(t, expectedOutput, result.Error.Error())
|
test.AssertResult(t, expectedOutput, result.Output)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestReadCmd_ArrayYaml_Splat_ErrorBadPath(t *testing.T) {
|
func TestReadCmd_ArrayYaml_Splat_ErrorBadPath(t *testing.T) {
|
||||||
cmd := getRootCommand()
|
cmd := getRootCommand()
|
||||||
result := test.RunCmd(cmd, "read ../examples/array.yaml [*].roles[x]")
|
result := test.RunCmd(cmd, "read ../examples/array.yaml [*].roles[x]")
|
||||||
if result.Error == nil {
|
if result.Error != nil {
|
||||||
t.Error("Expected command to fail due to missing arg")
|
t.Error(result.Error)
|
||||||
}
|
}
|
||||||
expectedOutput := `Error reading path in document index 0: Error parsing array index 'x' for '[0].roles': strconv.ParseInt: parsing "x": invalid syntax`
|
expectedOutput := ``
|
||||||
test.AssertResult(t, expectedOutput, result.Error.Error())
|
test.AssertResult(t, expectedOutput, result.Output)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestReadCmd_Error(t *testing.T) {
|
func TestReadCmd_Error(t *testing.T) {
|
||||||
@ -505,8 +505,11 @@ func TestReadCmd_ErrorBadPath(t *testing.T) {
|
|||||||
|
|
||||||
cmd := getRootCommand()
|
cmd := getRootCommand()
|
||||||
result := test.RunCmd(cmd, fmt.Sprintf("read %s b.d.*.[x]", filename))
|
result := test.RunCmd(cmd, fmt.Sprintf("read %s b.d.*.[x]", filename))
|
||||||
expectedOutput := `Error reading path in document index 0: Error parsing array index 'x' for 'b.d.e': strconv.ParseInt: parsing "x": invalid syntax`
|
if result.Error != nil {
|
||||||
test.AssertResult(t, expectedOutput, result.Error.Error())
|
t.Error(result.Error)
|
||||||
|
}
|
||||||
|
expectedOutput := ``
|
||||||
|
test.AssertResult(t, expectedOutput, result.Output)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestReadCmd_Verbose(t *testing.T) {
|
func TestReadCmd_Verbose(t *testing.T) {
|
||||||
|
@ -3,7 +3,6 @@ package yqlib
|
|||||||
import (
|
import (
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
errors "github.com/pkg/errors"
|
|
||||||
yaml "gopkg.in/yaml.v3"
|
yaml "gopkg.in/yaml.v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -69,12 +68,15 @@ func (n *navigator) recurse(value *yaml.Node, head string, tail []string, pathSt
|
|||||||
return n.recurseMap(value, head, tail, pathStack)
|
return n.recurseMap(value, head, tail, pathStack)
|
||||||
case yaml.SequenceNode:
|
case yaml.SequenceNode:
|
||||||
log.Debug("its a sequence of %v things!", len(value.Content))
|
log.Debug("its a sequence of %v things!", len(value.Content))
|
||||||
if n.navigationStrategy.GetPathParser().IsPathExpression(head) {
|
|
||||||
return n.splatArray(value, head, tail, pathStack)
|
var index, errorParsingIndex = strconv.ParseInt(head, 10, 64) // nolint
|
||||||
|
if errorParsingIndex == nil {
|
||||||
|
return n.recurseArray(value, index, head, tail, pathStack)
|
||||||
} else if head == "+" {
|
} else if head == "+" {
|
||||||
return n.appendArray(value, head, tail, pathStack)
|
return n.appendArray(value, head, tail, pathStack)
|
||||||
}
|
}
|
||||||
return n.recurseArray(value, head, tail, pathStack)
|
return n.splatArray(value, head, tail, pathStack)
|
||||||
|
|
||||||
case yaml.AliasNode:
|
case yaml.AliasNode:
|
||||||
log.Debug("its an alias!")
|
log.Debug("its an alias!")
|
||||||
DebugNode(value.Alias)
|
DebugNode(value.Alias)
|
||||||
@ -233,12 +235,7 @@ func (n *navigator) appendArray(value *yaml.Node, head string, tail []string, pa
|
|||||||
return n.doTraverse(&newNode, head, tail, append(pathStack, len(value.Content)-1))
|
return n.doTraverse(&newNode, head, tail, append(pathStack, len(value.Content)-1))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *navigator) recurseArray(value *yaml.Node, head string, tail []string, pathStack []interface{}) error {
|
func (n *navigator) recurseArray(value *yaml.Node, index int64, head string, tail []string, pathStack []interface{}) error {
|
||||||
var index, err = strconv.ParseInt(head, 10, 64) // nolint
|
|
||||||
if err != nil {
|
|
||||||
return errors.Wrapf(err, "Error parsing array index '%v' for '%v'", head, pathStackToString(pathStack))
|
|
||||||
}
|
|
||||||
|
|
||||||
for int64(len(value.Content)) <= index {
|
for int64(len(value.Content)) <= index {
|
||||||
value.Content = append(value.Content, &yaml.Node{Kind: guessKind(head, tail, 0)})
|
value.Content = append(value.Content, &yaml.Node{Kind: guessKind(head, tail, 0)})
|
||||||
}
|
}
|
||||||
|
@ -55,7 +55,8 @@ func (p *pathParser) MatchesNextPathElement(nodeContext NodeContext, nodeKey str
|
|||||||
navigator := NewDataNavigator(navigationStrategy)
|
navigator := NewDataNavigator(navigationStrategy)
|
||||||
err := navigator.Traverse(nodeContext.Node, p.ParsePath(path))
|
err := navigator.Traverse(nodeContext.Node, p.ParsePath(path))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Info(err.Error())
|
log.Error("Error deep recursing - ignoring")
|
||||||
|
log.Error(err.Error())
|
||||||
}
|
}
|
||||||
log.Debug("done deep recursing, found %v matches", len(navigationStrategy.GetVisitedNodes()))
|
log.Debug("done deep recursing, found %v matches", len(navigationStrategy.GetVisitedNodes()))
|
||||||
return len(navigationStrategy.GetVisitedNodes()) > 0
|
return len(navigationStrategy.GetVisitedNodes()) > 0
|
||||||
|
Loading…
Reference in New Issue
Block a user