/
tools.go
246 lines (225 loc) · 4.7 KB
/
tools.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
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
package binding
import (
"bytes"
"errors"
"io/ioutil"
"net/http"
"reflect"
"strconv"
_ "unsafe"
)
func copyBody(req *http.Request) ([]byte, error) {
b, err := ioutil.ReadAll(req.Body)
req.Body.Close()
if err != nil {
return nil, err
}
req.Body = ioutil.NopCloser(bytes.NewReader(b))
return b, nil
}
func getParamName(eval func() interface{}, defaultName string) (name string, errStr string) {
name, errStr = evalString(eval)
if errStr == "" || name != "" {
return
}
name = defaultName
return
}
func evalString(eval func() interface{}) (val string, errStr string) {
switch r := eval().(type) {
case string:
return r, ""
case nil:
return "", ""
default:
return "", "parameter position value must be a string type"
}
}
//go:linkname derefType validator.derefType
func derefType(t reflect.Type) reflect.Type
//go:linkname derefValue validator.derefValue
func derefValue(v reflect.Value) reflect.Value
func stringsToBools(a []string) ([]bool, error) {
r := make([]bool, len(a))
for k, v := range a {
i, err := strconv.ParseBool(v)
if err != nil {
return nil, err
}
r[k] = i
}
return r, nil
}
func stringsToFloat32s(a []string) ([]float32, error) {
r := make([]float32, len(a))
for k, v := range a {
i, err := strconv.ParseFloat(v, 32)
if err != nil {
return nil, err
}
r[k] = float32(i)
}
return r, nil
}
func stringsToFloat64s(a []string) ([]float64, error) {
r := make([]float64, len(a))
for k, v := range a {
i, err := strconv.ParseFloat(v, 64)
if err != nil {
return nil, err
}
r[k] = i
}
return r, nil
}
func stringsToInts(a []string) ([]int, error) {
r := make([]int, len(a))
for k, v := range a {
i, err := strconv.Atoi(v)
if err != nil {
return nil, err
}
r[k] = i
}
return r, nil
}
func stringsToInt64s(a []string) ([]int64, error) {
r := make([]int64, len(a))
for k, v := range a {
i, err := strconv.ParseInt(v, 10, 64)
if err != nil {
return nil, err
}
r[k] = i
}
return r, nil
}
func stringsToInt32s(a []string) ([]int32, error) {
r := make([]int32, len(a))
for k, v := range a {
i, err := strconv.ParseInt(v, 10, 32)
if err != nil {
return nil, err
}
r[k] = int32(i)
}
return r, nil
}
func stringsToInt16s(a []string) ([]int16, error) {
r := make([]int16, len(a))
for k, v := range a {
i, err := strconv.ParseInt(v, 10, 16)
if err != nil {
return nil, err
}
r[k] = int16(i)
}
return r, nil
}
func stringsToInt8s(a []string) ([]int8, error) {
r := make([]int8, len(a))
for k, v := range a {
i, err := strconv.ParseInt(v, 10, 8)
if err != nil {
return nil, err
}
r[k] = int8(i)
}
return r, nil
}
func stringsToUint8s(a []string) ([]uint8, error) {
r := make([]uint8, len(a))
for k, v := range a {
i, err := strconv.ParseUint(v, 10, 8)
if err != nil {
return nil, err
}
r[k] = uint8(i)
}
return r, nil
}
func stringsToUint16s(a []string) ([]uint16, error) {
r := make([]uint16, len(a))
for k, v := range a {
i, err := strconv.ParseUint(v, 10, 16)
if err != nil {
return nil, err
}
r[k] = uint16(i)
}
return r, nil
}
func stringsToUint32s(a []string) ([]uint32, error) {
r := make([]uint32, len(a))
for k, v := range a {
i, err := strconv.ParseUint(v, 10, 32)
if err != nil {
return nil, err
}
r[k] = uint32(i)
}
return r, nil
}
func stringsToUint64s(a []string) ([]uint64, error) {
r := make([]uint64, len(a))
for k, v := range a {
i, err := strconv.ParseUint(v, 10, 64)
if err != nil {
return nil, err
}
r[k] = uint64(i)
}
return r, nil
}
func stringsToUints(a []string) ([]uint, error) {
r := make([]uint, len(a))
for k, v := range a {
i, err := strconv.ParseUint(v, 10, 64)
if err != nil {
return nil, err
}
r[k] = uint(i)
}
return r, nil
}
var errMismatch = errors.New("type mismatch")
func stringsToValue(elmeKind reflect.Kind, a []string) (reflect.Value, error) {
var i interface{}
var err error
switch elmeKind {
case reflect.String:
i = a
case reflect.Bool:
i, err = stringsToBools(a)
case reflect.Float32:
i, err = stringsToFloat32s(a)
case reflect.Float64:
i, err = stringsToFloat64s(a)
case reflect.Int:
i, err = stringsToInts(a)
case reflect.Int64:
i, err = stringsToInt64s(a)
case reflect.Int32:
i, err = stringsToInt32s(a)
case reflect.Int16:
i, err = stringsToInt16s(a)
case reflect.Int8:
i, err = stringsToInt8s(a)
case reflect.Uint:
i, err = stringsToUints(a)
case reflect.Uint64:
i, err = stringsToUint64s(a)
case reflect.Uint32:
i, err = stringsToUint32s(a)
case reflect.Uint16:
i, err = stringsToUint16s(a)
case reflect.Uint8:
i, err = stringsToUint8s(a)
default:
return reflect.Value{}, errMismatch
}
if err != nil {
return reflect.Value{}, errMismatch
}
return reflect.ValueOf(i), nil
}