| 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 mapValuesExprExecutor(e ast.MapValuesExpr) (expressionExecutor, error) { |
| 12 | return func(ctx context.Context, options *Options, data *model.Value) (*model.Value, error) { |
| 13 | ctx = WithExecutorID(ctx, "mapValuesExpr") |
| 14 | if !data.IsMap() { |
| 15 | return nil, fmt.Errorf("cannot mapValues over non-map") |
| 16 | } |
| 17 | |
| 18 | res := model.NewMapValue() |
| 19 | |
| 20 | if err := data.RangeMap(func(key string, value *model.Value) error { |
| 21 | restore := withKeyVar(options, model.NewStringValue(key)) |
| 22 | defer restore() |
| 23 | |
| 24 | transformed, err := ExecuteAST(ctx, e.Expr, value, options) |
| 25 | if err != nil { |
| 26 | return fmt.Errorf("error evaluating mapValues expr for key %q: %w", key, err) |
| 27 | } |
| 28 | return res.SetMapKey(key, transformed) |
| 29 | }); err != nil { |
| 30 | return nil, fmt.Errorf("error ranging over map: %w", err) |
| 31 | } |
| 32 | |
| 33 | return res, nil |
| 34 | }, nil |
| 35 | } |