| 1 | package execution |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | |
| 7 | "github.com/tomwright/dasel/v3/model" |
| 8 | ) |
| 9 | |
| 10 | var ( |
| 11 | // DefaultFuncCollection is the default collection of functions that can be executed. |
| 12 | DefaultFuncCollection = NewFuncCollection( |
| 13 | FuncLen, |
| 14 | FuncAdd, |
| 15 | FuncToString, |
| 16 | FuncToInt, |
| 17 | FuncToFloat, |
| 18 | FuncMerge, |
| 19 | FuncReverse, |
| 20 | FuncTypeOf, |
| 21 | FuncMax, |
| 22 | FuncMin, |
| 23 | FuncIgnore, |
| 24 | FuncBase64Encode, |
| 25 | FuncBase64Decode, |
| 26 | FuncParse, |
| 27 | FuncReadFile, |
| 28 | FuncHas, |
| 29 | FuncGet, |
| 30 | FuncContains, |
| 31 | FuncSum, |
| 32 | FuncJoin, |
| 33 | FuncReplace, |
| 34 | FuncKeys, |
| 35 | FuncSplit, |
| 36 | FuncToLower, |
| 37 | FuncToUpper, |
| 38 | FuncTrim, |
| 39 | FuncTrimPrefix, |
| 40 | FuncTrimSuffix, |
| 41 | FuncStartsWith, |
| 42 | FuncEndsWith, |
| 43 | FuncIndexOf, |
| 44 | FuncAbs, |
| 45 | FuncFloor, |
| 46 | FuncCeil, |
| 47 | FuncRound, |
| 48 | FuncAvg, |
| 49 | FuncFlatten, |
| 50 | FuncUnique, |
| 51 | FuncFirst, |
| 52 | FuncLast, |
| 53 | FuncValues, |
| 54 | FuncEntries, |
| 55 | FuncFromEntries, |
| 56 | FuncToBool, |
| 57 | FuncStringify, |
| 58 | ) |
| 59 | ) |
| 60 | |
| 61 | // ArgsValidator is a function that validates the arguments passed to a function. |
| 62 | type ArgsValidator func(ctx context.Context, name string, args model.Values) error |
| 63 | |
| 64 | // ValidateArgsExactly returns an ArgsValidator that validates that the number of arguments passed to a function is exactly the expected number. |
| 65 | func ValidateArgsExactly(expected int) ArgsValidator { |
| 66 | return func(ctx context.Context, name string, args model.Values) error { |
| 67 | if len(args) == expected { |
| 68 | return nil |
| 69 | } |
| 70 | return fmt.Errorf("func %q expects exactly %d arguments, got %d", name, expected, len(args)) |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | // ValidateArgsMin returns an ArgsValidator that validates that the number of arguments passed to a function is at least the expected number. |
| 75 | func ValidateArgsMin(expected int) ArgsValidator { |
| 76 | return func(ctx context.Context, name string, args model.Values) error { |
| 77 | if len(args) >= expected { |
| 78 | return nil |
| 79 | } |
| 80 | return fmt.Errorf("func %q expects at least %d arguments, got %d", name, expected, len(args)) |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | // ValidateArgsMax returns an ArgsValidator that validates that the number of arguments passed to a function is at most the expected number. |
| 85 | func ValidateArgsMax(expected int) ArgsValidator { |
| 86 | return func(ctx context.Context, name string, args model.Values) error { |
| 87 | if len(args) <= expected { |
| 88 | return nil |
| 89 | } |
| 90 | return fmt.Errorf("func %q expects no more than %d arguments, got %d", name, expected, len(args)) |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | // ValidateArgsMinMax returns an ArgsValidator that validates that the number of arguments passed to a function is between the min and max expected numbers. |
| 95 | func ValidateArgsMinMax(min int, max int) ArgsValidator { |
| 96 | return func(ctx context.Context, name string, args model.Values) error { |
| 97 | if len(args) >= min && len(args) <= max { |
| 98 | return nil |
| 99 | } |
| 100 | return fmt.Errorf("func %q expects between %d and %d arguments, got %d", name, min, max, len(args)) |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | // Func represents a function that can be executed. |
| 105 | type Func struct { |
| 106 | name string |
| 107 | handler FuncFn |
| 108 | argsValidator ArgsValidator |
| 109 | } |
| 110 | |
| 111 | // Handler returns a FuncFn that can be used to execute the function. |
| 112 | func (f *Func) Handler() FuncFn { |
| 113 | return func(ctx context.Context, data *model.Value, args model.Values) (*model.Value, error) { |
| 114 | if f.argsValidator != nil { |
| 115 | if err := f.argsValidator(ctx, f.name, args); err != nil { |
| 116 | return nil, err |
| 117 | } |
| 118 | } |
| 119 | res, err := f.handler(ctx, data, args) |
| 120 | if err != nil { |
| 121 | return nil, fmt.Errorf("error execution func %q: %w", f.name, err) |
| 122 | } |
| 123 | return res, nil |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | // NewFunc creates a new Func. |
| 128 | func NewFunc(name string, handler FuncFn, argsValidator ArgsValidator) *Func { |
| 129 | return &Func{ |
| 130 | name: name, |
| 131 | handler: handler, |
| 132 | argsValidator: argsValidator, |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | // FuncFn is a function that can be executed. |
| 137 | type FuncFn func(ctx context.Context, data *model.Value, args model.Values) (*model.Value, error) |
| 138 | |
| 139 | // FuncCollection is a collection of functions that can be executed. |
| 140 | type FuncCollection map[string]FuncFn |
| 141 | |
| 142 | // NewFuncCollection creates a new FuncCollection with the given functions. |
| 143 | func NewFuncCollection(funcs ...*Func) FuncCollection { |
| 144 | return FuncCollection{}.Register(funcs...) |
| 145 | } |
| 146 | |
| 147 | // Register registers the given functions with the FuncCollection. |
| 148 | func (fc FuncCollection) Register(funcs ...*Func) FuncCollection { |
| 149 | for _, f := range funcs { |
| 150 | fc[f.name] = f.Handler() |
| 151 | } |
| 152 | return fc |
| 153 | } |
| 154 | |
| 155 | // Get returns the function with the given name. |
| 156 | func (fc FuncCollection) Get(name string) (FuncFn, bool) { |
| 157 | fn, ok := fc[name] |
| 158 | return fn, ok |
| 159 | } |
| 160 | |
| 161 | // Delete deletes the functions with the given names. |
| 162 | func (fc FuncCollection) Delete(names ...string) FuncCollection { |
| 163 | for _, name := range names { |
| 164 | delete(fc, name) |
| 165 | } |
| 166 | return fc |
| 167 | } |
| 168 | |
| 169 | // Copy returns a copy of the FuncCollection. |
| 170 | func (fc FuncCollection) Copy() FuncCollection { |
| 171 | c := NewFuncCollection() |
| 172 | for k, v := range fc { |
| 173 | c[k] = v |
| 174 | } |
| 175 | return c |
| 176 | } |