From f95226e2671b9f02dab72ad8e3dd64a853e6c4ee Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Fri, 9 Oct 2020 11:10:37 +1100 Subject: [PATCH] ops work in theory! --- pkg/yqlib/treeops/data_tree_navigator.go | 26 +++-- pkg/yqlib/treeops/data_tree_navigator_test.go | 102 +++++++++++++++++- 2 files changed, 116 insertions(+), 12 deletions(-) diff --git a/pkg/yqlib/treeops/data_tree_navigator.go b/pkg/yqlib/treeops/data_tree_navigator.go index 1b14490..4bfa873 100644 --- a/pkg/yqlib/treeops/data_tree_navigator.go +++ b/pkg/yqlib/treeops/data_tree_navigator.go @@ -34,12 +34,16 @@ func (d *dataTreeNavigator) traverse(matchingNodes []*CandidateNode, pathNode *P return newMatchingNodes, nil } +func (d *dataTreeNavigator) setFunction(op OperationType, lhs []*CandidateNode, rhs []*CandidateNode) ([]*CandidateNode, error) { + return append(lhs, rhs...), nil +} + func (d *dataTreeNavigator) GetMatchingNodes(matchingNodes []*CandidateNode, pathNode *PathTreeNode) ([]*CandidateNode, error) { log.Debugf("Processing Path: %v", pathNode.PathElement.toString()) if pathNode.PathElement.PathElementType == PathKey || pathNode.PathElement.PathElementType == ArrayIndex { return d.traverse(matchingNodes, pathNode.PathElement) } else { - var lhs []*CandidateNode //, rhs + var lhs, rhs []*CandidateNode var err error switch pathNode.PathElement.OperationType { case Traverse: @@ -48,16 +52,16 @@ func (d *dataTreeNavigator) GetMatchingNodes(matchingNodes []*CandidateNode, pat return nil, err } return d.GetMatchingNodes(lhs, pathNode.Rhs) - // case Or, And: - // lhs, err = d.GetMatchingNodes(matchingNodes, pathNode.Lhs) - // if err != nil { - // return nil, err - // } - // rhs, err = d.GetMatchingNodes(matchingNodes, pathNode.Rhs) - // if err != nil { - // return nil, err - // } - // return d.setFunction(pathNode.PathElement, lhs, rhs), nil + case Or, And: + lhs, err = d.GetMatchingNodes(matchingNodes, pathNode.Lhs) + if err != nil { + return nil, err + } + rhs, err = d.GetMatchingNodes(matchingNodes, pathNode.Rhs) + if err != nil { + return nil, err + } + return d.setFunction(pathNode.PathElement.OperationType, lhs, rhs) // case Equals: // lhs, err = d.GetMatchingNodes(matchingNodes, pathNode.Lhs) // if err != nil { diff --git a/pkg/yqlib/treeops/data_tree_navigator_test.go b/pkg/yqlib/treeops/data_tree_navigator_test.go index 0a37737..30742de 100644 --- a/pkg/yqlib/treeops/data_tree_navigator_test.go +++ b/pkg/yqlib/treeops/data_tree_navigator_test.go @@ -102,7 +102,8 @@ func TestDataTreeNavigatorSimpleMismatch(t *testing.T) { func TestDataTreeNavigatorWild(t *testing.T) { nodes := readDoc(t, `a: - cat: apple`) + cat: apple + mad: things`) path, errPath := treeCreator.ParsePath("a.*a*") if errPath != nil { @@ -119,7 +120,106 @@ func TestDataTreeNavigatorWild(t *testing.T) { Document 0, path: [a cat] Tag: !!str, Kind: ScalarNode, Anchor: apple + +-- Node -- + Document 0, path: [a mad] + Tag: !!str, Kind: ScalarNode, Anchor: + things ` test.AssertResult(t, expected, resultsToString(results)) } + +func TestDataTreeNavigatorWildDeepish(t *testing.T) { + + nodes := readDoc(t, `a: + cat: {b: 3} + mad: {b: 4} + fad: {c: t}`) + + path, errPath := treeCreator.ParsePath("a.*a*.b") + if errPath != nil { + t.Error(errPath) + } + results, errNav := treeNavigator.GetMatchingNodes(nodes, path) + + if errNav != nil { + t.Error(errNav) + } + + expected := ` +-- Node -- + Document 0, path: [a cat b] + Tag: !!int, Kind: ScalarNode, Anchor: + 3 + +-- Node -- + Document 0, path: [a mad b] + Tag: !!int, Kind: ScalarNode, Anchor: + 4 +` + + test.AssertResult(t, expected, resultsToString(results)) +} + +func TestDataTreeNavigatorOrSimple(t *testing.T) { + + nodes := readDoc(t, `a: + cat: apple + mad: things`) + + path, errPath := treeCreator.ParsePath("a.(cat or mad)") + if errPath != nil { + t.Error(errPath) + } + results, errNav := treeNavigator.GetMatchingNodes(nodes, path) + + if errNav != nil { + t.Error(errNav) + } + + expected := ` +-- Node -- + Document 0, path: [a cat] + Tag: !!str, Kind: ScalarNode, Anchor: + apple + +-- Node -- + Document 0, path: [a mad] + Tag: !!str, Kind: ScalarNode, Anchor: + things +` + + test.AssertResult(t, expected, resultsToString(results)) +} + +func TestDataTreeNavigatorOrSimpleWithDepth(t *testing.T) { + + nodes := readDoc(t, `a: + cat: {b: 3} + mad: {b: 4} + fad: {c: t}`) + + path, errPath := treeCreator.ParsePath("a.(cat.* or fad.*)") + if errPath != nil { + t.Error(errPath) + } + results, errNav := treeNavigator.GetMatchingNodes(nodes, path) + + if errNav != nil { + t.Error(errNav) + } + + expected := ` +-- Node -- + Document 0, path: [a cat b] + Tag: !!int, Kind: ScalarNode, Anchor: + 3 + +-- Node -- + Document 0, path: [a fad c] + Tag: !!str, Kind: ScalarNode, Anchor: + t +` + test.AssertResult(t, expected, resultsToString(results)) +}