From 93aaa8ccee910d90e91c28a05d90a8c92b25975d Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Fri, 9 Oct 2020 16:43:43 +1100 Subject: [PATCH] array equals! --- pkg/yqlib/treeops/data_tree_navigator_test.go | 31 +++++++++++++++++++ pkg/yqlib/treeops/traverse.go | 6 +--- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/pkg/yqlib/treeops/data_tree_navigator_test.go b/pkg/yqlib/treeops/data_tree_navigator_test.go index 35e04742..55585ed5 100644 --- a/pkg/yqlib/treeops/data_tree_navigator_test.go +++ b/pkg/yqlib/treeops/data_tree_navigator_test.go @@ -359,6 +359,37 @@ func TestDataTreeNavigatorEquals(t *testing.T) { test.AssertResult(t, expected, resultsToString(results)) } +func TestDataTreeNavigatorArrayEquals(t *testing.T) { + + nodes := readDoc(t, `- { b: apple, animal: rabbit } +- { b: banana, animal: cat } +- { b: corn, animal: dog }`) + + path, errPath := treeCreator.ParsePath("(b == apple or animal == dog)") + if errPath != nil { + t.Error(errPath) + } + results, errNav := treeNavigator.GetMatchingNodes(nodes, path) + + if errNav != nil { + t.Error(errNav) + } + + expected := ` +-- Node -- + Document 0, path: [0] + Tag: !!map, Kind: MappingNode, Anchor: + {b: apple, animal: rabbit} + +-- Node -- + Document 0, path: [2] + Tag: !!map, Kind: MappingNode, Anchor: + {b: corn, animal: dog} +` + + test.AssertResult(t, expected, resultsToString(results)) +} + func TestDataTreeNavigatorEqualsTrickey(t *testing.T) { nodes := readDoc(t, `a: diff --git a/pkg/yqlib/treeops/traverse.go b/pkg/yqlib/treeops/traverse.go index e980e731..7b3790d5 100644 --- a/pkg/yqlib/treeops/traverse.go +++ b/pkg/yqlib/treeops/traverse.go @@ -51,7 +51,7 @@ func (t *traverser) traverseMap(candidate *CandidateNode, pathNode *PathElement) func (t *traverser) traverseArray(candidate *CandidateNode, pathNode *PathElement) ([]*CandidateNode, error) { log.Debug("pathNode Value %v", pathNode.Value) - if pathNode.Value == "[*]" { + if pathNode.Value == "[*]" || pathNode.Value == "*" { var contents = candidate.Node.Content var newMatches = make([]*CandidateNode, len(contents)) @@ -92,10 +92,6 @@ func (t *traverser) Traverse(matchingNode *CandidateNode, pathNode *PathElement) case yaml.SequenceNode: log.Debug("its a sequence of %v things!", len(value.Content)) return t.traverseArray(matchingNode, pathNode) - - // switch head := head.(type) { - // case int64: - // return n.recurseArray(value, head, head, tail, pathStack) // default: // if head == "+" {