MCPcopy Index your code
hub / github.com/TomWright/dasel / filterExprExecutor

Function filterExprExecutor

execution/execute_filter.go:11–46  ·  view source on GitHub ↗
(e ast.FilterExpr)

Source from the content-addressed store, hash-verified

9)
10
11func filterExprExecutor(e ast.FilterExpr) (expressionExecutor, error) {
12 return func(ctx context.Context, options *Options, data *model.Value) (*model.Value, error) {
13 ctx = WithExecutorID(ctx, "filterExpr")
14 if !data.IsSlice() {
15 return nil, fmt.Errorf("cannot filter over non-array")
16 }
17 res := model.NewSliceValue()
18
19 if err := data.RangeSlice(func(i int, item *model.Value) error {
20 restore := withKeyVar(options, model.NewIntValue(int64(i)))
21 defer restore()
22
23 v, err := ExecuteAST(ctx, e.Expr, item, options)
24 if err != nil {
25 return err
26 }
27
28 boolV, err := v.BoolValue()
29 if err != nil {
30 return err
31 }
32
33 if !boolV {
34 return nil
35 }
36 if err := res.Append(item); err != nil {
37 return fmt.Errorf("error appending item to result: %w", err)
38 }
39 return nil
40 }); err != nil {
41 return nil, fmt.Errorf("error ranging over slice: %w", err)
42 }
43
44 return res, nil
45 }, nil
46}

Callers 1

exprExecutorFunction · 0.85

Calls 9

NewSliceValueFunction · 0.92
NewIntValueFunction · 0.92
WithExecutorIDFunction · 0.85
withKeyVarFunction · 0.85
ExecuteASTFunction · 0.85
IsSliceMethod · 0.80
RangeSliceMethod · 0.80
BoolValueMethod · 0.80
AppendMethod · 0.80

Tested by

no test coverage detected