-
Notifications
You must be signed in to change notification settings - Fork 51
/
invoke_result.go
96 lines (70 loc) · 1.67 KB
/
invoke_result.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
package aop
import (
"reflect"
"sync"
"github.com/gogap/errors"
)
type Result []reflect.Value
type Args []interface{}
func (p Result) MapTo(fn interface{}) {
mapTo(fn, p)
}
func (p Args) MapTo(fn interface{}) {
values := []reflect.Value{}
for _, arg := range p {
values = append(values, reflect.ValueOf(arg))
}
mapTo(fn, values)
}
func mapTo(fn interface{}, values []reflect.Value) {
fnType := reflect.TypeOf(fn)
if fnType.Kind() != reflect.Func {
panic(ErrMapperArgShouldBeFunc.New())
}
if fnType.NumIn() != len(values) {
panic(ErrWrongMapFuncArgsNum.New())
}
fnValue := reflect.ValueOf(fn)
fnValue.Call(values)
}
type InvokeResult struct {
beanID string
methodName string
values []reflect.Value
err error
callOnce sync.Once
called bool
}
func (p *InvokeResult) End(callback ...interface{}) (err error) {
if p.called {
return ErrEndInvokeTwice.New(errors.Params{"id": p.beanID, "method": p.methodName})
}
if p.err != nil {
return p.err
}
p.callOnce.Do(func() {
p.called = true
if callback == nil || len(callback) == 0 {
return
}
cbType := reflect.TypeOf(callback[0])
if cbType.Kind() != reflect.Func {
panic(ErrEndInvokeParamsIsNotFunc.New(errors.Params{"id": p.beanID, "method": p.methodName}))
}
if cbType.NumIn() != len(p.values) {
panic(ErrWrongEndInvokeFuncArgsNum.New(errors.Params{"id": p.beanID, "method": p.methodName}))
}
cbValue := reflect.ValueOf(callback[0])
cbValue.Call(p.values)
})
return
}
func (p *InvokeResult) MethodName() string {
return p.methodName
}
func (p *InvokeResult) BeanID() string {
return p.beanID
}
func (p *InvokeResult) Error() error {
return p.err
}