yq/pkg/yqlib/treeops/operators.go

167 lines
5.0 KiB
Go
Raw Normal View History

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)
func TraverseOperator(d *dataTreeNavigator, matchingNodes *orderedmap.OrderedMap, pathNode *PathTreeNode) (*orderedmap.OrderedMap, error) {
lhs, err := d.getMatchingNodes(matchingNodes, pathNode.Lhs)
if err != nil {
return nil, err
}
return d.getMatchingNodes(lhs, pathNode.Rhs)
}
2020-10-10 04:24:37 +00:00
func AssignOperator(d *dataTreeNavigator, matchingNodes *orderedmap.OrderedMap, pathNode *PathTreeNode) (*orderedmap.OrderedMap, error) {
lhs, err := d.getMatchingNodes(matchingNodes, pathNode.Lhs)
if err != nil {
return nil, err
}
for el := lhs.Front(); el != nil; el = el.Next() {
node := el.Value.(*CandidateNode)
2020-10-10 12:04:10 +00:00
log.Debugf("Assiging %v to %v", node.getKey(), pathNode.Rhs.PathElement.StringValue)
2020-10-10 04:24:37 +00:00
node.Node.Value = pathNode.Rhs.PathElement.StringValue
}
return lhs, nil
}
2020-10-10 04:00:39 +00:00
func UnionOperator(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
}
for el := rhs.Front(); el != nil; el = el.Next() {
node := el.Value.(*CandidateNode)
lhs.Set(node.getKey(), node)
}
return lhs, nil
}
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-10 11:42:09 +00:00
func splatNode(d *dataTreeNavigator, candidate *CandidateNode) (*orderedmap.OrderedMap, error) {
elMap := orderedmap.NewOrderedMap()
elMap.Set(candidate.getKey(), candidate)
//need to splat matching nodes, then search through them
splatter := &PathTreeNode{PathElement: &PathElement{
PathElementType: PathKey,
Value: "*",
StringValue: "*",
}}
return d.getMatchingNodes(elMap, splatter)
}
2020-10-10 04:00:39 +00:00
func EqualsOperator(d *dataTreeNavigator, matchMap *orderedmap.OrderedMap, pathNode *PathTreeNode) (*orderedmap.OrderedMap, error) {
log.Debugf("-- equalsOperation")
var results = orderedmap.NewOrderedMap()
for el := matchMap.Front(); el != nil; el = el.Next() {
2020-10-11 00:24:22 +00:00
candidate := el.Value.(*CandidateNode)
valuePattern := pathNode.Rhs.PathElement.StringValue
log.Debug("checking %v", candidate)
2020-10-10 04:00:39 +00:00
2020-10-11 00:45:20 +00:00
errInChild := findMatchingChildren(d, results, candidate, pathNode.Lhs, valuePattern)
if errInChild != nil {
return nil, errInChild
2020-10-10 04:00:39 +00:00
}
}
return results, nil
}
2020-10-12 01:24:59 +00:00
func CountOperator(d *dataTreeNavigator, matchMap *orderedmap.OrderedMap, pathNode *PathTreeNode) (*orderedmap.OrderedMap, error) {
log.Debugf("-- countOperation")
var results = orderedmap.NewOrderedMap()
for el := matchMap.Front(); el != nil; el = el.Next() {
candidate := el.Value.(*CandidateNode)
elMap := orderedmap.NewOrderedMap()
elMap.Set(el.Key, el.Value)
childMatches, errChild := d.getMatchingNodes(elMap, pathNode.Rhs)
if errChild != nil {
return nil, errChild
}
length := childMatches.Len()
node := &yaml.Node{Kind: yaml.ScalarNode, Value: fmt.Sprintf("%v", length), Tag: "!!int"}
lengthCand := &CandidateNode{Node: node, Document: candidate.Document, Path: candidate.Path}
results.Set(candidate.getKey(), lengthCand)
}
return results, nil
}
2020-10-11 00:24:22 +00:00
func findMatchingChildren(d *dataTreeNavigator, results *orderedmap.OrderedMap, candidate *CandidateNode, lhs *PathTreeNode, valuePattern string) error {
2020-10-11 00:45:20 +00:00
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)
2020-10-11 00:24:22 +00:00
}
2020-10-11 00:45:20 +00:00
2020-10-11 00:24:22 +00:00
for childEl := children.Front(); childEl != nil; childEl = childEl.Next() {
childMap := orderedmap.NewOrderedMap()
childMap.Set(childEl.Key, childEl.Value)
childMatches, errChild := d.getMatchingNodes(childMap, lhs)
log.Debug("got the LHS")
if errChild != nil {
return errChild
}
if containsMatchingValue(childMatches, valuePattern) {
results.Set(childEl.Key, childEl.Value)
}
}
return nil
}
2020-10-10 04:00:39 +00:00
func containsMatchingValue(matchMap *orderedmap.OrderedMap, valuePattern string) bool {
log.Debugf("-- findMatchingValues")
for el := matchMap.Front(); el != nil; el = el.Next() {
node := el.Value.(*CandidateNode)
2020-10-11 23:44:33 +00:00
log.Debugf("-- comparing %v to %v", node.Node.Value, valuePattern)
2020-10-10 04:00:39 +00:00
if Match(node.Node.Value, valuePattern) {
return true
}
}
2020-10-11 00:24:22 +00:00
log.Debugf("-- done findMatchingValues")
2020-10-10 04:00:39 +00:00
return false
}