MCPcopy
hub / github.com/TomWright/dasel / ExecuteAST

Function ExecuteAST

execution/execute.go:37–81  ·  view source on GitHub ↗

ExecuteAST executes the given AST with the given input.

(ctx context.Context, expr ast.Expr, value *model.Value, options *Options)

Source from the content-addressed store, hash-verified

35
36// ExecuteAST executes the given AST with the given input.
37func 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
83var unstableAstTypes = []reflect.Type{
84 reflect.TypeFor[ast.BranchExpr](),

Callers 15

prepareArgsFunction · 0.85
mapExprExecutorFunction · 0.85
groupByExprExecutorFunction · 0.85
unaryExprExecutorFunction · 0.85
arrayExprExecutorFunction · 0.85
rangeExprExecutorFunction · 0.85
indexExprExecutorFunction · 0.85
mapValuesExprExecutorFunction · 0.85
branchExprExecutorFunction · 0.85
filterExprExecutorFunction · 0.85
allExprExecutorFunction · 0.85
objectExprExecutorFunction · 0.85

Calls 7

NewSliceValueFunction · 0.92
exprExecutorFunction · 0.85
IsBranchMethod · 0.80
MarkAsBranchMethod · 0.80
RangeSliceMethod · 0.80
IsIgnoreMethod · 0.80
AppendMethod · 0.80

Tested by

no test coverage detected