dont splat scalars

This commit is contained in:
Mike Farah 2020-10-11 11:45:20 +11:00
parent e0d1aed5b9
commit 1ba1e90e58
3 changed files with 31 additions and 18 deletions

View File

@ -74,15 +74,16 @@ func (d *dataTreeNavigator) GetMatchingNodes(matchingNodes []*CandidateNode, pat
func (d *dataTreeNavigator) getMatchingNodes(matchingNodes *orderedmap.OrderedMap, pathNode *PathTreeNode) (*orderedmap.OrderedMap, error) { func (d *dataTreeNavigator) getMatchingNodes(matchingNodes *orderedmap.OrderedMap, pathNode *PathTreeNode) (*orderedmap.OrderedMap, error) {
log.Debugf("Processing Path: %v", pathNode.PathElement.toString()) 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) return d.traverse(matchingNodes, pathNode.PathElement)
} else { } else {
handler := d.operatorHandlers[pathNode.PathElement.OperationType] handler := d.operatorHandlers[pathNode.PathElement.OperationType]
if handler != nil { if handler != nil {
return handler(d, matchingNodes, pathNode) 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)
} }
} }

View File

@ -612,7 +612,7 @@ func TestDataTreeNavigatorArrayEqualsSelf(t *testing.T) {
- dog - dog
- frog`) - frog`)
path, errPath := treeCreator.ParsePath("*(. == *og)") path, errPath := treeCreator.ParsePath("(. == *og)")
if errPath != nil { if errPath != nil {
t.Error(errPath) t.Error(errPath)
} }

View File

@ -1,6 +1,9 @@
package treeops 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) 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 valuePattern := pathNode.Rhs.PathElement.StringValue
log.Debug("checking %v", candidate) log.Debug("checking %v", candidate)
if pathNode.Lhs.PathElement.PathElementType == SelfReference { // if pathNode.Lhs.PathElement.PathElementType == SelfReference {
if Match(candidate.Node.Value, valuePattern) { // if Match(candidate.Node.Value, valuePattern) {
results.Set(el.Key, el.Value) // results.Set(el.Key, el.Value)
} // }
} else { // } else {
errInChild := findMatchingChildren(d, results, candidate, pathNode.Lhs, valuePattern) errInChild := findMatchingChildren(d, results, candidate, pathNode.Lhs, valuePattern)
if errInChild != nil { if errInChild != nil {
return nil, errInChild 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 { func findMatchingChildren(d *dataTreeNavigator, results *orderedmap.OrderedMap, candidate *CandidateNode, lhs *PathTreeNode, valuePattern string) error {
children, err := splatNode(d, candidate) 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, ") log.Debugf("-- splatted matches, ")
if err != nil { if err != nil {
return err return err
} }
} else {
children = orderedmap.NewOrderedMap()
children.Set(candidate.getKey(), candidate)
}
for childEl := children.Front(); childEl != nil; childEl = childEl.Next() { for childEl := children.Front(); childEl != nil; childEl = childEl.Next() {
childMap := orderedmap.NewOrderedMap() childMap := orderedmap.NewOrderedMap()
childMap.Set(childEl.Key, childEl.Value) childMap.Set(childEl.Key, childEl.Value)