yq/pkg/yqlib/operator_variables.go

43 lines
1.2 KiB
Go
Raw Normal View History

2021-02-03 04:51:26 +00:00
package yqlib
import (
"container/list"
"fmt"
)
func getVariableOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
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
}
2021-02-03 04:51:26 +00:00
func assignVariableOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
lhs, err := d.GetMatchingNodes(context.ReadOnlyClone(), expressionNode.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 expressionNode.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 := expressionNode.RHS.Operation.StringValue
2021-09-12 06:55:55 +00:00
prefs := expressionNode.Operation.Preferences.(assignVarPreferences)
var variableValue *list.List
if prefs.IsReference {
variableValue = lhs.MatchingNodes
} else {
variableValue = lhs.DeepClone().MatchingNodes
}
context.SetVariable(variableName, variableValue)
2021-02-03 04:51:26 +00:00
return context, nil
}