-
-
Notifications
You must be signed in to change notification settings - Fork 167
Expand file tree
/
Copy pathexecute_branch.go
More file actions
49 lines (43 loc) · 1.35 KB
/
execute_branch.go
File metadata and controls
49 lines (43 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package execution
import (
"context"
"fmt"
"github.com/tomwright/dasel/v3/model"
"github.com/tomwright/dasel/v3/selector/ast"
)
func branchExprExecutor(e ast.BranchExpr) (expressionExecutor, error) {
return func(ctx context.Context, options *Options, data *model.Value) (*model.Value, error) {
ctx = WithExecutorID(ctx, "branchExpr")
res := model.NewSliceValue()
res.MarkAsBranch()
if len(e.Exprs) == 0 {
// No expressions given. We'll branch on the input data.
if err := data.RangeSlice(func(_ int, value *model.Value) error {
if err := res.Append(value); err != nil {
return fmt.Errorf("failed to append branch result: %w", err)
}
return nil
}); err != nil {
return nil, fmt.Errorf("failed to range slice: %w", err)
}
} else {
for _, expr := range e.Exprs {
r, err := ExecuteAST(ctx, expr, data, options)
if err != nil {
return nil, fmt.Errorf("failed to execute branch expr: %w", err)
}
// This deals with the spread operator in the branch expression.
valsToAppend, err := prepareSpreadValues(r)
if err != nil {
return nil, fmt.Errorf("error handling spread values: %w", err)
}
for _, v := range valsToAppend {
if err := res.Append(v); err != nil {
return nil, fmt.Errorf("failed to append branch result: %w", err)
}
}
}
}
return res, nil
}, nil
}