Debian Codegraph @

execution/execute_reduce.go

raw symbols SCIP
1 package executionscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/execution`/
2
3 import (
4 "contextscip-go gomod github.com/golang/go/src go1.25 context/"
5 "fmtscip-go gomod github.com/golang/go/src go1.25 fmt/"
6
7 "github.com/tomwright/dasel/v3/modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/"
8 "github.com/tomwright/dasel/v3/selector/astscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/selector/ast`/"
9 )
10
11 func reduceExprExecutorscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/execution`/reduceExprExecutor().(e astscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/selector/ast`/.ReduceExprscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/selector/ast`/ReduceExpr#) (expressionExecutorscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/execution`/expressionExecutor#, error) {
12 return func(ctx contextscip-go gomod github.com/golang/go/src go1.25 context/.Contextscip-go gomod github.com/golang/go/src go1.25 context/Context#, options *Optionsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/execution`/Options#, data *modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.Valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#) (*modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.Valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#, error) {
13 ctx = WithExecutorIDscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/execution`/WithExecutorID().(ctx, "reduceExpr")
14 if !data.IsSlicescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#IsSlice().() {
15 return nil, fmtscip-go gomod github.com/golang/go/src go1.25 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25 fmt/Errorf().("cannot reduce over non-array")
16 }
17
18 // Evaluate init expression to get the initial accumulator value.
19 acc, err := ExecuteASTscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/execution`/ExecuteAST().(ctx, e.Initscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/selector/ast`/ReduceExpr#Init., data, options)
20 if err != nil {
21 return nil, fmtscip-go gomod github.com/golang/go/src go1.25 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25 fmt/Errorf().("error evaluating reduce init: %w", err)
22 }
23
24 // Save and restore the acc variable to avoid polluting the caller's scope.
25 prevAcc, hadAcc := options.Varsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/execution`/Options#Vars.["acc"]
26 defer func() {
27 if hadAcc {
28 options.Varsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/execution`/Options#Vars.["acc"] = prevAcc
29 } else {
30 delete(options.Varsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/execution`/Options#Vars., "acc")
31 }
32 }()
33
34 if err := data.RangeSlicescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#RangeSlice().(func(i int, item *modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.Valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#) error {
35 restore := withKeyVarscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/execution`/withKeyVar().(options, modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.NewIntValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/NewIntValue().(int64(i)))
36 defer restore()
37
38 // Evaluate the per-element expression against the item.
39 elemVal, err := ExecuteASTscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/execution`/ExecuteAST().(ctx, e.Exprscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/selector/ast`/ReduceExpr#Expr., item, options)
40 if err != nil {
41 return fmtscip-go gomod github.com/golang/go/src go1.25 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25 fmt/Errorf().("error evaluating reduce expr for element %d: %w", i, err)
42 }
43
44 // Set $acc to the current accumulator and $this to the element value.
45 options.Varsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/execution`/Options#Vars.["acc"] = acc
46 options.Varsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/execution`/Options#Vars.["this"] = elemVal
47
48 // Evaluate the update expression.
49 newAcc, err := ExecuteASTscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/execution`/ExecuteAST().(ctx, e.Updatescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/selector/ast`/ReduceExpr#Update., elemVal, options)
50 if err != nil {
51 return fmtscip-go gomod github.com/golang/go/src go1.25 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25 fmt/Errorf().("error evaluating reduce update for element %d: %w", i, err)
52 }
53 acc = newAcc
54 return nil
55 }); err != nil {
56 return nil, fmtscip-go gomod github.com/golang/go/src go1.25 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25 fmt/Errorf().("error ranging over slice: %w", err)
57 }
58
59 return acc, nil
60 }, nil
61 }