2020-10-10 04:00:39 +00:00
|
|
|
package treeops
|
|
|
|
|
2020-10-11 00:45:20 +00:00
|
|
|
import (
|
2020-10-12 01:24:59 +00:00
|
|
|
"fmt"
|
|
|
|
|
2020-10-11 00:45:20 +00:00
|
|
|
"github.com/elliotchance/orderedmap"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
)
|
2020-10-10 04:00:39 +00:00
|
|
|
|
|
|
|
type OperatorHandler func(d *dataTreeNavigator, matchingNodes *orderedmap.OrderedMap, pathNode *PathTreeNode) (*orderedmap.OrderedMap, error)
|
|
|
|
|
2020-10-16 01:29:26 +00:00
|
|
|
func PipeOperator(d *dataTreeNavigator, matchingNodes *orderedmap.OrderedMap, pathNode *PathTreeNode) (*orderedmap.OrderedMap, error) {
|
2020-10-10 04:00:39 +00:00
|
|
|
lhs, err := d.getMatchingNodes(matchingNodes, pathNode.Lhs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return d.getMatchingNodes(lhs, pathNode.Rhs)
|
|
|
|
}
|
|
|
|
|
2020-10-17 11:10:47 +00:00
|
|
|
func createBooleanCandidate(owner *CandidateNode, value bool) *CandidateNode {
|
|
|
|
valString := "true"
|
|
|
|
if !value {
|
|
|
|
valString = "false"
|
|
|
|
}
|
|
|
|
node := &yaml.Node{Kind: yaml.ScalarNode, Value: valString, Tag: "!!bool"}
|
|
|
|
return &CandidateNode{Node: node, Document: owner.Document, Path: owner.Path}
|
|
|
|
}
|
|
|
|
|
2020-10-16 01:29:26 +00:00
|
|
|
func nodeToMap(candidate *CandidateNode) *orderedmap.OrderedMap {
|
|
|
|
elMap := orderedmap.NewOrderedMap()
|
|
|
|
elMap.Set(candidate.GetKey(), candidate)
|
|
|
|
return elMap
|
|
|
|
}
|
|
|
|
|
2020-10-10 04:00:39 +00:00
|
|
|
func IntersectionOperator(d *dataTreeNavigator, matchingNodes *orderedmap.OrderedMap, pathNode *PathTreeNode) (*orderedmap.OrderedMap, error) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return matchingNodeMap, nil
|
|
|
|
}
|
|
|
|
|
2020-10-16 01:29:26 +00:00
|
|
|
func LengthOperator(d *dataTreeNavigator, matchMap *orderedmap.OrderedMap, pathNode *PathTreeNode) (*orderedmap.OrderedMap, error) {
|
|
|
|
log.Debugf("-- lengthOperation")
|
2020-10-12 01:24:59 +00:00
|
|
|
var results = orderedmap.NewOrderedMap()
|
|
|
|
|
|
|
|
for el := matchMap.Front(); el != nil; el = el.Next() {
|
|
|
|
candidate := el.Value.(*CandidateNode)
|
2020-10-16 01:29:26 +00:00
|
|
|
var length int
|
|
|
|
switch candidate.Node.Kind {
|
|
|
|
case yaml.ScalarNode:
|
|
|
|
length = len(candidate.Node.Value)
|
|
|
|
case yaml.MappingNode:
|
|
|
|
length = len(candidate.Node.Content) / 2
|
|
|
|
case yaml.SequenceNode:
|
|
|
|
length = len(candidate.Node.Content)
|
|
|
|
default:
|
|
|
|
length = 0
|
2020-10-12 01:24:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
node := &yaml.Node{Kind: yaml.ScalarNode, Value: fmt.Sprintf("%v", length), Tag: "!!int"}
|
|
|
|
lengthCand := &CandidateNode{Node: node, Document: candidate.Document, Path: candidate.Path}
|
2020-10-13 01:51:37 +00:00
|
|
|
results.Set(candidate.GetKey(), lengthCand)
|
2020-10-12 01:24:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return results, nil
|
|
|
|
}
|