-
Notifications
You must be signed in to change notification settings - Fork 0
/
param_extractor.go
208 lines (178 loc) · 5.72 KB
/
param_extractor.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
package paramex
import (
"errors"
"fmt"
"net/http"
"reflect"
"strconv"
"github.com/google/uuid"
)
type extractorFunc func(key string, array bool) (interface{}, bool)
const (
stringType = string("")
boolType = true
int32Type = int32(0)
intType = int(0)
int64Type = int64(0)
float32Type = float32(0)
float64Type = float64(0)
)
// The Extractor interface is implemented to extract http request headers, form values
// and url query values
type Extractor interface {
// ExtractHeaders extract http headers from sent request and binds to `v`
// `v` should be a Go struct reference
ExtractHeaders(v interface{}, req *http.Request) error
// ExtractQueries extract http url parameters from sent request and binds to v
// `v` should be a Go struct reference
ExtractQueries(v interface{}, req *http.Request) error
// ExtractForms extract http form values from sent request and binds to v
// `v` should be a Go struct reference
ExtractForms(v interface{}, req *http.Request) error
}
type extractor struct{}
// NewParamExtractor returns an Extractor which extract
// req.Header, req.FormValue, req.URL.Query values and
// binds them to a Go struct
func NewParamExtractor() Extractor {
return extractor{}
}
// ExtractHeaders extract http headers from sent request and binds to v
func (p extractor) ExtractHeaders(v interface{}, req *http.Request) error {
return p.extract(v, func(key string, array bool) (interface{}, bool) {
if array {
return nil, false
}
str := req.Header.Get(key)
if str == `` {
return str, false
}
return str, true
})
}
// ExtractQueries extract http url parameters from sent request and binds to v
func (p extractor) ExtractQueries(v interface{}, req *http.Request) error {
return p.extract(v, func(key string, array bool) (interface{}, bool) {
str := req.URL.Query()[key]
if len(str) == 0 {
return "", false
}
if !array {
return str[0], true
}
return str, true
})
}
// ExtractForms extract http form values from sent request and binds to v
func (p extractor) ExtractForms(v interface{}, req *http.Request) error {
err := req.ParseForm()
if err != nil {
return err
}
return p.extract(v, func(key string, array bool) (interface{}, bool) {
str := req.PostForm[key]
if len(str) == 0 {
return "", false
}
if !array {
return str[0], true
}
return str, true
})
}
func (p extractor) extract(v interface{}, keyExtractor extractorFunc) error {
t := reflect.TypeOf(v)
if t.Kind() != reflect.Ptr || v == nil {
return ErrorNotAssignable{
fmt.Errorf(`type of %v is not assignabale, required object reference`, t)}
}
elem := reflect.ValueOf(v).Elem()
if elem.Kind() != reflect.Struct {
return ErrorUnSupportedType{
fmt.Errorf(`type of %v is not extractable, required struct object`, elem.Type().String())}
}
t = elem.Type()
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
tag, ok := field.Tag.Lookup(`param`)
if !ok || tag == `-` {
continue
}
_, ok = keyExtractor(tag, false)
if !ok {
continue
}
switch field.Type {
case reflect.TypeOf(stringType):
valueStr, _ := keyExtractor(tag, false)
elem.Field(i).Set(reflect.ValueOf(valueStr.(string)))
case reflect.TypeOf(boolType):
valueStr, _ := keyExtractor(tag, false)
value, err := strconv.ParseBool(valueStr.(string))
if err != nil {
return ErrorUnmarshalType{
fmt.Errorf(`error unmarshalling [%v] into [bool] due to %v`, valueStr, err)}
}
elem.Field(i).Set(reflect.ValueOf(value))
case reflect.TypeOf(int32Type):
valueStr, _ := keyExtractor(tag, false)
value, err := strconv.Atoi(valueStr.(string))
if err != nil {
return ErrorUnmarshalType{
fmt.Errorf(`error unmarshalling [%v] into [int32] due to %v`, valueStr, err)}
}
elem.Field(i).Set(reflect.ValueOf(int32(value)))
case reflect.TypeOf(intType):
valueStr, _ := keyExtractor(tag, false)
value, err := strconv.Atoi(valueStr.(string))
if err != nil {
return ErrorUnmarshalType{
fmt.Errorf(`error unmarshalling [%v] into [int] due to %v`, valueStr, err)}
}
elem.Field(i).Set(reflect.ValueOf(value))
case reflect.TypeOf(int64Type):
valueStr, _ := keyExtractor(tag, false)
value, err := strconv.ParseInt(valueStr.(string), 10, 64)
if err != nil {
return ErrorUnmarshalType{
fmt.Errorf(`error unmarshalling [%v] into [int64] due to %v`, valueStr, err)}
}
elem.Field(i).Set(reflect.ValueOf(value))
case reflect.TypeOf(float32Type):
valueStr, _ := keyExtractor(tag, false)
value, err := strconv.ParseFloat(valueStr.(string), 32)
if err != nil {
return ErrorUnmarshalType{
fmt.Errorf(`error unmarshalling [%v] into [float32] due to %v`, valueStr, err)}
}
elem.Field(i).Set(reflect.ValueOf(float32(value)))
case reflect.TypeOf(float64Type):
valueStr, _ := keyExtractor(tag, false)
value, err := strconv.ParseFloat(valueStr.(string), 64)
if err != nil {
return ErrorUnmarshalType{
fmt.Errorf(`error unmarshalling [%v] into [float64] due to %v`, valueStr, err)}
}
elem.Field(i).Set(reflect.ValueOf(value))
case reflect.TypeOf(uuid.UUID{}):
valueStr, _ := keyExtractor(tag, false)
value, err := uuid.Parse(valueStr.(string))
if err != nil {
return ErrorUnmarshalType{
fmt.Errorf(`error unmarshalling [%v] into [uuid] due to %v`, valueStr, err)}
}
elem.Field(i).Set(reflect.ValueOf(value))
case reflect.TypeOf([]string{stringType}):
valueStr, ok := keyExtractor(tag, true)
if !ok {
return ErrorUnSupportedParamType{
fmt.Errorf(`error unmarshalling []string into "%v", unsupported param type`, tag),
}
}
elem.Field(i).Set(reflect.ValueOf(valueStr.([]string)))
default:
return ErrorUnSupportedParamType{errors.New(`unsupported param extractor type`)}
}
}
return nil
}