yq/pkg/yqlib/treeops/data_tree_navigator.go

165 lines
4.6 KiB
Go
Raw Normal View History

2020-10-08 23:59:03 +00:00
package treeops
2020-10-09 01:04:19 +00:00
import (
"github.com/elliotchance/orderedmap"
)
2020-10-08 23:59:03 +00:00
type dataTreeNavigator struct {
traverser Traverser
}
type NavigationPrefs struct {
FollowAlias bool
}
type DataTreeNavigator interface {
GetMatchingNodes(matchingNodes []*CandidateNode, pathNode *PathTreeNode) ([]*CandidateNode, error)
}
func NewDataTreeNavigator(navigationPrefs NavigationPrefs) DataTreeNavigator {
traverse := NewTraverser(navigationPrefs)
return &dataTreeNavigator{traverse}
}
2020-10-09 01:04:19 +00:00
func (d *dataTreeNavigator) traverse(matchMap *orderedmap.OrderedMap, pathNode *PathElement) (*orderedmap.OrderedMap, error) {
2020-10-08 23:59:03 +00:00
log.Debugf("-- Traversing")
2020-10-09 01:04:19 +00:00
var matchingNodeMap = orderedmap.NewOrderedMap()
2020-10-08 23:59:03 +00:00
var newNodes []*CandidateNode
var err error
2020-10-09 01:04:19 +00:00
for el := matchMap.Front(); el != nil; el = el.Next() {
newNodes, err = d.traverser.Traverse(el.Value.(*CandidateNode), pathNode)
2020-10-08 23:59:03 +00:00
if err != nil {
return nil, err
}
2020-10-09 01:04:19 +00:00
for _, n := range newNodes {
matchingNodeMap.Set(n.getKey(), n)
}
2020-10-08 23:59:03 +00:00
}
2020-10-09 01:04:19 +00:00
return matchingNodeMap, nil
2020-10-08 23:59:03 +00:00
}
2020-10-09 04:05:45 +00:00
func (d *dataTreeNavigator) equalsOperation(matchMap *orderedmap.OrderedMap, pathNode *PathTreeNode) (*orderedmap.OrderedMap, error) {
log.Debugf("-- equalsOperation")
var results = orderedmap.NewOrderedMap()
for el := matchMap.Front(); el != nil; el = el.Next() {
elMap := orderedmap.NewOrderedMap()
elMap.Set(el.Key, el.Value)
//need to splat matching nodes, then search through them
splatter := &PathTreeNode{PathElement: &PathElement{
PathElementType: PathKey,
Value: "*",
StringValue: "*",
}}
children, err := d.getMatchingNodes(elMap, splatter)
log.Debugf("-- splatted matches, ")
if err != nil {
return nil, err
}
for childEl := children.Front(); childEl != nil; childEl = childEl.Next() {
childMap := orderedmap.NewOrderedMap()
childMap.Set(childEl.Key, childEl.Value)
childMatches, errChild := d.getMatchingNodes(childMap, pathNode.Lhs)
if errChild != nil {
return nil, errChild
}
if d.containsMatchingValue(childMatches, pathNode.Rhs.PathElement.StringValue) {
results.Set(childEl.Key, childEl.Value)
}
}
}
return results, nil
}
func (d *dataTreeNavigator) containsMatchingValue(matchMap *orderedmap.OrderedMap, valuePattern string) bool {
log.Debugf("-- findMatchingValues")
for el := matchMap.Front(); el != nil; el = el.Next() {
node := el.Value.(*CandidateNode)
if Match(node.Node.Value, valuePattern) {
return true
}
}
return false
}
2020-10-09 01:04:19 +00:00
func (d *dataTreeNavigator) setFunction(op OperationType, lhs *orderedmap.OrderedMap, rhs *orderedmap.OrderedMap) *orderedmap.OrderedMap {
2020-10-09 01:10:46 +00:00
if op == Or {
for el := rhs.Front(); el != nil; el = el.Next() {
node := el.Value.(*CandidateNode)
lhs.Set(node.getKey(), node)
}
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)
}
2020-10-09 01:04:19 +00:00
}
2020-10-09 01:10:46 +00:00
return matchingNodeMap
2020-10-09 00:10:37 +00:00
}
2020-10-08 23:59:03 +00:00
func (d *dataTreeNavigator) GetMatchingNodes(matchingNodes []*CandidateNode, pathNode *PathTreeNode) ([]*CandidateNode, error) {
2020-10-09 01:04:19 +00:00
var matchingNodeMap = orderedmap.NewOrderedMap()
for _, n := range matchingNodes {
matchingNodeMap.Set(n.getKey(), n)
}
matchedNodes, err := d.getMatchingNodes(matchingNodeMap, pathNode)
if err != nil {
return nil, err
}
values := make([]*CandidateNode, 0, matchedNodes.Len())
for el := matchedNodes.Front(); el != nil; el = el.Next() {
values = append(values, el.Value.(*CandidateNode))
}
return values, nil
}
func (d *dataTreeNavigator) getMatchingNodes(matchingNodes *orderedmap.OrderedMap, pathNode *PathTreeNode) (*orderedmap.OrderedMap, error) {
2020-10-08 23:59:03 +00:00
log.Debugf("Processing Path: %v", pathNode.PathElement.toString())
if pathNode.PathElement.PathElementType == PathKey || pathNode.PathElement.PathElementType == ArrayIndex {
return d.traverse(matchingNodes, pathNode.PathElement)
} else {
2020-10-09 01:04:19 +00:00
var lhs, rhs *orderedmap.OrderedMap
2020-10-08 23:59:03 +00:00
var err error
switch pathNode.PathElement.OperationType {
case Traverse:
2020-10-09 01:04:19 +00:00
lhs, err = d.getMatchingNodes(matchingNodes, pathNode.Lhs)
2020-10-08 23:59:03 +00:00
if err != nil {
return nil, err
}
2020-10-09 01:04:19 +00:00
return d.getMatchingNodes(lhs, pathNode.Rhs)
2020-10-09 00:10:37 +00:00
case Or, And:
2020-10-09 01:04:19 +00:00
lhs, err = d.getMatchingNodes(matchingNodes, pathNode.Lhs)
2020-10-09 00:10:37 +00:00
if err != nil {
return nil, err
}
2020-10-09 01:04:19 +00:00
rhs, err = d.getMatchingNodes(matchingNodes, pathNode.Rhs)
2020-10-09 00:10:37 +00:00
if err != nil {
return nil, err
}
2020-10-09 00:37:47 +00:00
return d.setFunction(pathNode.PathElement.OperationType, lhs, rhs), nil
2020-10-09 04:05:45 +00:00
case Equals:
return d.equalsOperation(matchingNodes, pathNode)
2020-10-08 23:59:03 +00:00
// case EqualsSelf:
// return d.findMatchingValues(matchingNodes, pathNode.Rhs)
default:
return nil, nil
}
}
}