yq/pkg/yqlib/operator_assign.go

104 lines
3.0 KiB
Go
Raw Permalink Normal View History

2020-11-03 23:48:43 +00:00
package yqlib
2020-10-17 11:39:01 +00:00
type assignPreferences struct {
DontOverWriteAnchor bool
OnlyWriteNull bool
ClobberCustomTags bool
}
func assignUpdateFunc(prefs assignPreferences) crossFunctionCalculation {
return func(_ *dataTreeNavigator, _ Context, lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error) {
if !prefs.OnlyWriteNull || lhs.Tag == "!!null" {
lhs.UpdateFrom(rhs, prefs)
}
return lhs, nil
}
2021-07-07 09:22:51 +00:00
}
// they way *= (multipleAssign) is handled, we set the multiplePrefs
// on the node, not assignPrefs. Long story.
func getAssignPreferences(preferences interface{}) assignPreferences {
prefs := assignPreferences{}
switch typedPref := preferences.(type) {
case assignPreferences:
prefs = typedPref
case multiplyPreferences:
prefs = typedPref.AssignPrefs
}
return prefs
}
func assignUpdateOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
lhs, err := d.GetMatchingNodes(context, expressionNode.LHS)
2020-10-17 11:39:01 +00:00
if err != nil {
return Context{}, err
2020-10-17 11:39:01 +00:00
}
2021-07-07 09:22:51 +00:00
prefs := getAssignPreferences(expressionNode.Operation.Preferences)
log.Debug("assignUpdateOperator prefs: %v", prefs)
2021-01-12 23:18:53 +00:00
if !expressionNode.Operation.UpdateAssign {
2021-07-07 09:22:51 +00:00
// this works because we already ran against LHS with an editable context.
_, err := crossFunction(d, context.ReadOnlyClone(), expressionNode, assignUpdateFunc(prefs), false)
2021-07-07 09:22:51 +00:00
return context, err
2020-11-19 06:08:13 +00:00
}
//traverse backwards through the context -
// like delete, we need to run against the children first.
// (e.g. consider when running with expression '.. |= [.]' - we need
// to wrap the children first
for el := lhs.MatchingNodes.Back(); el != nil; el = el.Prev() {
2020-10-17 11:39:01 +00:00
candidate := el.Value.(*CandidateNode)
rhs, err := d.GetMatchingNodes(context.SingleChildContext(candidate), expressionNode.RHS)
2020-10-17 11:39:01 +00:00
if err != nil {
return Context{}, err
2020-10-17 11:39:01 +00:00
}
// grab the first value
first := rhs.MatchingNodes.Front()
2020-10-17 11:39:01 +00:00
if first != nil {
2021-01-01 23:27:32 +00:00
rhsCandidate := first.Value.(*CandidateNode)
candidate.UpdateFrom(rhsCandidate, prefs)
2020-10-17 11:39:01 +00:00
}
}
return context, nil
2020-10-17 11:39:01 +00:00
}
2020-10-19 05:14:29 +00:00
// does not update content or values
func assignAttributesOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
2021-02-08 02:58:46 +00:00
log.Debug("getting lhs matching nodes for update")
lhs, err := d.GetMatchingNodes(context, expressionNode.LHS)
2020-10-19 05:14:29 +00:00
if err != nil {
return Context{}, err
2020-10-19 05:14:29 +00:00
}
for el := lhs.MatchingNodes.Front(); el != nil; el = el.Next() {
2020-10-19 05:14:29 +00:00
candidate := el.Value.(*CandidateNode)
rhs, err := d.GetMatchingNodes(context.SingleReadonlyChildContext(candidate), expressionNode.RHS)
2020-10-19 05:14:29 +00:00
if err != nil {
return Context{}, err
2020-10-19 05:14:29 +00:00
}
// grab the first value
first := rhs.MatchingNodes.Front()
2020-10-19 05:14:29 +00:00
if first != nil {
prefs := assignPreferences{}
if expressionNode.Operation.Preferences != nil {
prefs = expressionNode.Operation.Preferences.(assignPreferences)
}
if !prefs.OnlyWriteNull || candidate.Tag == "!!null" {
candidate.UpdateAttributesFrom(first.Value.(*CandidateNode), prefs)
}
2020-10-19 05:14:29 +00:00
}
}
return context, nil
2020-10-19 05:14:29 +00:00
}