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) {
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)
}
}

View File

@ -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)
}

View File

@ -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 {
// 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)
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)