2020-11-03 23:48:43 +00:00
|
|
|
package yqlib
|
2020-10-17 11:39:01 +00:00
|
|
|
|
2021-12-02 22:23:16 +00:00
|
|
|
type assignPreferences struct {
|
|
|
|
DontOverWriteAnchor bool
|
2022-01-15 04:48:34 +00:00
|
|
|
OnlyWriteNull bool
|
2021-12-02 22:23:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func assignUpdateFunc(prefs assignPreferences) crossFunctionCalculation {
|
|
|
|
return func(d *dataTreeNavigator, context Context, lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error) {
|
|
|
|
rhs.Node = unwrapDoc(rhs.Node)
|
2022-01-15 04:48:34 +00:00
|
|
|
if !prefs.OnlyWriteNull || lhs.Node.Tag == "!!null" {
|
|
|
|
lhs.UpdateFrom(rhs, prefs)
|
|
|
|
}
|
2021-12-02 22:23:16 +00:00
|
|
|
return lhs, nil
|
|
|
|
}
|
2021-07-07 09:22:51 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 07:17:59 +00:00
|
|
|
func assignUpdateOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
|
2022-02-07 00:55:55 +00:00
|
|
|
lhs, err := d.GetMatchingNodes(context, expressionNode.LHS)
|
2020-10-17 11:39:01 +00:00
|
|
|
if err != nil {
|
2021-02-02 07:17:59 +00:00
|
|
|
return Context{}, err
|
2020-10-17 11:39:01 +00:00
|
|
|
}
|
2021-07-07 09:22:51 +00:00
|
|
|
|
2021-12-02 22:23:16 +00:00
|
|
|
prefs := assignPreferences{}
|
2022-06-25 02:14:11 +00:00
|
|
|
// they way *= (multipleAssign) is handled, we set the multiplePrefs
|
|
|
|
// on the node, not assignPrefs. Long story.
|
2022-06-25 02:08:52 +00:00
|
|
|
if p, ok := expressionNode.Operation.Preferences.(assignPreferences); ok {
|
|
|
|
prefs = p
|
2021-12-02 22:23:16 +00:00
|
|
|
}
|
|
|
|
|
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.
|
2021-12-02 22:23:16 +00:00
|
|
|
_, 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
|
|
|
}
|
|
|
|
|
2021-02-02 07:17:59 +00:00
|
|
|
for el := lhs.MatchingNodes.Front(); el != nil; el = el.Next() {
|
2020-10-17 11:39:01 +00:00
|
|
|
candidate := el.Value.(*CandidateNode)
|
|
|
|
|
2022-02-07 00:55:55 +00:00
|
|
|
rhs, err := d.GetMatchingNodes(context.SingleChildContext(candidate), expressionNode.RHS)
|
2020-10-17 11:39:01 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2021-02-02 07:17:59 +00:00
|
|
|
return Context{}, err
|
2020-10-17 11:39:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// grab the first value
|
2021-02-02 07:17:59 +00:00
|
|
|
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-12-02 22:23:16 +00:00
|
|
|
candidate.UpdateFrom(rhsCandidate, prefs)
|
2020-10-17 11:39:01 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-11 22:40:37 +00:00
|
|
|
|
2021-02-02 07:17:59 +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
|
2021-02-02 07:17:59 +00:00
|
|
|
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")
|
2022-02-07 00:55:55 +00:00
|
|
|
lhs, err := d.GetMatchingNodes(context, expressionNode.LHS)
|
2020-10-19 05:14:29 +00:00
|
|
|
if err != nil {
|
2021-02-02 07:17:59 +00:00
|
|
|
return Context{}, err
|
2020-10-19 05:14:29 +00:00
|
|
|
}
|
2021-02-02 07:17:59 +00:00
|
|
|
for el := lhs.MatchingNodes.Front(); el != nil; el = el.Next() {
|
2020-10-19 05:14:29 +00:00
|
|
|
candidate := el.Value.(*CandidateNode)
|
|
|
|
|
2022-02-07 00:55:55 +00:00
|
|
|
rhs, err := d.GetMatchingNodes(context.SingleReadonlyChildContext(candidate), expressionNode.RHS)
|
2020-10-19 05:14:29 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2021-02-02 07:17:59 +00:00
|
|
|
return Context{}, err
|
2020-10-19 05:14:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// grab the first value
|
2021-02-02 07:17:59 +00:00
|
|
|
first := rhs.MatchingNodes.Front()
|
2020-10-19 05:14:29 +00:00
|
|
|
|
|
|
|
if first != nil {
|
2021-12-02 22:23:16 +00:00
|
|
|
prefs := assignPreferences{}
|
|
|
|
if expressionNode.Operation.Preferences != nil {
|
|
|
|
prefs = expressionNode.Operation.Preferences.(assignPreferences)
|
|
|
|
}
|
2022-01-15 04:48:34 +00:00
|
|
|
if !prefs.OnlyWriteNull || candidate.Node.Tag == "!!null" {
|
|
|
|
candidate.UpdateAttributesFrom(first.Value.(*CandidateNode), prefs)
|
|
|
|
}
|
2020-10-19 05:14:29 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-02 07:17:59 +00:00
|
|
|
return context, nil
|
2020-10-19 05:14:29 +00:00
|
|
|
}
|