2020-10-08 23:59:03 +00:00
|
|
|
package treeops
|
|
|
|
|
2020-10-09 01:04:19 +00:00
|
|
|
import (
|
2020-10-10 04:00:39 +00:00
|
|
|
"fmt"
|
|
|
|
|
2020-10-09 01:04:19 +00:00
|
|
|
"github.com/elliotchance/orderedmap"
|
2020-10-17 11:10:47 +00:00
|
|
|
"gopkg.in/op/go-logging.v1"
|
2020-10-09 01:04:19 +00:00
|
|
|
)
|
|
|
|
|
2020-10-08 23:59:03 +00:00
|
|
|
type dataTreeNavigator struct {
|
2020-10-11 23:44:33 +00:00
|
|
|
leafTraverser LeafTraverser
|
2020-10-08 23:59:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type NavigationPrefs struct {
|
|
|
|
FollowAlias bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type DataTreeNavigator interface {
|
|
|
|
GetMatchingNodes(matchingNodes []*CandidateNode, pathNode *PathTreeNode) ([]*CandidateNode, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDataTreeNavigator(navigationPrefs NavigationPrefs) DataTreeNavigator {
|
2020-10-10 04:00:39 +00:00
|
|
|
leafTraverser := NewLeafTraverser(navigationPrefs)
|
2020-10-11 23:44:33 +00:00
|
|
|
return &dataTreeNavigator{leafTraverser}
|
2020-10-08 23:59:03 +00:00
|
|
|
}
|
|
|
|
|
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() {
|
2020-10-10 04:00:39 +00:00
|
|
|
newNodes, err = d.leafTraverser.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 {
|
2020-10-13 01:51:37 +00:00
|
|
|
matchingNodeMap.Set(n.GetKey(), n)
|
2020-10-09 01:04:19 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2020-10-13 01:51:37 +00:00
|
|
|
matchingNodeMap.Set(n.GetKey(), n)
|
2020-10-09 01:04:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-13 01:51:37 +00:00
|
|
|
if pathNode == nil {
|
|
|
|
log.Debugf("getMatchingNodes - nothing to do")
|
|
|
|
return matchingNodes, nil
|
|
|
|
}
|
2020-10-08 23:59:03 +00:00
|
|
|
log.Debugf("Processing Path: %v", pathNode.PathElement.toString())
|
2020-10-17 11:10:47 +00:00
|
|
|
if log.IsEnabledFor(logging.DEBUG) {
|
|
|
|
for el := matchingNodes.Front(); el != nil; el = el.Next() {
|
|
|
|
log.Debug(NodeToString(el.Value.(*CandidateNode)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.Debug(">>")
|
|
|
|
|
2020-10-11 00:45:20 +00:00
|
|
|
if pathNode.PathElement.PathElementType == SelfReference {
|
|
|
|
return matchingNodes, nil
|
2020-10-16 01:29:26 +00:00
|
|
|
} else if pathNode.PathElement.PathElementType == PathKey {
|
2020-10-08 23:59:03 +00:00
|
|
|
return d.traverse(matchingNodes, pathNode.PathElement)
|
2020-10-16 01:47:31 +00:00
|
|
|
} else if pathNode.PathElement.PathElementType == Value {
|
|
|
|
return nodeToMap(BuildCandidateNodeFrom(pathNode.PathElement)), nil
|
2020-10-08 23:59:03 +00:00
|
|
|
} else {
|
2020-10-11 23:44:33 +00:00
|
|
|
handler := pathNode.PathElement.OperationType.Handler
|
2020-10-10 04:00:39 +00:00
|
|
|
if handler != nil {
|
|
|
|
return handler(d, matchingNodes, pathNode)
|
2020-10-08 23:59:03 +00:00
|
|
|
}
|
2020-10-11 00:45:20 +00:00
|
|
|
return nil, fmt.Errorf("Unknown operator %v", pathNode.PathElement.OperationType)
|
2020-10-08 23:59:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|