-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasync.go
328 lines (282 loc) · 8.48 KB
/
async.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package async
import (
"context"
"reflect"
"github.com/ghosind/go-try"
)
// AsyncFn is the function to run, the function can be a function without any restriction that
// accepts any parameters and any return values. For the best practice, please define the function
// like the following styles:
//
// func(context.Context) error
// func(context.Context) (out_type, error)
// func(context.Context, in_type) error
// func(context.Context, in_type) (out_type, error)
// func(context.Context, in_type1, in_type2/*, ...*/) (out_type1, out_type_2,/* ...,*/ error)
type AsyncFn any
// executeResult indicates the execution result whether the function returns an error or panic, and
// the index of the function in the parameters list.
type executeResult struct {
// Error is the execution result of the function, it will be nil if the function does not return
// an error and does not panic.
Error error
// Index is the index of the function in the parameters list.
Index int
// Out is an array to store the return values without the last error.
Out []any
}
// empty is a smallest cost struct.
type empty struct{}
// contextType is the reflect type of context.Context.
var contextType reflect.Type = reflect.TypeOf((*context.Context)(nil)).Elem()
// errorType is the reflect type of an error
var errorType reflect.Type = reflect.TypeOf((*error)(nil)).Elem()
// getContext returns the specified non-nil context from the parameter, or creates and returns a
// new empty context.
func getContext(ctx context.Context) context.Context {
if ctx != nil {
return ctx
}
return context.Background()
}
// validateAsyncFuncs validates the functions list, and it will panic if any function is nil or not
// a function.
func validateAsyncFuncs(funcs ...AsyncFn) {
for _, fn := range funcs {
if fn == nil || reflect.TypeOf(fn).Kind() != reflect.Func {
panic(ErrNotFunction)
}
}
}
// isContextType returns a boolean value to indicates whether the type is context or not.
func isContextType(ty reflect.Type) bool {
return ty.Kind() == reflect.Interface &&
ty.Implements(contextType) && contextType.Implements(ty)
}
// isFuncTakesContexts checks the function takes Contexts as the arguments.
func isFuncTakesContexts(fn reflect.Type) (bool, int) {
if fn.NumIn() <= 0 {
return false, 0
}
hasContext := false
contextNum := 0
for i := 0; i < fn.NumIn(); i++ {
ok := isContextType(fn.In(i))
if ok {
hasContext = true
contextNum++
} else {
break
}
}
return hasContext, contextNum
}
// isFuncReturnsError checks the last return value of the function is an error if the function
// returns some values.
func isFuncReturnsError(fn reflect.Type) bool {
if fn.NumOut() <= 0 {
return false
}
out := fn.Out(fn.NumOut() - 1)
if out.Kind() != reflect.Interface || !out.Implements(errorType) || !errorType.Implements(out) {
return false
}
return true
}
// isFirstParamContext checks the any type slice, and return true if the first element in the slice
// is a context object.
func isFirstParamContext(params []any, numIn int) bool {
if len(params) == 0 || len(params) < numIn {
return false
}
ty := reflect.TypeOf(params[0])
return ty == nil || ty.Implements(contextType)
}
// invokeAsyncFn tries to call the function with the specified parameters, and it'll also set the
// context if it is the function's first parameter. After the function is finished, it will return
// a return values array and the error. It will store the return values into the out array without
// the error if it is the last return value.
func invokeAsyncFn(fn AsyncFn, ctx context.Context, params []any) ([]any, error) {
fv := reflect.ValueOf(fn)
ft := fv.Type()
var out []reflect.Value
in := makeFuncIn(ft, ctx, params)
numRet := ft.NumOut()
ret := make([]any, numRet)
_, err := try.Try(func() {
out = fv.Call(in)
})
if err != nil {
for i := 0; i < numRet; i++ {
ret[i] = reflect.Zero(ft.Out(i)).Interface()
}
return ret, err
}
if isFuncReturnsError(ft) {
if out[numRet-1].IsNil() {
err = nil
} else {
err = out[numRet-1].Interface().(error)
// double check if the error is a custom error pointer
if err == nil || reflect.ValueOf(err).IsNil() {
err = nil
}
}
}
for i := 0; i < numRet; i++ {
ret[i] = out[i].Interface()
}
return ret, err
}
// makeFuncIn makes a reflected values list of the parameters to call the function.
func makeFuncIn(ft reflect.Type, ctx context.Context, params []any) []reflect.Value {
isTakeContext, _ := isFuncTakesContexts(ft)
isContextParam := isTakeContext && isFirstParamContext(params, ft.NumIn())
if !ft.IsVariadic() {
return makeNonVariadicFuncIn(ft, ctx, params, isTakeContext, isContextParam)
} else {
return makeVariadicFuncIn(ft, ctx, params, isTakeContext, isContextParam)
}
}
// makeVariadicFuncIn checks the parameters of the variadic function and the params slice from the
// caller, and returns a reflect.Value slice of the input parameters. It'll prepend the context to
// the parameter list if the function's first parameter is a context and the first element in the
// parameter list is not a context.
func makeVariadicFuncIn(
ft reflect.Type,
ctx context.Context,
params []any,
isTakeContext, isContextParam bool,
) []reflect.Value {
ftNumIn := ft.NumIn() - 1
numIn := len(params)
if isTakeContext && !isContextParam {
ftNumIn--
numIn++
}
if len(params) < ftNumIn {
panic(ErrUnmatchedParam)
}
ftNumIn = ft.NumIn() - 1
lastType := ft.In(ftNumIn).Elem()
in := make([]reflect.Value, numIn)
i := 0
if isTakeContext && !isContextParam {
in[i] = reflect.ValueOf(ctx)
i++
}
for j := 0; i < ftNumIn || j < len(params); j++ {
v := params[j]
vt := reflect.TypeOf(v)
vv := reflect.ValueOf(v)
it := lastType
if i < ftNumIn {
it = ft.In(i)
}
if vt != it {
if vt != nil && vt.ConvertibleTo(it) {
vv = vv.Convert(it)
} else if v == nil {
kind := it.Kind()
switch kind {
case reflect.Chan, reflect.Map, reflect.Pointer, reflect.UnsafePointer,
reflect.Interface, reflect.Slice:
vv = reflect.Zero(it)
default:
panic(ErrUnmatchedParam)
}
} else {
panic(ErrUnmatchedParam)
}
}
in[i] = vv
i++
}
return in
}
// makeNonVariadicFuncIn checks the parameters of the non-variadic function and the params slice
// from the caller, and returns a reflect.Value slice of the input parameters. It'll prepend the
// context to the parameter list if the function's first parameter is a context and the first
// element in the parameter list is not a context.
//
// The function will panic an unmatched param error if the number of parameters for the function is
// greater to the specified parameters list, or some elements' types of parameters are not match.
func makeNonVariadicFuncIn(
ft reflect.Type,
ctx context.Context,
params []any,
isTakeContext, isContextParam bool,
) []reflect.Value {
numIn := ft.NumIn()
if isTakeContext && !isContextParam {
numIn--
}
if numIn > len(params) {
panic(ErrUnmatchedParam)
}
in := make([]reflect.Value, ft.NumIn())
i := 0 // index of the input parameter list
if isTakeContext && !isContextParam {
// prepend context to the input parameter list
in[i] = reflect.ValueOf(ctx)
i++
numIn++
}
for j := 0; i < numIn; j++ {
v := params[j]
vt := reflect.TypeOf(v) // the type of the value
vv := reflect.ValueOf(v)
it := ft.In(i) // the type in the parameter list
if vt != it {
// if the value's type does not match the parameter list, try to convert it first
if vt != nil && vt.ConvertibleTo(it) {
vv = vv.Convert(it)
} else if v == nil {
// check the parameter's type is whether nil-able or not when the value is nil
kind := it.Kind()
switch kind {
case reflect.Chan, reflect.Map, reflect.Pointer, reflect.UnsafePointer,
reflect.Interface, reflect.Slice:
vv = reflect.Zero(it)
default:
panic(ErrUnmatchedParam)
}
} else {
panic(ErrUnmatchedParam)
}
}
in[i] = vv
i++
}
return in
}
// isValidNextFunc checks the current function's return values and the next function's parameters,
// and returns a boolean value to indicates whether the functions are match or not
func isValidNextFunc(cur, next reflect.Type) bool {
isTakeContext, _ := isFuncTakesContexts(next)
numOut := cur.NumOut()
numIn := next.NumIn()
if isTakeContext {
numIn--
}
if numOut < numIn {
return false
}
i := 0
j := 0
if isTakeContext {
if numOut > 0 && isContextType(cur.Out(0)) {
i++
}
numIn++
j++
}
for i < numOut && j < numIn {
if cur.Out(i) != next.In(j) {
return false
}
i++
j++
}
return true
}