-
Notifications
You must be signed in to change notification settings - Fork 0
/
intercept.go
170 lines (155 loc) · 3.75 KB
/
intercept.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
package peony
import (
"fmt"
"reflect"
)
const (
BEFORE int = iota
AFTER
FINALLY
PANIC
)
type Interceptors map[string][]*Interceptor
func NewInterceptors() Interceptors {
return make(map[string][]*Interceptor)
}
type Interceptor struct {
When int
Priority int
Call reflect.Value
NumIn int
Function interface{}
Method interface{}
Target reflect.Type
}
func GetInterceptorFilter(svr *Server) Filter {
return func(c *Controller, filter []Filter) {
// just now can't support interceptor for func action
if c.action.method == nil {
filter[0](c, filter[1:])
return
}
interceptors := svr.Interceptors
defer interceptors.Invoke(c, FINALLY)
interceptors.Invoke(c, BEFORE)
//already get the result
if c.render != nil {
return
}
filter[0](c, filter[1:])
interceptors.Invoke(c, AFTER)
}
}
func (i *Interceptors) Invoke(c *Controller, when int) {
interceptors := i.GetInterceptor(c.action.targetType, when)
for _, interceptor := range interceptors {
rsSlice := interceptor.Invoke(c)
if len(rsSlice) > 0 {
rsVal := rsSlice[0]
rs := rsVal.Interface()
if rs != nil && rsVal.Type().Implements(RendererType) {
c.render = rsSlice[0].Interface().(Renderer)
}
return
}
}
}
func (i *Interceptor) Invoke(c *Controller) []reflect.Value {
var args = []reflect.Value{}
if i.Function != nil {
if i.NumIn == 1 {
//if func have arg controller
args = append(args, reflect.ValueOf(c))
}
} else {
if c.action.method == nil {
ERROR.Println("the action are not method action")
return []reflect.Value{}
}
target := c.action.targetPtr
if i.Target.Kind() != reflect.Ptr {
target = target.Elem()
}
args = append(args, target)
if i.NumIn == 2 {
//if func have arg controller
args = append(args, reflect.ValueOf(c))
}
}
return i.Call.Call(args)
}
//always use original type addr for key.
func (i *Interceptors) genKey(target reflect.Type, when int) string {
if target.Kind() == reflect.Ptr {
return fmt.Sprintf("%p:%i", target.Elem(), when)
}
return fmt.Sprintf("%p:%i", target, when)
}
func (i *Interceptors) AddInterceptor(interceptor *Interceptor) {
key := i.genKey(interceptor.Target, interceptor.When)
interceptors := (*i)[key]
interceptors = append(interceptors, interceptor)
(*i)[key] = interceptors
}
func (i *Interceptors) GetInterceptor(typ reflect.Type, when int) []*Interceptor {
key := i.genKey(typ, when)
return (*i)[key]
}
//intercept function
func (i *Interceptors) InterceptFunc(call interface{}, target interface{}, when int, priority int) {
callVal := reflect.ValueOf(call)
callType := callVal.Type()
numIn := callType.NumIn()
var interceptor *Interceptor
switch {
case callType.Kind() != reflect.Func:
goto FAIL
case numIn > 1:
goto FAIL
case numIn == 1:
callType.In(0)
if callType.In(0) != ControllerPtrType {
goto FAIL
}
}
interceptor = &Interceptor{When: when,
Call: callVal,
Function: call,
NumIn: numIn,
Target: reflect.TypeOf(target),
Priority: priority,
}
i.AddInterceptor(interceptor)
return
FAIL:
ERROR.Fatalln("call must be like func([*Controller]) [Render]")
}
//intercept method
func (i *Interceptors) InterceptMethod(call interface{}, when, priority int) {
callVal := reflect.ValueOf(call)
callType := callVal.Type()
numIn := callType.NumIn()
var interceptor *Interceptor
switch {
case callType.Kind() != reflect.Func:
goto FAIL
case numIn > 2 || numIn < 1:
goto FAIL
case numIn == 2:
callType.In(1)
if callType.In(1) != ControllerPtrType {
goto FAIL
}
}
interceptor = &Interceptor{When: when,
Call: callVal,
Method: call,
NumIn: numIn,
Target: callType.In(0),
Priority: priority,
}
i.AddInterceptor(interceptor)
return
FAIL:
ERROR.Fatalln("call must be like (*Struct).Method([*Controller])")
}