| 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 | "github.com/tomwright/dasel/v3/selector/lexer" |
| 10 | ) |
| 11 | |
| 12 | func unaryExprExecutor(e ast.UnaryExpr) (expressionExecutor, error) { |
| 13 | return func(ctx context.Context, options *Options, data *model.Value) (*model.Value, error) { |
| 14 | ctx = WithExecutorID(ctx, "unaryExpr") |
| 15 | right, err := ExecuteAST(ctx, e.Right, data, options) |
| 16 | if err != nil { |
| 17 | return nil, fmt.Errorf("error evaluating right expression: %w", err) |
| 18 | } |
| 19 | |
| 20 | switch e.Operator.Kind { |
| 21 | case lexer.Exclamation: |
| 22 | boolV, err := right.BoolValue() |
| 23 | if err != nil { |
| 24 | return nil, fmt.Errorf("error converting value to boolean: %w", err) |
| 25 | } |
| 26 | return model.NewBoolValue(!boolV), nil |
| 27 | default: |
| 28 | return nil, fmt.Errorf("unhandled unary operator: %s", e.Operator.Value) |
| 29 | } |
| 30 | }, nil |
| 31 | } |