-
Notifications
You must be signed in to change notification settings - Fork 2
/
handler.go
215 lines (182 loc) · 5.4 KB
/
handler.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
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
package kcd
import (
"fmt"
"net/http"
"reflect"
"runtime"
)
type inputType int
const (
inputTypeResponse inputType = iota
inputTypeRequest
inputTypeInput
)
// Handler returns a default http handler.
//
// The handler may use the following signature:
//
// func([w http.ResponseWriter], [r *http.Request], [input object ptr]) ([output object], error)
//
// inputError and output objects are both optional.
// As such, the minimal accepted signature is:
//
// func(w http.ResponseWriter, r *http.Request) error
//
// The wrapped handler will bind the parameters from the query-string,
// path, body and headers, and handle the errors.
//
// Handler will panic if the kcd handler or its input/output values
// are of incompatible type.
func Handler(h interface{}, defaultSuccessStatusCode int) http.HandlerFunc {
hv := reflect.ValueOf(h)
if hv.Kind() != reflect.Func {
panic(fmt.Sprintf("handler parameters must be a httpHandler, got %T", h))
}
ht := hv.Type()
fundName := fmt.Sprintf("%s", runtime.FuncForPC(hv.Pointer()).Name())
orderInput, in := input(ht, fundName)
out := output(ht, fundName)
var input *reflect.Value = nil
// Wrap http handler.
httpHandler := func(w http.ResponseWriter, r *http.Request) {
// kcd handler has custom input, handle binding.
if in != nil {
i := reflect.New(in)
input = &i
// Bind body
if err := Config.BindHook(w, r, input.Interface()); err != nil {
Config.ErrorHook(w, r, err)
return
}
// Bind query-parameters.
if err := bind(w, r, i, queryTag, Config.QueryExtractor); err != nil {
Config.ErrorHook(w, r, err)
return
}
// Bind path arguments.
if err := bind(w, r, i, pathTag, Config.PathExtractor); err != nil {
Config.ErrorHook(w, r, err)
return
}
// Bind headers.
if err := bind(w, r, i, headerTag, Config.HeaderExtractor); err != nil {
Config.ErrorHook(w, r, err)
return
}
if err := Config.ValidateHook(r.Context(), i.Interface()); err != nil {
Config.ErrorHook(w, r, err)
return
}
}
var err, val interface{}
// funcIn contains the input parameters of the kcd handler call.
var args []reflect.Value
for _, t := range orderInput {
switch t {
case inputTypeInput:
args = append(args, *input)
case inputTypeRequest:
args = append(args, reflect.ValueOf(r))
case inputTypeResponse:
args = append(args, reflect.ValueOf(w))
}
}
ret := hv.Call(args)
if out != nil {
val = ret[0].Interface()
err = ret[1].Interface()
} else {
err = ret[0].Interface()
}
// Handle the error returned by the handler invocation, if any.
if err != nil {
Config.ErrorHook(w, r, err.(error))
return
}
if err := Config.RenderHook(w, r, defaultSuccessStatusCode, val); err != nil {
Config.ErrorHook(w, r, err)
return
}
}
return httpHandler
}
var interfaceResponseWriter = reflect.TypeOf((*http.ResponseWriter)(nil)).Elem()
// input checks the input parameters of a kcd handler
// and return the type of the second parameter, if any.
func input(ht reflect.Type, name string) (orderedInputType []inputType, reflectType reflect.Type) {
n := ht.NumIn()
if n > 3 {
panic(fmt.Sprintf(
"incorrect number of input parameters for handler %s, expected 0 or 3, got %d",
name, n,
))
}
orderedInputType = make([]inputType, 0)
setInputType := map[inputType]bool{}
for i := 0; i < n; i++ {
currentInput := ht.In(i)
if currentInput.Implements(interfaceResponseWriter) {
if _, exist := setInputType[inputTypeResponse]; exist {
panic(fmt.Sprintf(
"invalid parameter %d at handler %s: there is already a http.ResponseWriter parameter",
i, name,
))
}
setInputType[inputTypeResponse] = true
orderedInputType = append(orderedInputType, inputTypeResponse)
} else if currentInput.ConvertibleTo(reflect.TypeOf(&http.Request{})) {
if _, exist := setInputType[inputTypeRequest]; exist {
panic(fmt.Sprintf(
"invalid parameter %d at handler %s: there is already a http.Request parameter",
i, name,
))
}
setInputType[inputTypeRequest] = true
orderedInputType = append(orderedInputType, inputTypeRequest)
} else {
if _, exist := setInputType[inputTypeInput]; exist {
panic(fmt.Sprintf(
"invalid parameter %d at handler %s: there is already the input parameter",
i, name,
))
}
if currentInput.Kind() != reflect.Ptr || currentInput.Elem().Kind() != reflect.Struct {
panic(fmt.Sprintf(
"invalid %d parameter for handler %s, expected pointer to struct, got %v",
n, name, currentInput,
))
}
setInputType[inputTypeInput] = true
orderedInputType = append(orderedInputType, inputTypeInput)
reflectType = currentInput.Elem()
}
}
return orderedInputType, reflectType
}
// output checks the output parameters of a kcd handler
// and return the type of the return type, if any.
func output(ht reflect.Type, name string) reflect.Type {
n := ht.NumOut()
if n < 1 || n > 2 {
panic(fmt.Sprintf(
"incorrect number of output parameters for handler %s, expected 1 or 2, got %d",
name, n,
))
}
// Check the type of the error parameter, which
// should always come last.
if !ht.Out(n - 1).Implements(reflect.TypeOf((*error)(nil)).Elem()) {
panic(fmt.Sprintf(
"unsupported type for handler %s output parameter: expected error interface, got %v",
name, ht.Out(n-1),
))
}
if n == 2 {
t := ht.Out(0)
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
return t
}
return nil
}