yq/pkg/yqlib/treeops/data_tree_navigator.go

81 lines
2.3 KiB
Go
Raw Normal View History

2020-10-08 23:59:03 +00:00
package treeops
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}
}
func (d *dataTreeNavigator) traverse(matchingNodes []*CandidateNode, pathNode *PathElement) ([]*CandidateNode, error) {
log.Debugf("-- Traversing")
var newMatchingNodes = make([]*CandidateNode, 0)
var newNodes []*CandidateNode
var err error
for _, node := range matchingNodes {
newNodes, err = d.traverser.Traverse(node, pathNode)
if err != nil {
return nil, err
}
newMatchingNodes = append(newMatchingNodes, newNodes...)
}
return newMatchingNodes, nil
}
2020-10-09 00:37:47 +00:00
func (d *dataTreeNavigator) setFunction(op OperationType, lhs []*CandidateNode, rhs []*CandidateNode) []*CandidateNode {
return append(lhs, rhs...)
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) {
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 00:10:37 +00:00
var lhs, rhs []*CandidateNode
2020-10-08 23:59:03 +00:00
var err error
switch pathNode.PathElement.OperationType {
case Traverse:
lhs, err = d.GetMatchingNodes(matchingNodes, pathNode.Lhs)
if err != nil {
return nil, err
}
return d.GetMatchingNodes(lhs, pathNode.Rhs)
2020-10-09 00:10:37 +00:00
case Or, And:
lhs, err = d.GetMatchingNodes(matchingNodes, pathNode.Lhs)
if err != nil {
return nil, err
}
rhs, err = d.GetMatchingNodes(matchingNodes, pathNode.Rhs)
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-08 23:59:03 +00:00
// case Equals:
// lhs, err = d.GetMatchingNodes(matchingNodes, pathNode.Lhs)
// if err != nil {
// return nil, err
// }
// return d.findMatchingValues(lhs, pathNode.Rhs)
// case EqualsSelf:
// return d.findMatchingValues(matchingNodes, pathNode.Rhs)
default:
return nil, nil
}
}
}