2020-11-24 02:07:19 +00:00
|
|
|
package yqlib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"container/list"
|
|
|
|
|
|
|
|
yaml "gopkg.in/yaml.v3"
|
|
|
|
)
|
|
|
|
|
2020-11-28 00:24:16 +00:00
|
|
|
func createSelfAddOp(rhs *PathTreeNode) *PathTreeNode {
|
|
|
|
return &PathTreeNode{Operation: &Operation{OperationType: Add},
|
|
|
|
Lhs: &PathTreeNode{Operation: &Operation{OperationType: SelfReference}},
|
|
|
|
Rhs: rhs}
|
|
|
|
}
|
|
|
|
|
|
|
|
func AddAssignOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode *PathTreeNode) (*list.List, error) {
|
|
|
|
assignmentOp := &Operation{OperationType: Assign}
|
|
|
|
assignmentOp.Preferences = &AssignOpPreferences{true}
|
|
|
|
|
|
|
|
assignmentOpNode := &PathTreeNode{Operation: assignmentOp, Lhs: pathNode.Lhs, Rhs: createSelfAddOp(pathNode.Rhs)}
|
|
|
|
return d.GetMatchingNodes(matchingNodes, assignmentOpNode)
|
2020-11-27 23:41:09 +00:00
|
|
|
}
|
|
|
|
|
2020-12-21 00:54:03 +00:00
|
|
|
func toNodes(candidate *CandidateNode) []*yaml.Node {
|
2020-11-24 02:07:19 +00:00
|
|
|
if candidate.Node.Tag == "!!null" {
|
|
|
|
return []*yaml.Node{}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch candidate.Node.Kind {
|
|
|
|
case yaml.SequenceNode:
|
|
|
|
return candidate.Node.Content
|
|
|
|
default:
|
|
|
|
return []*yaml.Node{candidate.Node}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func AddOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode *PathTreeNode) (*list.List, error) {
|
|
|
|
log.Debugf("Add operator")
|
|
|
|
|
2020-12-21 00:54:03 +00:00
|
|
|
return crossFunction(d, matchingNodes, pathNode, add)
|
|
|
|
}
|
2020-11-24 02:07:19 +00:00
|
|
|
|
2020-12-21 00:54:03 +00:00
|
|
|
func add(d *dataTreeNavigator, lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error) {
|
|
|
|
lhs.Node = UnwrapDoc(lhs.Node)
|
|
|
|
rhs.Node = UnwrapDoc(rhs.Node)
|
2020-11-24 02:07:19 +00:00
|
|
|
|
2020-12-21 00:54:03 +00:00
|
|
|
target := &CandidateNode{
|
|
|
|
Path: lhs.Path,
|
|
|
|
Document: lhs.Document,
|
|
|
|
Filename: lhs.Filename,
|
|
|
|
Node: &yaml.Node{},
|
|
|
|
}
|
|
|
|
lhsNode := lhs.Node
|
2020-11-24 02:07:19 +00:00
|
|
|
|
2020-12-21 00:54:03 +00:00
|
|
|
switch lhsNode.Kind {
|
|
|
|
case yaml.MappingNode:
|
|
|
|
return nil, fmt.Errorf("Maps not yet supported for addition")
|
|
|
|
case yaml.SequenceNode:
|
|
|
|
target.Node.Kind = yaml.SequenceNode
|
|
|
|
target.Node.Style = lhsNode.Style
|
|
|
|
target.Node.Tag = "!!seq"
|
|
|
|
target.Node.Content = append(lhsNode.Content, toNodes(rhs)...)
|
|
|
|
case yaml.ScalarNode:
|
|
|
|
return nil, fmt.Errorf("Scalars not yet supported for addition")
|
2020-11-24 02:07:19 +00:00
|
|
|
}
|
2020-12-21 00:54:03 +00:00
|
|
|
|
|
|
|
return target, nil
|
2020-11-24 02:07:19 +00:00
|
|
|
}
|