From 1ba1e90e588573c7bdbe05076613189dce04e2bb Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Sun, 11 Oct 2020 11:45:20 +1100 Subject: [PATCH] dont splat scalars --- pkg/yqlib/treeops/data_tree_navigator.go | 7 ++-- pkg/yqlib/treeops/data_tree_navigator_test.go | 2 +- pkg/yqlib/treeops/operators.go | 40 ++++++++++++------- 3 files changed, 31 insertions(+), 18 deletions(-) diff --git a/pkg/yqlib/treeops/data_tree_navigator.go b/pkg/yqlib/treeops/data_tree_navigator.go index 6cffe9c..5fb4736 100644 --- a/pkg/yqlib/treeops/data_tree_navigator.go +++ b/pkg/yqlib/treeops/data_tree_navigator.go @@ -74,15 +74,16 @@ func (d *dataTreeNavigator) GetMatchingNodes(matchingNodes []*CandidateNode, pat func (d *dataTreeNavigator) getMatchingNodes(matchingNodes *orderedmap.OrderedMap, pathNode *PathTreeNode) (*orderedmap.OrderedMap, error) { log.Debugf("Processing Path: %v", pathNode.PathElement.toString()) - if pathNode.PathElement.PathElementType == PathKey || pathNode.PathElement.PathElementType == ArrayIndex { + if pathNode.PathElement.PathElementType == SelfReference { + return matchingNodes, nil + } else if pathNode.PathElement.PathElementType == PathKey || pathNode.PathElement.PathElementType == ArrayIndex { return d.traverse(matchingNodes, pathNode.PathElement) } else { handler := d.operatorHandlers[pathNode.PathElement.OperationType] if handler != nil { return handler(d, matchingNodes, pathNode) - } else { - return nil, fmt.Errorf("Unknown operator %v", pathNode.PathElement.OperationType) } + return nil, fmt.Errorf("Unknown operator %v", pathNode.PathElement.OperationType) } } diff --git a/pkg/yqlib/treeops/data_tree_navigator_test.go b/pkg/yqlib/treeops/data_tree_navigator_test.go index 669cc4f..b882ccf 100644 --- a/pkg/yqlib/treeops/data_tree_navigator_test.go +++ b/pkg/yqlib/treeops/data_tree_navigator_test.go @@ -612,7 +612,7 @@ func TestDataTreeNavigatorArrayEqualsSelf(t *testing.T) { - dog - frog`) - path, errPath := treeCreator.ParsePath("*(. == *og)") + path, errPath := treeCreator.ParsePath("(. == *og)") if errPath != nil { t.Error(errPath) } diff --git a/pkg/yqlib/treeops/operators.go b/pkg/yqlib/treeops/operators.go index c23ae0f..3564835 100644 --- a/pkg/yqlib/treeops/operators.go +++ b/pkg/yqlib/treeops/operators.go @@ -1,6 +1,9 @@ package treeops -import "github.com/elliotchance/orderedmap" +import ( + "github.com/elliotchance/orderedmap" + "gopkg.in/yaml.v3" +) type OperatorHandler func(d *dataTreeNavigator, matchingNodes *orderedmap.OrderedMap, pathNode *PathTreeNode) (*orderedmap.OrderedMap, error) @@ -81,16 +84,16 @@ func EqualsOperator(d *dataTreeNavigator, matchMap *orderedmap.OrderedMap, pathN valuePattern := pathNode.Rhs.PathElement.StringValue log.Debug("checking %v", candidate) - if pathNode.Lhs.PathElement.PathElementType == SelfReference { - if Match(candidate.Node.Value, valuePattern) { - results.Set(el.Key, el.Value) - } - } else { - errInChild := findMatchingChildren(d, results, candidate, pathNode.Lhs, valuePattern) - if errInChild != nil { - return nil, errInChild - } + // if pathNode.Lhs.PathElement.PathElementType == SelfReference { + // if Match(candidate.Node.Value, valuePattern) { + // results.Set(el.Key, el.Value) + // } + // } else { + errInChild := findMatchingChildren(d, results, candidate, pathNode.Lhs, valuePattern) + if errInChild != nil { + return nil, errInChild } + // } } @@ -98,11 +101,20 @@ func EqualsOperator(d *dataTreeNavigator, matchMap *orderedmap.OrderedMap, pathN } func findMatchingChildren(d *dataTreeNavigator, results *orderedmap.OrderedMap, candidate *CandidateNode, lhs *PathTreeNode, valuePattern string) error { - children, err := splatNode(d, candidate) - log.Debugf("-- splatted matches, ") - if err != nil { - return err + var children *orderedmap.OrderedMap + var err error + // don't splat scalars. + if candidate.Node.Kind != yaml.ScalarNode { + children, err = splatNode(d, candidate) + log.Debugf("-- splatted matches, ") + if err != nil { + return err + } + } else { + children = orderedmap.NewOrderedMap() + children.Set(candidate.getKey(), candidate) } + for childEl := children.Front(); childEl != nil; childEl = childEl.Next() { childMap := orderedmap.NewOrderedMap() childMap.Set(childEl.Key, childEl.Value)