From f7cfdc29e162620838876a6b5f50fb25944ec34a Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Sat, 16 Jan 2021 14:09:49 +1100 Subject: [PATCH] cross function fix wip --- pkg/yqlib/operator_add_test.go | 9 ++++++ pkg/yqlib/operator_multiply.go | 50 +++++++++++++++++++--------------- 2 files changed, 37 insertions(+), 22 deletions(-) diff --git a/pkg/yqlib/operator_add_test.go b/pkg/yqlib/operator_add_test.go index 5edb8433..d3001192 100644 --- a/pkg/yqlib/operator_add_test.go +++ b/pkg/yqlib/operator_add_test.go @@ -5,6 +5,15 @@ import ( ) 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", document: `{a: {val: thing, b: [cat,dog]}}`, diff --git a/pkg/yqlib/operator_multiply.go b/pkg/yqlib/operator_multiply.go index 1cfd14a1..7d8d0787 100644 --- a/pkg/yqlib/operator_multiply.go +++ b/pkg/yqlib/operator_multiply.go @@ -11,35 +11,40 @@ import ( 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) { - 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() + for matchEl := matchingNodes.Front(); matchEl != nil; matchEl = matchEl.Next() { + contextList := nodeToMap(matchEl.Value.(*CandidateNode)) - for el := lhs.Front(); el != nil; el = el.Next() { - lhsCandidate := el.Value.(*CandidateNode) + lhs, err := d.GetMatchingNodes(contextList, expressionNode.Lhs) + if err != nil { + return nil, err + } + log.Debugf("crossFunction LHS len: %v", lhs.Len()) - 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 + rhs, err := d.GetMatchingNodes(contextList, expressionNode.Rhs) + + 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 } @@ -69,7 +74,8 @@ func multiply(preferences multiplyPreferences) func(d *dataTreeNavigator, lhs *C return nil, err } 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) }