Debian Codegraph @

execution/execute_search.go

raw symbols SCIP
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 searchExprExecutor(e ast.SearchExpr) (expressionExecutor, error) {
11 var doSearch func(ctx context.Context, options *Options, data *model.Value) ([]*model.Value, error)
12 processValue := func(ctx context.Context, v *model.Value, options *Options) (bool, error) {
13 got, 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 if err != nil {
30 return false, err
31 }
32
33 if got == nil {
34 return false, nil
35 }
36
37 gotV, err := got.BoolValue()
38 if err != nil {
39 return false, err
40 }
41 return gotV, nil
42 }
43 doSearch = func(ctx context.Context, options *Options, data *model.Value) ([]*model.Value, error) {
44 res := make([]*model.Value, 0)
45
46 switch data.Type() {
47 case model.TypeMap:
48 if err := data.RangeMap(func(key string, v *model.Value) error {
49 restore := withKeyVar(options, model.NewStringValue(key))
50 defer restore()
51
52 match, err := processValue(ctx, v, options)
53 if err != nil {
54 return err
55 }
56
57 if match {
58 res = append(res, v)
59 }
60
61 gotNext, err := doSearch(ctx, options, v)
62 if err != nil {
63 return err
64 }
65 res = append(res, gotNext...)
66
67 return nil
68 }); err != nil {
69 return nil, err
70 }
71 case model.TypeSlice:
72 if err := data.RangeSlice(func(i int, v *model.Value) error {
73 restore := withKeyVar(options, model.NewIntValue(int64(i)))
74 defer restore()
75
76 match, err := processValue(ctx, v, options)
77 if err != nil {
78 return err
79 }
80
81 if match {
82 res = append(res, v)
83 }
84
85 gotNext, err := doSearch(ctx, options, v)
86 if err != nil {
87 return err
88 }
89 res = append(res, gotNext...)
90
91 return nil
92 }); err != nil {
93 return nil, err
94 }
95 }
96
97 return res, nil
98 }
99
100 return func(ctx context.Context, options *Options, data *model.Value) (*model.Value, error) {
101 ctx = WithExecutorID(ctx, "searchExpr")
102 matches := model.NewSliceValue()
103
104 found, err := doSearch(ctx, options, data)
105 if err != nil {
106 return nil, err
107 }
108
109 for _, f := range found {
110 if err := matches.Append(f); err != nil {
111 return nil, err
112 }
113 }
114
115 return matches, nil
116 }, nil
117 }