| 1 | package execution |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "github.com/tomwright/dasel/v3/model" |
| 7 | "github.com/tomwright/dasel/v3/selector/ast" |
| 8 | ) |
| 9 | |
| 10 | func recursiveDescentExprExecutor2(e ast.RecursiveDescentExpr) (expressionExecutor, error) { |
| 11 | var doSearch func(ctx context.Context, options *Options, data *model.Value) ([]*model.Value, error) |
| 12 | findValue := func(ctx context.Context, options *Options, v *model.Value) (*model.Value, error) { |
| 13 | property, err := ExecuteAST(ctx, e.Expr, v, options) |
| 14 | if err != nil { |
| 15 | handleErrs := []any{ |
| 16 | model.ErrIncompatibleTypes{}, |
| 17 | model.ErrUnexpectedType{}, |
| 18 | model.ErrUnexpectedTypes{}, |
| 19 | model.SliceIndexOutOfRange{}, |
| 20 | model.MapKeyNotFound{}, |
| 21 | } |
| 22 | for _, e := range handleErrs { |
| 23 | if errors.As(err, &e) { |
| 24 | err = nil |
| 25 | break |
| 26 | } |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | if err != nil { |
| 31 | return nil, err |
| 32 | } |
| 33 | return property, nil |
| 34 | } |
| 35 | doSearch = func(ctx context.Context, options *Options, data *model.Value) ([]*model.Value, error) { |
| 36 | res := make([]*model.Value, 0) |
| 37 | |
| 38 | switch data.Type() { |
| 39 | case model.TypeMap: |
| 40 | if err := data.RangeMap(func(key string, v *model.Value) error { |
| 41 | restore := withKeyVar(options, model.NewStringValue(key)) |
| 42 | defer restore() |
| 43 | |
| 44 | if v.IsScalar() { |
| 45 | if e.IsWildcard { |
| 46 | res = append(res, v) |
| 47 | } |
| 48 | } else { |
| 49 | if !e.IsWildcard { |
| 50 | property, err := findValue(ctx, options, v) |
| 51 | if err != nil { |
| 52 | return err |
| 53 | } |
| 54 | if property != nil { |
| 55 | res = append(res, property) |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | gotNext, err := doSearch(ctx, options, v) |
| 60 | if err != nil { |
| 61 | return err |
| 62 | } |
| 63 | res = append(res, gotNext...) |
| 64 | } |
| 65 | return nil |
| 66 | }); err != nil { |
| 67 | return nil, err |
| 68 | } |
| 69 | case model.TypeSlice: |
| 70 | if err := data.RangeSlice(func(i int, v *model.Value) error { |
| 71 | restore := withKeyVar(options, model.NewIntValue(int64(i))) |
| 72 | defer restore() |
| 73 | |
| 74 | if v.IsScalar() { |
| 75 | if e.IsWildcard { |
| 76 | res = append(res, v) |
| 77 | } |
| 78 | } else { |
| 79 | if !e.IsWildcard { |
| 80 | property, err := findValue(ctx, options, v) |
| 81 | if err != nil { |
| 82 | return err |
| 83 | } |
| 84 | if property != nil { |
| 85 | res = append(res, property) |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | gotNext, err := doSearch(ctx, options, v) |
| 90 | if err != nil { |
| 91 | return err |
| 92 | } |
| 93 | res = append(res, gotNext...) |
| 94 | } |
| 95 | return nil |
| 96 | }); err != nil { |
| 97 | return nil, err |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | return res, nil |
| 102 | } |
| 103 | |
| 104 | return func(ctx context.Context, options *Options, data *model.Value) (*model.Value, error) { |
| 105 | ctx = WithExecutorID(ctx, "recursiveDescentExpr") |
| 106 | matches := model.NewSliceValue() |
| 107 | |
| 108 | found, err := doSearch(ctx, options, data) |
| 109 | if err != nil { |
| 110 | return nil, err |
| 111 | } |
| 112 | |
| 113 | for _, f := range found { |
| 114 | if err := matches.Append(f); err != nil { |
| 115 | return nil, err |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | return matches, nil |
| 120 | }, nil |
| 121 | } |