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
|
|
|
|
2021-02-08 02:58:46 +00:00
|
|
|
"github.com/jinzhu/copier"
|
2022-08-29 05:37:25 +00:00
|
|
|
logging "gopkg.in/op/go-logging.v1"
|
2020-10-11 00:45:20 +00:00
|
|
|
)
|
2020-10-10 04:00:39 +00:00
|
|
|
|
2021-02-02 07:17:59 +00:00
|
|
|
type operatorHandler func(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error)
|
2020-10-10 04:00:39 +00:00
|
|
|
|
2021-07-07 10:00:46 +00:00
|
|
|
type compoundCalculation func(lhs *ExpressionNode, rhs *ExpressionNode) *ExpressionNode
|
|
|
|
|
|
|
|
func compoundAssignFunction(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode, calculation compoundCalculation) (Context, error) {
|
2022-02-07 00:55:55 +00:00
|
|
|
lhs, err := d.GetMatchingNodes(context, expressionNode.LHS)
|
2021-07-07 10:00:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return Context{}, err
|
|
|
|
}
|
|
|
|
|
2022-09-09 02:28:38 +00:00
|
|
|
// tricky logic when we are running *= with flags.
|
|
|
|
// we have an op like: .a *=nc .b
|
|
|
|
// which should roughly translate to .a =c .a *nc .b
|
|
|
|
// note that the 'n' flag only applies to the multiple op, not the assignment
|
|
|
|
// but the clobber flag applies to both!
|
|
|
|
|
|
|
|
prefs := assignPreferences{}
|
|
|
|
switch typedPref := expressionNode.Operation.Preferences.(type) {
|
|
|
|
case assignPreferences:
|
|
|
|
prefs = typedPref
|
|
|
|
case multiplyPreferences:
|
|
|
|
prefs.ClobberCustomTags = typedPref.AssignPrefs.ClobberCustomTags
|
|
|
|
}
|
|
|
|
|
|
|
|
assignmentOp := &Operation{OperationType: assignOpType, Preferences: prefs}
|
2021-07-07 10:00:46 +00:00
|
|
|
|
|
|
|
for el := lhs.MatchingNodes.Front(); el != nil; el = el.Next() {
|
|
|
|
candidate := el.Value.(*CandidateNode)
|
2023-04-11 02:39:22 +00:00
|
|
|
clone := candidate.Copy()
|
2023-01-12 04:11:45 +00:00
|
|
|
valueCopyExp := &ExpressionNode{Operation: &Operation{OperationType: referenceOpType, CandidateNode: clone}}
|
2022-03-19 07:42:12 +00:00
|
|
|
|
2023-01-12 04:11:45 +00:00
|
|
|
valueExpression := &ExpressionNode{Operation: &Operation{OperationType: referenceOpType, CandidateNode: candidate}}
|
2021-07-07 10:00:46 +00:00
|
|
|
|
2022-03-19 07:42:12 +00:00
|
|
|
assignmentOpNode := &ExpressionNode{Operation: assignmentOp, LHS: valueExpression, RHS: calculation(valueCopyExp, expressionNode.RHS)}
|
2021-07-07 10:00:46 +00:00
|
|
|
|
|
|
|
_, err = d.GetMatchingNodes(context, assignmentOpNode)
|
|
|
|
if err != nil {
|
|
|
|
return Context{}, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return context, nil
|
|
|
|
}
|
|
|
|
|
2021-02-02 07:17:59 +00:00
|
|
|
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)
|
|
|
|
|
2022-03-20 01:55:58 +00:00
|
|
|
func resultsForRHS(d *dataTreeNavigator, context Context, lhsCandidate *CandidateNode, prefs crossFunctionPreferences, rhsExp *ExpressionNode, results *list.List) error {
|
2021-05-16 02:16:10 +00:00
|
|
|
|
2022-03-20 01:55:58 +00:00
|
|
|
if prefs.LhsResultValue != nil {
|
|
|
|
result, err := prefs.LhsResultValue(lhsCandidate)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if result != nil {
|
|
|
|
results.PushBack(result)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rhs, err := d.GetMatchingNodes(context, rhsExp)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if prefs.CalcWhenEmpty && rhs.MatchingNodes.Len() == 0 {
|
|
|
|
resultCandidate, err := prefs.Calculation(d, context, lhsCandidate, nil)
|
2021-05-16 02:16:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-05-28 06:59:02 +00:00
|
|
|
if resultCandidate != nil {
|
|
|
|
results.PushBack(resultCandidate)
|
|
|
|
}
|
2021-05-16 02:16:10 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-13 00:42:20 +00:00
|
|
|
for rightEl := rhs.MatchingNodes.Front(); rightEl != nil; rightEl = rightEl.Next() {
|
|
|
|
rhsCandidate := rightEl.Value.(*CandidateNode)
|
2022-08-29 05:37:25 +00:00
|
|
|
if !log.IsEnabledFor(logging.DEBUG) {
|
|
|
|
log.Debugf("Applying lhs: %v, rhsCandidate, %v", NodeToString(lhsCandidate), NodeToString(rhsCandidate))
|
|
|
|
}
|
2022-03-20 01:55:58 +00:00
|
|
|
resultCandidate, err := prefs.Calculation(d, context, lhsCandidate, rhsCandidate)
|
2021-04-13 00:42:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-05-28 06:59:02 +00:00
|
|
|
if resultCandidate != nil {
|
|
|
|
results.PushBack(resultCandidate)
|
|
|
|
}
|
2021-04-13 00:42:20 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-20 01:55:58 +00:00
|
|
|
type crossFunctionPreferences struct {
|
|
|
|
CalcWhenEmpty bool
|
|
|
|
// if this returns a result node,
|
|
|
|
// we wont bother calculating the RHS
|
|
|
|
LhsResultValue func(*CandidateNode) (*CandidateNode, error)
|
|
|
|
Calculation crossFunctionCalculation
|
|
|
|
}
|
|
|
|
|
|
|
|
func doCrossFunc(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode, prefs crossFunctionPreferences) (Context, error) {
|
2021-02-02 07:17:59 +00:00
|
|
|
var results = list.New()
|
2022-02-07 00:55:55 +00:00
|
|
|
lhs, err := d.GetMatchingNodes(context, expressionNode.LHS)
|
2021-02-02 07:17:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return Context{}, err
|
|
|
|
}
|
|
|
|
log.Debugf("crossFunction LHS len: %v", lhs.MatchingNodes.Len())
|
|
|
|
|
2022-03-20 01:55:58 +00:00
|
|
|
if prefs.CalcWhenEmpty && lhs.MatchingNodes.Len() == 0 {
|
|
|
|
err := resultsForRHS(d, context, nil, prefs, expressionNode.RHS, results)
|
2021-04-13 00:42:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return Context{}, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for el := lhs.MatchingNodes.Front(); el != nil; el = el.Next() {
|
|
|
|
lhsCandidate := el.Value.(*CandidateNode)
|
|
|
|
|
2022-03-20 01:55:58 +00:00
|
|
|
err = resultsForRHS(d, context, lhsCandidate, prefs, expressionNode.RHS, results)
|
2021-04-13 00:42:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return Context{}, err
|
|
|
|
}
|
2021-02-02 07:17:59 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
return context.ChildContext(results), nil
|
|
|
|
}
|
|
|
|
|
2021-04-13 00:42:20 +00:00
|
|
|
func crossFunction(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode, calculation crossFunctionCalculation, calcWhenEmpty bool) (Context, error) {
|
2022-03-20 01:55:58 +00:00
|
|
|
prefs := crossFunctionPreferences{CalcWhenEmpty: calcWhenEmpty, Calculation: calculation}
|
|
|
|
return crossFunctionWithPrefs(d, context, expressionNode, prefs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func crossFunctionWithPrefs(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode, prefs crossFunctionPreferences) (Context, error) {
|
2021-02-02 07:17:59 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2022-03-20 01:55:58 +00:00
|
|
|
|
2021-02-02 07:17:59 +00:00
|
|
|
if evaluateAllTogether {
|
2021-07-07 09:22:51 +00:00
|
|
|
log.Debug("crossFunction evaluateAllTogether!")
|
2022-03-20 01:55:58 +00:00
|
|
|
return doCrossFunc(d, context, expressionNode, prefs)
|
2021-02-02 07:17:59 +00:00
|
|
|
}
|
|
|
|
|
2021-07-07 09:22:51 +00:00
|
|
|
log.Debug("crossFunction evaluate apart!")
|
|
|
|
|
2021-02-02 07:17:59 +00:00
|
|
|
for matchEl := context.MatchingNodes.Front(); matchEl != nil; matchEl = matchEl.Next() {
|
2022-03-20 01:55:58 +00:00
|
|
|
innerResults, err := doCrossFunc(d, context.SingleChildContext(matchEl.Value.(*CandidateNode)), expressionNode, prefs)
|
2021-02-02 07:17:59 +00:00
|
|
|
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"
|
|
|
|
}
|
2023-05-02 04:07:59 +00:00
|
|
|
noob := owner.CreateReplacement(ScalarNode, "!!bool", valString)
|
|
|
|
if owner.IsMapKey {
|
|
|
|
noob.IsMapKey = false
|
|
|
|
noob.Key = owner
|
|
|
|
}
|
|
|
|
|
|
|
|
return noob
|
2020-10-17 11:10:47 +00:00
|
|
|
}
|
|
|
|
|
2021-02-08 02:58:46 +00:00
|
|
|
func createTraversalTree(path []interface{}, traversePrefs traversePreferences, targetKey bool) *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-02-08 02:58:46 +00:00
|
|
|
lastPrefs := traversePrefs
|
|
|
|
if targetKey {
|
|
|
|
err := copier.Copy(&lastPrefs, traversePrefs)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
lastPrefs.IncludeMapKeys = true
|
|
|
|
lastPrefs.DontIncludeMapValues = true
|
|
|
|
}
|
|
|
|
return &ExpressionNode{Operation: &Operation{OperationType: traversePathOpType, Preferences: lastPrefs, 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{
|
2021-01-11 06:13:48 +00:00
|
|
|
Operation: &Operation{OperationType: shortPipeOpType},
|
2022-02-07 00:55:55 +00:00
|
|
|
LHS: createTraversalTree(path[0:1], traversePrefs, false),
|
|
|
|
RHS: createTraversalTree(path[1:], traversePrefs, targetKey),
|
2021-01-13 05:54:28 +00:00
|
|
|
}
|
2020-12-22 00:45:51 +00:00
|
|
|
}
|