-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunc.go
289 lines (257 loc) · 7.53 KB
/
func.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package typutil
import (
"context"
"encoding/json"
"fmt"
"reflect"
"strings"
)
type requiredArg int
type funcOption func(*Callable)
const Required requiredArg = 1 // Required denotes a default argument that must be specified
// StrictArgs, when passed to Func, forces that function's arguments to be of the right type
func StrictArgs(c *Callable) {
c.strict = true
}
type Callable struct {
fn reflect.Value
cnt int // number of actual args
ctxPos int // pos of ctx argument, or -1
arg []reflect.Type // type used for the argument to the method
def []reflect.Value // default values
variadic bool // is the func's last argument a ...
strict bool
vartyp reflect.Type
}
var (
ctxTyp = reflect.TypeOf((*context.Context)(nil)).Elem()
)
// Func returns a [Callable] object for a func that accepts a context.Context and/or any
// number of arguments
func Func(method any, options ...funcOption) *Callable {
if v, ok := method.(*Callable); ok {
// Func(Func(...)) can be used to apply different options
// for example f := Func(...) f2 := Func(f, StrictArgs)
//
// But the main use is probably to pass a *Callable to a method expecting a func() and using Func() directly
nv := &Callable{}
*nv = *v
for _, opt := range options {
opt(nv)
}
return nv
}
v := reflect.ValueOf(method)
if v.Kind() != reflect.Func {
panic("static method not a method")
}
typ := v.Type()
res := &Callable{fn: v, ctxPos: -1, cnt: typ.NumIn()}
ni := res.cnt
for i := 0; i < ni; i += 1 {
in := typ.In(i)
if in.Implements(ctxTyp) {
if res.ctxPos != -1 {
panic("method taking multiple ctx arguments")
}
res.ctxPos = i
res.cnt -= 1
continue
}
res.arg = append(res.arg, in)
}
if typ.IsVariadic() {
res.variadic = true
ln := len(res.arg)
res.vartyp = res.arg[ln-1].Elem() // last argument is an array []...
res.arg = res.arg[:ln-1]
}
for _, opt := range options {
opt(res)
}
return res
}
// String returns a string representation of the function and its arguments
func (s *Callable) String() string {
var args []string
for _, arg := range s.arg {
args = append(args, arg.String())
}
if s.variadic {
args = append(args, "..."+s.vartyp.String())
}
return "func(" + strings.Join(args, ", ") + ")"
}
// WithDefaults sets default arguments for the given callable that will be used when a call doesn't have
// enough arguments to call the method. It will panic if there aren't enough arguments provided or if a
// value is not compatible with the function's expected argument type.
//
// Example:
//
// func myFunc(a, b, c int) { ... }
// f := Func(myFunc).WithDefaults(typutil.Required, typutil.Required, 42) // c defaults to 42
// f.CallArg(context.Background(), 10, 20) // equivalent to myFunc(10, 20, 42)
func (s *Callable) WithDefaults(args ...any) *Callable {
// build def
if len(args) < s.cnt {
panic("WithDefaults requires at least the same number of arguments as the function")
}
if len(args) > s.cnt && !s.variadic {
panic(ErrTooManyArgs)
}
def := make([]reflect.Value, len(args))
for argN, arg := range args {
if arg == Required {
// keep this as an invalid value
continue
}
var argV reflect.Value
if argN >= len(s.arg) {
argV = reflect.New(s.vartyp)
} else {
argV = reflect.New(s.arg[argN])
}
if s.strict {
argV.Set(reflect.ValueOf(arg))
} else {
err := AssignReflect(argV, reflect.ValueOf(arg))
if err != nil {
panic(err)
}
}
def[argN] = argV.Elem()
}
res := &Callable{}
*res = *s
res.def = def
return res
}
// Call invokes the func without any argument. If the func expects some kind of argument, Call will attempt
// to get input_json from the context, and if obtained it will be parsed and passed as argument to the method.
func (s *Callable) Call(ctx context.Context) (any, error) {
// call this function, typically fetching request body from the context via input_json
if s.cnt > 0 {
// grab input json, call json.Unmarshal on argV
input, ok := ctx.Value("input_json").(json.RawMessage)
if ok {
if s.cnt > 1 {
// if the method take multiple arguments, the json value must be an array. By using RawMessage we
// ensure we only parse the array part here, and not the contents, so it can be parsed for each
// relevant argument type directly
var args []RawJsonMessage
err := json.Unmarshal(input, &args)
if err != nil {
return nil, err
}
anyArgs := make([]any, len(args))
for n, v := range args {
anyArgs[n] = v
}
return s.CallArg(ctx, anyArgs...)
}
return s.CallArg(ctx, RawJsonMessage(input))
}
}
return s.CallArg(ctx)
}
// CallArg calls the method with the specified arguments. If less arguments are provided than required, an error will be raised.
func (s *Callable) CallArg(ctx context.Context, arg ...any) (any, error) {
if s.cnt == 0 {
// no args, ignore any input
var args []reflect.Value
if s.ctxPos == 0 {
args = append(args, reflect.ValueOf(ctx))
}
return s.parseResult(s.fn.Call(args))
}
if len(arg) < s.cnt && s.def == nil {
// not enough arguments to cover cnt
return nil, ErrMissingArgs
}
if len(arg) > s.cnt && !s.variadic {
return nil, ErrTooManyArgs
}
// call this function but pass arg values
var args []reflect.Value
var ctxPos int
var ctxCnt int
if s.ctxPos != -1 {
args = make([]reflect.Value, len(arg)+1)
args[s.ctxPos] = reflect.ValueOf(ctx)
ctxPos = s.ctxPos
ctxCnt = 1
} else {
args = make([]reflect.Value, len(arg))
ctxPos = len(arg) + 1
}
for argN, v := range arg {
var argV reflect.Value
if argN >= len(s.arg) {
argV = reflect.New(s.vartyp)
} else {
argV = reflect.New(s.arg[argN])
}
if s.strict {
subv := reflect.ValueOf(v)
if !subv.Type().AssignableTo(argV.Type()) {
return nil, ErrAssignImpossible
}
argV.Set(reflect.ValueOf(v))
} else {
err := AssignReflect(argV, reflect.ValueOf(v))
if err != nil {
return nil, err
}
}
if argN >= ctxPos {
args[argN+1] = argV.Elem()
} else {
args[argN] = argV.Elem()
}
}
if len(args)-ctxCnt < len(s.def) {
add := s.def[len(args)-ctxCnt:]
for _, v := range add {
if !v.IsValid() {
return nil, ErrMissingArgs
}
}
args = append(args, add...)
}
return s.parseResult(s.fn.Call(args))
}
// IsStringArg returns true if the nth argument of the callable is a string, or a type related to string
func (s *Callable) IsStringArg(n int) bool {
return s.ArgKind(n) == reflect.String
}
// ArgKind returns the kind for the nth argument. reflect.Invalid will be returned if there is no such argument
func (s *Callable) ArgKind(n int) reflect.Kind {
if n >= len(s.arg) {
return reflect.Invalid
}
return s.arg[n].Kind()
}
var errTyp = reflect.TypeOf((*error)(nil)).Elem()
func (s *Callable) parseResult(res []reflect.Value) (output any, err error) {
// for each value in res, try to find which one is an error and which one is a result
for _, v := range res {
if v.Type().Implements(errTyp) {
err, _ = v.Interface().(error)
continue
}
output = v.Interface()
}
return
}
// Call will call the callable with the provided arguments, and cast the return type to the specified
// type automatically. If the return type is not of the correct type, an error will be returned, unless
// there was already an error.
func Call[T any](s *Callable, ctx context.Context, arg ...any) (T, error) {
res, err := s.CallArg(ctx, arg...)
if v, ok := res.(T); ok {
return v, err
} else if err == nil {
err = fmt.Errorf("%w: %T", ErrDifferentType, res)
}
return reflect.New(reflect.TypeFor[T]()).Elem().Interface().(T), err
}