yq/pkg/yqlib/operator_variables.go

98 lines
3.0 KiB
Go
Raw Permalink Normal View History

2021-02-03 04:51:26 +00:00
package yqlib
import (
"container/list"
"fmt"
)
2024-01-11 02:17:34 +00:00
func getVariableOperator(_ *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
2021-02-03 04:51:26 +00:00
variableName := expressionNode.Operation.StringValue
log.Debug("getVariableOperator %v", variableName)
result := context.GetVariable(variableName)
if result == nil {
result = list.New()
}
return context.ChildContext(result), nil
}
2021-09-12 06:55:55 +00:00
type assignVarPreferences struct {
IsReference bool
}
2024-01-11 02:17:34 +00:00
func useWithPipe(_ *dataTreeNavigator, _ Context, _ *ExpressionNode) (Context, error) {
return Context{}, fmt.Errorf("must use variable with a pipe, e.g. `exp as $x | ...`")
}
// variables are like loops in jq
// https://stedolan.github.io/jq/manual/#Variable
func variableLoop(d *dataTreeNavigator, context Context, originalExp *ExpressionNode) (Context, error) {
log.Debug("variable loop!")
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 variableLoopSingleChild(d, context, originalExp)
}
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
result, err := variableLoopSingleChild(d, context.SingleChildContext(el.Value.(*CandidateNode)), originalExp)
if err != nil {
return Context{}, err
}
results.PushBackList(result.MatchingNodes)
}
return context.ChildContext(results), nil
}
func variableLoopSingleChild(d *dataTreeNavigator, context Context, originalExp *ExpressionNode) (Context, error) {
variableExp := originalExp.LHS
lhs, err := d.GetMatchingNodes(context.ReadOnlyClone(), variableExp.LHS)
2021-02-03 04:51:26 +00:00
if err != nil {
2022-02-09 00:47:21 +00:00
return Context{}, err
2021-02-03 04:51:26 +00:00
}
if variableExp.RHS.Operation.OperationType.Type != "GET_VARIABLE" {
2021-02-03 04:51:26 +00:00
return Context{}, fmt.Errorf("RHS of 'as' operator must be a variable name e.g. $foo")
}
variableName := variableExp.RHS.Operation.StringValue
prefs := variableExp.Operation.Preferences.(assignVarPreferences)
2021-09-12 06:55:55 +00:00
results := list.New()
2021-09-12 06:55:55 +00:00
// now we loop over lhs, set variable to each result and calculate originalExp.Rhs
for el := lhs.MatchingNodes.Front(); el != nil; el = el.Next() {
log.Debug("PROCESSING VARIABLE: ", NodeToString(el.Value.(*CandidateNode)))
var variableValue = list.New()
if prefs.IsReference {
variableValue.PushBack(el.Value)
} else {
candidateCopy := el.Value.(*CandidateNode).Copy()
variableValue.PushBack(candidateCopy)
}
newContext := context.ChildContext(context.MatchingNodes)
newContext.SetVariable(variableName, variableValue)
rhs, err := d.GetMatchingNodes(newContext, originalExp.RHS)
2023-03-15 02:57:47 +00:00
if err != nil {
return Context{}, err
}
2023-03-15 02:57:47 +00:00
log.Debug("PROCESSING VARIABLE DONE, got back: ", rhs.MatchingNodes.Len())
results.PushBackList(rhs.MatchingNodes)
2021-09-12 06:55:55 +00:00
}
// if there is no LHS - then I guess we just calculate originalExp.Rhs
if lhs.MatchingNodes.Len() == 0 {
return d.GetMatchingNodes(context, originalExp.RHS)
}
return context.ChildContext(results), nil
2021-02-03 04:51:26 +00:00
}