yq/pkg/yqlib/operators.go

104 lines
3.2 KiB
Go
Raw Normal View History

2020-11-03 23:48:43 +00:00
package yqlib
2020-10-10 04:00:39 +00:00
2020-10-11 00:45:20 +00:00
import (
2020-10-21 01:54:58 +00:00
"container/list"
2020-12-22 00:45:51 +00:00
"fmt"
2020-10-12 01:24:59 +00:00
2020-10-11 00:45:20 +00:00
"gopkg.in/yaml.v3"
)
2020-10-10 04:00:39 +00:00
type operatorHandler func(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error)
2020-10-10 04:00:39 +00:00
2021-01-12 23:00:51 +00:00
func unwrapDoc(node *yaml.Node) *yaml.Node {
2020-10-27 05:45:16 +00:00
if node.Kind == yaml.DocumentNode {
return node.Content[0]
}
return node
}
func emptyOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
context.MatchingNodes = list.New()
return context, nil
}
type crossFunctionCalculation func(d *dataTreeNavigator, context Context, lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error)
func doCrossFunc(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode, calculation crossFunctionCalculation) (Context, error) {
var results = list.New()
lhs, err := d.GetMatchingNodes(context, expressionNode.Lhs)
if err != nil {
return Context{}, err
}
log.Debugf("crossFunction LHS len: %v", lhs.MatchingNodes.Len())
rhs, err := d.GetMatchingNodes(context, expressionNode.Rhs)
if err != nil {
return Context{}, err
}
for el := lhs.MatchingNodes.Front(); el != nil; el = el.Next() {
lhsCandidate := el.Value.(*CandidateNode)
for rightEl := rhs.MatchingNodes.Front(); rightEl != nil; rightEl = rightEl.Next() {
log.Debugf("Applying calc")
rhsCandidate := rightEl.Value.(*CandidateNode)
resultCandidate, err := calculation(d, context, lhsCandidate, rhsCandidate)
if err != nil {
return Context{}, err
}
results.PushBack(resultCandidate)
}
}
return context.ChildContext(results), nil
}
func crossFunction(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode, calculation crossFunctionCalculation) (Context, error) {
var results = list.New()
var evaluateAllTogether = true
for matchEl := context.MatchingNodes.Front(); matchEl != nil; matchEl = matchEl.Next() {
evaluateAllTogether = evaluateAllTogether && matchEl.Value.(*CandidateNode).EvaluateTogether
if !evaluateAllTogether {
break
}
}
if evaluateAllTogether {
return doCrossFunc(d, context, expressionNode, calculation)
}
for matchEl := context.MatchingNodes.Front(); matchEl != nil; matchEl = matchEl.Next() {
innerResults, err := doCrossFunc(d, context.SingleChildContext(matchEl.Value.(*CandidateNode)), expressionNode, calculation)
if err != nil {
return Context{}, err
}
results.PushBackList(innerResults.MatchingNodes)
}
return context.ChildContext(results), nil
2020-11-13 02:19:54 +00:00
}
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 owner.CreateChild(nil, node)
2020-10-17 11:10:47 +00:00
}
2021-01-13 05:54:28 +00:00
func createTraversalTree(path []interface{}, traversePrefs traversePreferences) *ExpressionNode {
2020-12-22 00:45:51 +00:00
if len(path) == 0 {
2021-01-12 23:18:53 +00:00
return &ExpressionNode{Operation: &Operation{OperationType: selfReferenceOpType}}
2020-12-22 00:45:51 +00:00
} else if len(path) == 1 {
2021-01-13 05:54:28 +00:00
return &ExpressionNode{Operation: &Operation{OperationType: traversePathOpType, Preferences: traversePrefs, Value: path[0], StringValue: fmt.Sprintf("%v", path[0])}}
2020-12-22 00:45:51 +00:00
}
2021-01-13 05:54:28 +00:00
2021-01-12 23:18:53 +00:00
return &ExpressionNode{
Operation: &Operation{OperationType: shortPipeOpType},
2021-01-13 05:54:28 +00:00
Lhs: createTraversalTree(path[0:1], traversePrefs),
Rhs: createTraversalTree(path[1:], traversePrefs),
}
2020-12-22 00:45:51 +00:00
}