| 1 | package execution |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | |
| 7 | "github.com/tomwright/dasel/v3/model" |
| 8 | "github.com/tomwright/dasel/v3/selector/ast" |
| 9 | ) |
| 10 | |
| 11 | func conditionalExprExecutor(e ast.ConditionalExpr) (expressionExecutor, error) { |
| 12 | return func(ctx context.Context, options *Options, data *model.Value) (*model.Value, error) { |
| 13 | ctx = WithExecutorID(ctx, "conditionalExpr") |
| 14 | cond, err := ExecuteAST(ctx, e.Cond, data, options) |
| 15 | if err != nil { |
| 16 | return nil, fmt.Errorf("error evaluating condition: %w", err) |
| 17 | } |
| 18 | |
| 19 | condBool, err := cond.BoolValue() |
| 20 | if err != nil { |
| 21 | return nil, fmt.Errorf("error converting condition to boolean: %w", err) |
| 22 | } |
| 23 | |
| 24 | if condBool { |
| 25 | res, err := ExecuteAST(ctx, e.Then, data, options) |
| 26 | if err != nil { |
| 27 | return nil, fmt.Errorf("error executing then block: %w", err) |
| 28 | } |
| 29 | return res, nil |
| 30 | } |
| 31 | |
| 32 | if e.Else != nil { |
| 33 | res, err := ExecuteAST(ctx, e.Else, data, options) |
| 34 | if err != nil { |
| 35 | return nil, fmt.Errorf("error executing else block: %w", err) |
| 36 | } |
| 37 | return res, nil |
| 38 | } |
| 39 | |
| 40 | return model.NewNullValue(), nil |
| 41 | }, nil |
| 42 | } |