mirror of
https://github.com/mikefarah/yq.git
synced 2026-10-12 02:19:06 +00:00
match(...) | (.captures[] as $c ireduce (...)) folded every match's captures into one accumulator, so only the last object remained. jq reduce runs once per input. Use the same EvaluateTogether split as `as` and collect; eval-all file merges are unchanged. Fixes #2859
84 lines
2.6 KiB
Go
84 lines
2.6 KiB
Go
package yqlib
|
|
|
|
import (
|
|
"container/list"
|
|
"fmt"
|
|
)
|
|
|
|
func reduceOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
|
|
log.Debugf("reduceOp")
|
|
//.a as $var reduce (0; . + $var)
|
|
//lhs is the assignment operator
|
|
//rhs is the reduce block
|
|
// '.' refers to the current accumulator, initialised to 0
|
|
// $var references a single element from the .a
|
|
|
|
//ensure lhs is actually an assignment
|
|
//and rhs is a block (empty)
|
|
if expressionNode.LHS.Operation.OperationType != assignVariableOpType {
|
|
return Context{}, fmt.Errorf("reduce must be given a variables assignment, got %v instead", expressionNode.LHS.Operation.OperationType.Type)
|
|
} else if expressionNode.RHS.Operation.OperationType != blockOpType {
|
|
return Context{}, fmt.Errorf("reduce must be given a block, got %v instead", expressionNode.RHS.Operation.OperationType.Type)
|
|
}
|
|
|
|
// Same EvaluateTogether split as `as` / collect / crossFunction:
|
|
// file documents are reduced together (eval-all merge), while
|
|
// pipeline results such as match(...) are reduced per input, like jq.
|
|
var evaluateAllTogether = true
|
|
for matchEl := context.MatchingNodes.Front(); matchEl != nil; matchEl = matchEl.Next() {
|
|
evaluateAllTogether = evaluateAllTogether && matchEl.Value.(*CandidateNode).EvaluateTogether
|
|
if !evaluateAllTogether {
|
|
break
|
|
}
|
|
}
|
|
if evaluateAllTogether {
|
|
return reduceOperatorSingle(d, context, expressionNode)
|
|
}
|
|
|
|
results := list.New()
|
|
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
|
|
result, err := reduceOperatorSingle(d, context.SingleChildContext(el.Value.(*CandidateNode)), expressionNode)
|
|
if err != nil {
|
|
return Context{}, err
|
|
}
|
|
results.PushBackList(result.MatchingNodes)
|
|
}
|
|
return context.ChildContext(results), nil
|
|
}
|
|
|
|
func reduceOperatorSingle(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
|
|
arrayExpNode := expressionNode.LHS.LHS
|
|
array, err := d.GetMatchingNodes(context, arrayExpNode)
|
|
|
|
if err != nil {
|
|
return Context{}, err
|
|
}
|
|
|
|
variableName := expressionNode.LHS.RHS.Operation.StringValue
|
|
|
|
initExp := expressionNode.RHS.LHS
|
|
|
|
accum, err := d.GetMatchingNodes(context, initExp)
|
|
if err != nil {
|
|
return Context{}, err
|
|
}
|
|
|
|
log.Debugf("with variable %v", variableName)
|
|
|
|
blockExp := expressionNode.RHS.RHS
|
|
for el := array.MatchingNodes.Front(); el != nil; el = el.Next() {
|
|
candidate := el.Value.(*CandidateNode)
|
|
log.Debugf("REDUCING WITH %v", NodeToString(candidate))
|
|
l := list.New()
|
|
l.PushBack(candidate)
|
|
accum.SetVariable(variableName, l)
|
|
|
|
accum, err = d.GetMatchingNodes(accum, blockExp)
|
|
if err != nil {
|
|
return Context{}, err
|
|
}
|
|
}
|
|
|
|
return accum, nil
|
|
}
|