Debian Codegraph @

execution/execute.go

raw symbols SCIP
1 package execution
2
3 import (
4 "context"
5 "errors"
6 "fmt"
7 "github.com/tomwright/dasel/v3/model"
8 "github.com/tomwright/dasel/v3/selector"
9 "github.com/tomwright/dasel/v3/selector/ast"
10 "os"
11 "reflect"
12 "slices"
13 )
14
15 // ExecuteSelector parses the selector and executes the resulting AST with the given input.
16 func ExecuteSelector(ctx context.Context, selectorStr string, value *model.Value, opts *Options) (*model.Value, error) {
17 if selectorStr == "" {
18 return value, nil
19 }
20
21 expr, err := selector.Parse(selectorStr)
22 if err != nil {
23 return nil, fmt.Errorf("error parsing selector: %w", err)
24 }
25
26 res, err := ExecuteAST(ctx, expr, value, opts)
27 if err != nil {
28 return nil, fmt.Errorf("error executing selector: %w", err)
29 }
30
31 return res, nil
32 }
33
34 type expressionExecutor func(ctx context.Context, options *Options, data *model.Value) (*model.Value, error)
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](),
85 }
86
87 func exprExecutor(options *Options, expr ast.Expr) (expressionExecutor, error) {
88 if !options.Unstable && (slices.Contains(unstableAstTypes, reflect.TypeOf(expr)) ||
89 slices.Contains(unstableAstTypes, reflect.ValueOf(expr).Type())) {
90 return nil, errors.New("unstable ast types are not enabled. to enable them use --unstable")
91 }
92
93 switch e := expr.(type) {
94 case ast.BinaryExpr:
95 return binaryExprExecutor(e)
96 case ast.UnaryExpr:
97 return unaryExprExecutor(e)
98 case ast.CallExpr:
99 return callExprExecutor(options, e)
100 case ast.ChainedExpr:
101 return chainedExprExecutor(e)
102 case ast.SpreadExpr:
103 return spreadExprExecutor()
104 case ast.RangeExpr:
105 return rangeExprExecutor(e)
106 case ast.IndexExpr:
107 return indexExprExecutor(e)
108 case ast.PropertyExpr:
109 return propertyExprExecutor(e)
110 case ast.VariableExpr:
111 return variableExprExecutor(e)
112 case ast.NumberIntExpr:
113 return numberIntExprExecutor(e)
114 case ast.NumberFloatExpr:
115 return numberFloatExprExecutor(e)
116 case ast.StringExpr:
117 return stringExprExecutor(e)
118 case ast.BoolExpr:
119 return boolExprExecutor(e)
120 case ast.ObjectExpr:
121 return objectExprExecutor(e)
122 case ast.MapExpr:
123 return mapExprExecutor(e)
124 case ast.EachExpr:
125 return eachExprExecutor(e)
126 case ast.FilterExpr:
127 return filterExprExecutor(e)
128 case ast.SearchExpr:
129 return searchExprExecutor(e)
130 case ast.RecursiveDescentExpr:
131 return recursiveDescentExprExecutor2(e)
132 case ast.ConditionalExpr:
133 return conditionalExprExecutor(e)
134 case ast.BranchExpr:
135 return branchExprExecutor(e)
136 case ast.ArrayExpr:
137 return arrayExprExecutor(e)
138 case ast.RegexExpr:
139 // Noop
140 return func(ctx context.Context, options *Options, data *model.Value) (*model.Value, error) {
141 //ctx = WithExecutorID(ctx, "regexExpr")
142 return data, nil
143 }, nil
144 case ast.SortByExpr:
145 return sortByExprExecutor(e)
146 case ast.GroupByExpr:
147 return groupByExprExecutor(e)
148 case ast.ReduceExpr:
149 return reduceExprExecutor(e)
150 case ast.MapValuesExpr:
151 return mapValuesExprExecutor(e)
152 case ast.AnyExpr:
153 return anyExprExecutor(e)
154 case ast.AllExpr:
155 return allExprExecutor(e)
156 case ast.CountExpr:
157 return countExprExecutor(e)
158 case ast.NullExpr:
159 return func(ctx context.Context, options *Options, data *model.Value) (*model.Value, error) {
160 //ctx = WithExecutorID(ctx, "nullExpr")
161 return model.NewNullValue(), nil
162 }, nil
163 default:
164 return nil, fmt.Errorf("unhandled expression type: %T", e)
165 }
166 }
167
168 func chainedExprExecutor(e ast.ChainedExpr) (expressionExecutor, error) {
169 return func(ctx context.Context, options *Options, data *model.Value) (*model.Value, error) {
170 ctx = WithExecutorID(ctx, "chainedExpr")
171 var curData = data
172 for _, expr := range e.Exprs {
173 res, err := ExecuteAST(ctx, expr, curData, options)
174 if err != nil {
175 return nil, fmt.Errorf("error executing expression: %w", err)
176 }
177 curData = res
178 }
179 return curData, nil
180 }, nil
181 }
182
183 func variableExprExecutor(e ast.VariableExpr) (expressionExecutor, error) {
184 return func(ctx context.Context, options *Options, data *model.Value) (*model.Value, error) {
185 //ctx = WithExecutorID(ctx, "variableExpr")
186 varName := e.Name
187 res, ok := options.Vars[varName]
188 if ok {
189 return res, nil
190 }
191
192 envVarValue := os.Getenv(varName)
193 if envVarValue != "" {
194 return model.NewStringValue(envVarValue), nil
195 }
196
197 return nil, fmt.Errorf("variable %s not found", varName)
198 }, nil
199 }