-
Notifications
You must be signed in to change notification settings - Fork 55
/
inject.go
60 lines (52 loc) · 975 Bytes
/
inject.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
package zpool
import (
"reflect"
"github.com/sohaha/zlsgo/zdi"
)
type (
invokerPre func() error
)
func (i invokerPre) Invoke(_ []interface{}) ([]reflect.Value, error) {
return nil, i()
}
var (
_ zdi.PreInvoker = (*invokerPre)(nil)
)
func invokeHandler(v []reflect.Value, err error) error {
if err != nil {
return err
}
for i := range v {
val := v[i].Interface()
switch e := val.(type) {
case error:
return e
}
}
return nil
}
func (wp *WorkPool) Injector() zdi.TypeMapper {
return wp.injector
}
func (wp *WorkPool) handlerFunc(h Task) (fn taskfn) {
switch v := h.(type) {
case func():
return func() error {
v()
return nil
}
case func() error:
return func() error {
return invokeHandler(wp.injector.Invoke(invokerPre(v)))
}
case zdi.PreInvoker:
return func() error {
err := invokeHandler(wp.injector.Invoke(v))
return err
}
default:
return func() error {
return invokeHandler(wp.injector.Invoke(v))
}
}
}