cross function fix wip

This commit is contained in:
Mike Farah 2021-01-16 14:09:49 +11:00
parent 07c6549a58
commit f7cfdc29e1
2 changed files with 37 additions and 22 deletions

View File

@ -5,6 +5,15 @@ import (
) )
var addOperatorScenarios = []expressionScenario{ var addOperatorScenarios = []expressionScenario{
{
skipDoc: true,
document: `[{a: foo, b: bar}, {a: 1, b: 2}]`,
expression: ".[] | .a + .b",
expected: []string{
"D0, P[0 a], (!!str)::foobar\n",
"D0, P[1 a], (!!int)::3\n",
},
},
{ {
description: "Concatenate and assign arrays", description: "Concatenate and assign arrays",
document: `{a: {val: thing, b: [cat,dog]}}`, document: `{a: {val: thing, b: [cat,dog]}}`,

View File

@ -11,35 +11,40 @@ import (
type crossFunctionCalculation func(d *dataTreeNavigator, lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error) type crossFunctionCalculation func(d *dataTreeNavigator, lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error)
func crossFunction(d *dataTreeNavigator, matchingNodes *list.List, expressionNode *ExpressionNode, calculation crossFunctionCalculation) (*list.List, error) { func crossFunction(d *dataTreeNavigator, matchingNodes *list.List, expressionNode *ExpressionNode, calculation crossFunctionCalculation) (*list.List, error) {
lhs, err := d.GetMatchingNodes(matchingNodes, expressionNode.Lhs)
if err != nil {
return nil, err
}
log.Debugf("crossFunction LHS len: %v", lhs.Len())
rhs, err := d.GetMatchingNodes(matchingNodes, expressionNode.Rhs)
if err != nil {
return nil, err
}
log.Debugf("crossFunction RHS len: %v", rhs.Len())
var results = list.New() var results = list.New()
for matchEl := matchingNodes.Front(); matchEl != nil; matchEl = matchEl.Next() {
contextList := nodeToMap(matchEl.Value.(*CandidateNode))
for el := lhs.Front(); el != nil; el = el.Next() { lhs, err := d.GetMatchingNodes(contextList, expressionNode.Lhs)
lhsCandidate := el.Value.(*CandidateNode) if err != nil {
return nil, err
}
log.Debugf("crossFunction LHS len: %v", lhs.Len())
for rightEl := rhs.Front(); rightEl != nil; rightEl = rightEl.Next() { rhs, err := d.GetMatchingNodes(contextList, expressionNode.Rhs)
log.Debugf("Applying calc")
rhsCandidate := rightEl.Value.(*CandidateNode) if err != nil {
resultCandidate, err := calculation(d, lhsCandidate, rhsCandidate) return nil, err
if err != nil { }
return nil, err
for el := lhs.Front(); el != nil; el = el.Next() {
lhsCandidate := el.Value.(*CandidateNode)
for rightEl := rhs.Front(); rightEl != nil; rightEl = rightEl.Next() {
log.Debugf("Applying calc")
rhsCandidate := rightEl.Value.(*CandidateNode)
resultCandidate, err := calculation(d, lhsCandidate, rhsCandidate)
if err != nil {
return nil, err
}
results.PushBack(resultCandidate)
} }
results.PushBack(resultCandidate)
} }
} }
return results, nil return results, nil
} }
@ -69,7 +74,8 @@ func multiply(preferences multiplyPreferences) func(d *dataTreeNavigator, lhs *C
return nil, err return nil, err
} }
return mergeObjects(d, newThing, rhs, preferences) return mergeObjects(d, newThing, rhs, preferences)
} else if lhs.Node.Tag == "!!int" && rhs.Node.Tag == "!!int" {
return lhs.CreateChild(nil, &yaml.Node{Kind: yaml.ScalarNode, Tag: "!!str", Value: "12"}), nil
} }
return nil, fmt.Errorf("Cannot multiply %v with %v", lhs.Node.Tag, rhs.Node.Tag) return nil, fmt.Errorf("Cannot multiply %v with %v", lhs.Node.Tag, rhs.Node.Tag)
} }