Add now uses crossFunction

This commit is contained in:
Mike Farah 2020-12-21 11:54:03 +11:00
parent 9876b0ce8f
commit ca8cd78616
2 changed files with 37 additions and 42 deletions

View File

@ -22,13 +22,7 @@ func AddAssignOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode
return d.GetMatchingNodes(matchingNodes, assignmentOpNode) return d.GetMatchingNodes(matchingNodes, assignmentOpNode)
} }
func toNodes(candidates *list.List) []*yaml.Node { func toNodes(candidate *CandidateNode) []*yaml.Node {
if candidates.Len() == 0 {
return []*yaml.Node{}
}
candidate := candidates.Front().Value.(*CandidateNode)
if candidate.Node.Tag == "!!null" { if candidate.Node.Tag == "!!null" {
return []*yaml.Node{} return []*yaml.Node{}
} }
@ -44,27 +38,21 @@ func toNodes(candidates *list.List) []*yaml.Node {
func AddOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode *PathTreeNode) (*list.List, error) { func AddOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode *PathTreeNode) (*list.List, error) {
log.Debugf("Add operator") log.Debugf("Add operator")
var results = list.New()
lhs, err := d.GetMatchingNodes(matchingNodes, pathNode.Lhs)
if err != nil {
return nil, err
}
rhs, err := d.GetMatchingNodes(matchingNodes, pathNode.Rhs)
if err != nil { return crossFunction(d, matchingNodes, pathNode, add)
return nil, err
} }
for el := lhs.Front(); el != nil; el = el.Next() { func add(d *dataTreeNavigator, lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error) {
lhsCandidate := el.Value.(*CandidateNode) lhs.Node = UnwrapDoc(lhs.Node)
lhsNode := UnwrapDoc(lhsCandidate.Node) rhs.Node = UnwrapDoc(rhs.Node)
target := &CandidateNode{ target := &CandidateNode{
Path: lhsCandidate.Path, Path: lhs.Path,
Document: lhsCandidate.Document, Document: lhs.Document,
Filename: lhsCandidate.Filename, Filename: lhs.Filename,
Node: &yaml.Node{}, Node: &yaml.Node{},
} }
lhsNode := lhs.Node
switch lhsNode.Kind { switch lhsNode.Kind {
case yaml.MappingNode: case yaml.MappingNode:
@ -74,10 +62,9 @@ func AddOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode *PathT
target.Node.Style = lhsNode.Style target.Node.Style = lhsNode.Style
target.Node.Tag = "!!seq" target.Node.Tag = "!!seq"
target.Node.Content = append(lhsNode.Content, toNodes(rhs)...) target.Node.Content = append(lhsNode.Content, toNodes(rhs)...)
results.PushBack(target)
case yaml.ScalarNode: case yaml.ScalarNode:
return nil, fmt.Errorf("Scalars not yet supported for addition") return nil, fmt.Errorf("Scalars not yet supported for addition")
} }
}
return results, nil return target, nil
} }

View File

@ -21,6 +21,14 @@ var addOperatorScenarios = []expressionScenario{
"D0, P[a], (!!seq)::[1, 2, 3, 4]\n", "D0, P[a], (!!seq)::[1, 2, 3, 4]\n",
}, },
}, },
{
skipDoc: true,
expression: `[1] + ([2], [3])`,
expected: []string{
"D0, P[], (!!seq)::- 1\n- 2\n",
"D0, P[], (!!seq)::- 1\n- 3\n",
},
},
{ {
description: "Concatenate null to array", description: "Concatenate null to array",
document: `{a: [1,2]}`, document: `{a: [1,2]}`,