-
Notifications
You must be signed in to change notification settings - Fork 5
/
handler_caller.go
54 lines (47 loc) · 1.05 KB
/
handler_caller.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
package gnext
import (
"reflect"
"runtime/debug"
)
func newHandlerCaller(receiver reflect.Value) *handlerCaller {
return &handlerCaller{
receiver: receiver,
}
}
type handlerCaller struct {
receiver reflect.Value
argBuilders []argBuilder
argSetters []argSetter
errorIndex int
}
func (c *handlerCaller) addSetter(setter argSetter) {
c.argSetters = append(c.argSetters, setter)
}
func (c *handlerCaller) addBuilder(b argBuilder) {
c.argBuilders = append(c.argBuilders, b)
}
func (c *handlerCaller) call(ctx *callContext) {
defer func() {
if e := recover(); e != nil {
errValue := reflect.ValueOf(&HandlerPanicked{
Value: e,
StackTrace: debug.Stack(),
})
ctx.error = &errValue
}
}()
values := make([]reflect.Value, len(c.argBuilders))
for i, builder := range c.argBuilders {
value, err := builder(ctx)
if err != nil {
errValue := reflect.ValueOf(err)
ctx.error = &errValue
return
}
values[i] = value
}
results := c.receiver.Call(values)
for i, setter := range c.argSetters {
setter(&results[i], ctx)
}
}