yq/pkg/yqlib/operator_assign.go

62 lines
1.6 KiB
Go
Raw Normal View History

2020-11-03 23:48:43 +00:00
package yqlib
2020-10-17 11:39:01 +00:00
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
}
var rhs Context
2021-01-12 23:18:53 +00:00
if !expressionNode.Operation.UpdateAssign {
rhs, err = d.GetMatchingNodes(context.ReadOnlyClone(), expressionNode.Rhs)
2020-11-19 06:08:13 +00:00
}
for el := lhs.MatchingNodes.Front(); el != nil; el = el.Next() {
2020-10-17 11:39:01 +00:00
candidate := el.Value.(*CandidateNode)
2021-01-12 23:18:53 +00:00
if expressionNode.Operation.UpdateAssign {
rhs, err = d.GetMatchingNodes(context.SingleChildContext(candidate), expressionNode.Rhs)
2020-11-19 06:08:13 +00:00
}
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)
2021-01-12 23:00:51 +00:00
rhsCandidate.Node = unwrapDoc(rhsCandidate.Node)
2021-01-01 23:27:32 +00:00
candidate.UpdateFrom(rhsCandidate)
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 {
candidate.UpdateAttributesFrom(first.Value.(*CandidateNode))
}
}
return context, nil
2020-10-19 05:14:29 +00:00
}