yq/pkg/yqlib/operator_assign.go

71 lines
1.7 KiB
Go
Raw Normal View History

2020-11-03 23:48:43 +00:00
package yqlib
2020-10-17 11:39:01 +00:00
2020-10-21 01:54:58 +00:00
import "container/list"
2020-10-17 11:39:01 +00:00
2020-11-19 06:08:13 +00:00
type AssignOpPreferences struct {
UpdateAssign bool
}
2020-11-02 00:20:38 +00:00
func AssignUpdateOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode *PathTreeNode) (*list.List, error) {
2020-10-27 05:45:16 +00:00
lhs, err := d.GetMatchingNodes(matchingNodes, pathNode.Lhs)
2020-10-17 11:39:01 +00:00
if err != nil {
return nil, err
}
2020-11-19 06:08:13 +00:00
preferences := pathNode.Operation.Preferences.(*AssignOpPreferences)
var rhs *list.List
if !preferences.UpdateAssign {
rhs, err = d.GetMatchingNodes(matchingNodes, pathNode.Rhs)
}
2020-10-17 11:39:01 +00:00
for el := lhs.Front(); el != nil; el = el.Next() {
candidate := el.Value.(*CandidateNode)
2020-11-19 06:08:13 +00:00
if preferences.UpdateAssign {
rhs, err = d.GetMatchingNodes(nodeToMap(candidate), pathNode.Rhs)
}
2020-10-17 11:39:01 +00:00
if err != nil {
return nil, err
}
// grab the first value
first := rhs.Front()
if first != nil {
candidate.UpdateFrom(first.Value.(*CandidateNode))
}
}
2020-12-01 03:06:49 +00:00
// // if there was nothing given, perhaps we are creating a new yaml doc
// if matchingNodes.Len() == 0 {
// log.Debug("started with nothing, returning LHS, %v", lhs.Len())
// return lhs, nil
// }
2020-10-17 11:39:01 +00:00
return matchingNodes, nil
}
2020-10-19 05:14:29 +00:00
// does not update content or values
2020-10-21 01:54:58 +00:00
func AssignAttributesOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode *PathTreeNode) (*list.List, error) {
2020-10-27 05:45:16 +00:00
lhs, err := d.GetMatchingNodes(matchingNodes, pathNode.Lhs)
2020-10-19 05:14:29 +00:00
if err != nil {
return nil, err
}
for el := lhs.Front(); el != nil; el = el.Next() {
candidate := el.Value.(*CandidateNode)
2020-10-27 05:45:16 +00:00
rhs, err := d.GetMatchingNodes(nodeToMap(candidate), pathNode.Rhs)
2020-10-19 05:14:29 +00:00
if err != nil {
return nil, err
}
// grab the first value
first := rhs.Front()
if first != nil {
candidate.UpdateAttributesFrom(first.Value.(*CandidateNode))
}
}
return matchingNodes, nil
}