ExecuteAST executes the given AST with the given input.
(ctx context.Context, expr ast.Expr, value *model.Value, options *Options)
| 35 | |
| 36 | // ExecuteAST executes the given AST with the given input. |
| 37 | func ExecuteAST(ctx context.Context, expr ast.Expr, value *model.Value, options *Options) (*model.Value, error) { |
| 38 | if expr == nil { |
| 39 | return value, nil |
| 40 | } |
| 41 | |
| 42 | executorFn, err := exprExecutor(options, expr) |
| 43 | if err != nil { |
| 44 | return nil, fmt.Errorf("error evaluating expression %T: %w", expr, err) |
| 45 | } |
| 46 | |
| 47 | executor := func(ctx context.Context, options *Options, data *model.Value) (*model.Value, error) { |
| 48 | options.Vars["this"] = data |
| 49 | out, err := executorFn(ctx, options, data) |
| 50 | if err != nil { |
| 51 | return out, err |
| 52 | } |
| 53 | return out, nil |
| 54 | } |
| 55 | |
| 56 | if !value.IsBranch() { |
| 57 | res, err := executor(ctx, options, value) |
| 58 | if err != nil { |
| 59 | return nil, fmt.Errorf("execution error when processing %T: %w", expr, err) |
| 60 | } |
| 61 | return res, nil |
| 62 | } |
| 63 | |
| 64 | res := model.NewSliceValue() |
| 65 | res.MarkAsBranch() |
| 66 | |
| 67 | if err := value.RangeSlice(func(i int, v *model.Value) error { |
| 68 | r, err := executor(ctx, options, v) |
| 69 | if err != nil { |
| 70 | return err |
| 71 | } |
| 72 | if r.IsIgnore() { |
| 73 | return nil |
| 74 | } |
| 75 | return res.Append(r) |
| 76 | }); err != nil { |
| 77 | return nil, fmt.Errorf("branch execution error when processing %T: %w", expr, err) |
| 78 | } |
| 79 | |
| 80 | return res, nil |
| 81 | } |
| 82 | |
| 83 | var unstableAstTypes = []reflect.Type{ |
| 84 | reflect.TypeFor[ast.BranchExpr](), |
no test coverage detected