From c7ebdda5305392deac0c8227cbc820b4616af858 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Fri, 9 Oct 2020 12:10:46 +1100 Subject: [PATCH] added AND op --- pkg/yqlib/treeops/data_tree_navigator.go | 18 +++++++++--- pkg/yqlib/treeops/data_tree_navigator_test.go | 28 +++++++++++++++++++ 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/pkg/yqlib/treeops/data_tree_navigator.go b/pkg/yqlib/treeops/data_tree_navigator.go index 057f564f..4d76e083 100644 --- a/pkg/yqlib/treeops/data_tree_navigator.go +++ b/pkg/yqlib/treeops/data_tree_navigator.go @@ -42,11 +42,21 @@ func (d *dataTreeNavigator) traverse(matchMap *orderedmap.OrderedMap, pathNode * func (d *dataTreeNavigator) setFunction(op OperationType, lhs *orderedmap.OrderedMap, rhs *orderedmap.OrderedMap) *orderedmap.OrderedMap { - for el := rhs.Front(); el != nil; el = el.Next() { - node := el.Value.(*CandidateNode) - lhs.Set(node.getKey(), node) + if op == Or { + for el := rhs.Front(); el != nil; el = el.Next() { + node := el.Value.(*CandidateNode) + lhs.Set(node.getKey(), node) + } + return lhs } - return lhs + var matchingNodeMap = orderedmap.NewOrderedMap() + for el := lhs.Front(); el != nil; el = el.Next() { + _, exists := rhs.Get(el.Key) + if exists { + matchingNodeMap.Set(el.Key, el.Value) + } + } + return matchingNodeMap } diff --git a/pkg/yqlib/treeops/data_tree_navigator_test.go b/pkg/yqlib/treeops/data_tree_navigator_test.go index 73414224..65611b81 100644 --- a/pkg/yqlib/treeops/data_tree_navigator_test.go +++ b/pkg/yqlib/treeops/data_tree_navigator_test.go @@ -249,3 +249,31 @@ func TestDataTreeNavigatorOrDeDupes(t *testing.T) { test.AssertResult(t, expected, resultsToString(results)) } + +func TestDataTreeNavigatorAnd(t *testing.T) { + + nodes := readDoc(t, `a: + cat: apple + pat: apple + cow: apple + mad: things`) + + path, errPath := treeCreator.ParsePath("a.(*t and c*)") + 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 +` + + test.AssertResult(t, expected, resultsToString(results)) +}