-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdispatch.go
More file actions
296 lines (251 loc) · 6.38 KB
/
dispatch.go
File metadata and controls
296 lines (251 loc) · 6.38 KB
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
package rivet
import (
"io"
"net/http"
"reflect"
"unsafe"
)
var (
idString = TypePointerOf([]string{})
idBytes = TypePointerOf([][]byte{})
idError = TypePointerOf([]error{})
idBool = TypePointerOf([]bool{})
)
// Dispatcher 接口用于派发
type Dispatcher interface {
// IsInjector 返回 true, 表示使用 Dispatch 方法派发, 否则使用 Hand 派发
IsInjector() bool
Dispatch(c *Context) bool
Hand(Params, http.ResponseWriter, *http.Request) bool
}
type dispatchs struct {
queue []Dispatcher
isInjector bool
}
func (ds dispatchs) IsInjector() bool { return ds.isInjector }
func (ds dispatchs) Dispatch(c *Context) bool {
for _, d := range ds.queue {
if d.IsInjector() {
if !d.Dispatch(c) {
return false
}
} else if !d.Hand(c.Params, c.Res, c.Req) {
return false
}
}
return true
}
func (ds dispatchs) Hand(p Params, rw http.ResponseWriter, req *http.Request) bool {
for _, d := range ds.queue {
if !d.IsInjector() && !d.Hand(p, rw, req) {
return false
}
}
return true
}
// 注入反射调用
type dispatcher struct {
fn reflect.Value
in []unsafe.Pointer
out []unsafe.Pointer
isVariadic bool
}
func (d dispatcher) IsInjector() bool { return true }
func (d dispatcher) Hand(Params, http.ResponseWriter, *http.Request) bool { return true }
func (d dispatcher) Dispatch(c *Context) bool {
var (
v interface{}
has bool
out []reflect.Value
)
in := make([]reflect.Value, len(d.in))
for i := 0; i < len(d.in); i++ {
v, has = c.Pick(d.in[i])
if !has {
return false
}
in[i] = reflect.ValueOf(v)
}
if d.isVariadic {
out = d.fn.CallSlice(in)
} else {
out = d.fn.Call(in)
}
if d.out == nil {
return true
}
switch d.out[0] {
case idString:
io.WriteString(c.Res, out[0].String())
case idBytes:
c.Res.Write(out[0].Bytes())
case idError:
if !out[0].IsNil() {
err, ok := out[0].Interface().(error)
if ok {
HandleError(err, c.Res, c.Req)
return false
}
}
case idBool:
if !out[0].Bool() {
return false
}
}
if len(d.out) == 1 || out[1].IsNil() {
return true
}
err, ok := out[1].Interface().(error)
if ok && err != nil {
HandleError(err, c.Res, c.Req)
return false
}
return true
}
type dispatchContext struct {
c func(*Context)
r func(*Context) bool
}
func (d dispatchContext) IsInjector() bool { return true }
func (d dispatchContext) Hand(Params, http.ResponseWriter, *http.Request) bool { return true }
func (d dispatchContext) Dispatch(c *Context) bool {
if d.c == nil {
return d.r(c)
}
d.c(c)
return true
}
type dispatchHandle struct {
c func(http.ResponseWriter, *http.Request)
r func(http.ResponseWriter, *http.Request) bool
}
func (d dispatchHandle) IsInjector() bool { return false }
func (d dispatchHandle) Dispatch(_ *Context) bool { return true }
func (d dispatchHandle) Hand(_ Params, rw http.ResponseWriter, req *http.Request) bool {
if d.c == nil {
return d.r(rw, req)
}
d.c(rw, req)
return true
}
type dispatchParams struct {
c func(Params, http.ResponseWriter, *http.Request)
r func(Params, http.ResponseWriter, *http.Request) bool
}
func (d dispatchParams) IsInjector() bool { return false }
func (d dispatchParams) Dispatch(_ *Context) bool { return true }
func (d dispatchParams) Hand(p Params, rw http.ResponseWriter, req *http.Request) bool {
if d.c == nil {
return d.r(p, rw, req)
}
d.c(p, rw, req)
return true
}
type dispatchHandler struct {
http.Handler
}
func (d dispatchHandler) IsInjector() bool { return false }
func (d dispatchHandler) Dispatch(_ *Context) bool { return true }
func (d dispatchHandler) Hand(_ Params, rw http.ResponseWriter, req *http.Request) bool {
d.ServeHTTP(rw, req)
return true
}
type dispatchEmpty func()
func (d dispatchEmpty) IsInjector() bool { return false }
func (d dispatchEmpty) Dispatch(_ *Context) bool { return true }
func (d dispatchEmpty) Hand(_ Params, _ http.ResponseWriter, _ *http.Request) bool {
d()
return true
}
type dispatch struct {
i interface{}
}
func (d dispatch) IsInjector() bool { return true }
func (d dispatch) Hand(Params, http.ResponseWriter, *http.Request) bool { return true }
func (d dispatch) Dispatch(c *Context) bool {
c.Map(d.i)
return true
}
// ToDispatcher 包装 handler 为 Dispatcher.
// 特别的, 如果 handler 函数中包含 Store 类型参数
func ToDispatcher(handler ...interface{}) Dispatcher {
var fun reflect.Value
var withContext bool
ds := make([]Dispatcher, 0)
for _, i := range handler {
if i == nil {
continue
}
switch d := i.(type) {
case func(*Context):
withContext = true
ds = append(ds, dispatchContext{c: d})
continue
case func():
ds = append(ds, dispatchEmpty(d))
continue
case func(http.ResponseWriter, *http.Request):
ds = append(ds, dispatchHandle{c: d})
continue
case func(Params, http.ResponseWriter, *http.Request):
ds = append(ds, dispatchParams{c: d})
continue
case func(*Context) bool:
withContext = true
ds = append(ds, dispatchContext{r: d})
continue
case func(http.ResponseWriter, *http.Request) bool:
ds = append(ds, dispatchHandle{r: d})
continue
case func(Params, http.ResponseWriter, *http.Request) bool:
ds = append(ds, dispatchParams{r: d})
continue
// Dispatcher 接口优先于其它接口.
case Dispatcher:
if d.IsInjector() {
withContext = true
}
ds = append(ds, d)
continue
case http.Handler:
ds = append(ds, dispatchHandler{d})
continue
default:
fun = reflect.ValueOf(i)
if fun.Kind() != reflect.Func {
ds = append(ds, dispatch{i})
continue
}
}
t := fun.Type()
d := dispatcher{
fn: fun,
in: make([]unsafe.Pointer, t.NumIn()),
isVariadic: t.IsVariadic(),
}
for i := 0; i < t.NumIn(); i++ {
d.in[i] = TypePointerOf(t.In(i))
if d.in[i] == idContext {
withContext = true
}
}
if t.NumOut() > 0 && t.NumOut() <= 2 {
out := make([]unsafe.Pointer, t.NumOut())
for i := 0; i < t.NumOut(); i++ {
out[i] = TypePointerOf(t.Out(i))
}
if out[0] == idError || out[0] == idString || out[0] == idBytes || out[0] == idBool ||
len(out) == 2 && out[1] == idError {
d.out = out
}
}
ds = append(ds, d)
}
if len(ds) == 0 {
return nil
}
if len(ds) == 1 {
return ds[0]
}
return dispatchs{queue: ds, isInjector: withContext}
}