From 4dbe3636c202deaf8f17b0b9b53a99ced0b5ba47 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Thu, 30 Jan 2020 15:11:47 +1100 Subject: [PATCH] Splat array is now the fallback instead of parsing int --- cmd/commands_test.go | 23 +++++++++++++---------- pkg/yqlib/data_navigator.go | 17 +++++++---------- pkg/yqlib/path_parser.go | 3 ++- 3 files changed, 22 insertions(+), 21 deletions(-) diff --git a/cmd/commands_test.go b/cmd/commands_test.go index 220728f..fdbd4ac 100644 --- a/cmd/commands_test.go +++ b/cmd/commands_test.go @@ -438,21 +438,21 @@ true` func TestReadCmd_ArrayYaml_ErrorBadPath(t *testing.T) { cmd := getRootCommand() result := test.RunCmd(cmd, "read ../examples/array.yaml [x].gather_facts") - if result.Error == nil { - t.Error("Expected command to fail due to missing arg") + if result.Error != nil { + t.Error(result.Error) } - expectedOutput := `Error reading path in document index 0: Error parsing array index 'x' for '': strconv.ParseInt: parsing "x": invalid syntax` - test.AssertResult(t, expectedOutput, result.Error.Error()) + expectedOutput := `` + test.AssertResult(t, expectedOutput, result.Output) } func TestReadCmd_ArrayYaml_Splat_ErrorBadPath(t *testing.T) { cmd := getRootCommand() result := test.RunCmd(cmd, "read ../examples/array.yaml [*].roles[x]") - if result.Error == nil { - t.Error("Expected command to fail due to missing arg") + if result.Error != nil { + 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` - test.AssertResult(t, expectedOutput, result.Error.Error()) + expectedOutput := `` + test.AssertResult(t, expectedOutput, result.Output) } func TestReadCmd_Error(t *testing.T) { @@ -505,8 +505,11 @@ func TestReadCmd_ErrorBadPath(t *testing.T) { cmd := getRootCommand() 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` - test.AssertResult(t, expectedOutput, result.Error.Error()) + if result.Error != nil { + t.Error(result.Error) + } + expectedOutput := `` + test.AssertResult(t, expectedOutput, result.Output) } func TestReadCmd_Verbose(t *testing.T) { diff --git a/pkg/yqlib/data_navigator.go b/pkg/yqlib/data_navigator.go index b393216..0ff63b2 100644 --- a/pkg/yqlib/data_navigator.go +++ b/pkg/yqlib/data_navigator.go @@ -3,7 +3,6 @@ package yqlib import ( "strconv" - errors "github.com/pkg/errors" 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) case yaml.SequenceNode: 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 == "+" { return n.appendArray(value, head, tail, pathStack) } - return n.recurseArray(value, head, tail, pathStack) + return n.splatArray(value, head, tail, pathStack) + case yaml.AliasNode: log.Debug("its an 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)) } -func (n *navigator) recurseArray(value *yaml.Node, 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)) - } - +func (n *navigator) recurseArray(value *yaml.Node, index int64, head string, tail []string, pathStack []interface{}) error { for int64(len(value.Content)) <= index { value.Content = append(value.Content, &yaml.Node{Kind: guessKind(head, tail, 0)}) } diff --git a/pkg/yqlib/path_parser.go b/pkg/yqlib/path_parser.go index 6a96d69..ae680a8 100644 --- a/pkg/yqlib/path_parser.go +++ b/pkg/yqlib/path_parser.go @@ -55,7 +55,8 @@ func (p *pathParser) MatchesNextPathElement(nodeContext NodeContext, nodeKey str navigator := NewDataNavigator(navigationStrategy) err := navigator.Traverse(nodeContext.Node, p.ParsePath(path)) 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())) return len(navigationStrategy.GetVisitedNodes()) > 0