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,40 +38,33 @@ 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 }
}
func add(d *dataTreeNavigator, lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error) {
for el := lhs.Front(); el != nil; el = el.Next() { lhs.Node = UnwrapDoc(lhs.Node)
lhsCandidate := el.Value.(*CandidateNode) rhs.Node = UnwrapDoc(rhs.Node)
lhsNode := UnwrapDoc(lhsCandidate.Node)
target := &CandidateNode{
target := &CandidateNode{ Path: lhs.Path,
Path: lhsCandidate.Path, Document: lhs.Document,
Document: lhsCandidate.Document, Filename: lhs.Filename,
Filename: lhsCandidate.Filename, Node: &yaml.Node{},
Node: &yaml.Node{}, }
} lhsNode := lhs.Node
switch lhsNode.Kind { switch lhsNode.Kind {
case yaml.MappingNode: case yaml.MappingNode:
return nil, fmt.Errorf("Maps not yet supported for addition") return nil, fmt.Errorf("Maps not yet supported for addition")
case yaml.SequenceNode: case yaml.SequenceNode:
target.Node.Kind = yaml.SequenceNode target.Node.Kind = yaml.SequenceNode
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 target, nil
return results, 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]}`,