This repository has been archived by the owner on Feb 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathquery.go
353 lines (312 loc) · 8.49 KB
/
query.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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
// Copyright 2019 The Tango Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package tango
import (
"html/template"
"net/http"
"net/url"
"strconv"
)
// Queries a new enhancement of http.Request
type Queries http.Request
var _ Set = &Queries{}
// Values returns http.Request values
func (f *Queries) Values() url.Values {
return (*http.Request)(f).URL.Query()
}
// String returns request form as string
func (f *Queries) String(key string) (string, error) {
if v, ok := f.Values()[key]; ok {
return v[0], nil
}
return "", ErrorKeyIsNotExist{key}
}
// Strings returns request form as strings
func (f *Queries) Strings(key string) ([]string, error) {
if v, ok := f.Values()[key]; ok {
return v, nil
}
return nil, ErrorKeyIsNotExist{key}
}
// Escape returns request form as escaped string
func (f *Queries) Escape(key string) (string, error) {
s, err := f.String(key)
if err != nil {
return "", err
}
return template.HTMLEscapeString(s), nil
}
// Int returns request form as int
func (f *Queries) Int(key string) (int, error) {
s, err := f.String(key)
if err != nil {
return 0, err
}
return strconv.Atoi(s)
}
// Int32 returns request form as int32
func (f *Queries) Int32(key string) (int32, error) {
s, err := f.String(key)
if err != nil {
return 0, err
}
v, err := strconv.ParseInt(s, 10, 32)
return int32(v), err
}
// Int64 returns request form as int64
func (f *Queries) Int64(key string) (int64, error) {
s, err := f.String(key)
if err != nil {
return 0, err
}
v, err := strconv.ParseInt(s, 10, 64)
return v, err
}
// Uint returns request form as uint
func (f *Queries) Uint(key string) (uint, error) {
s, err := f.String(key)
if err != nil {
return 0, err
}
v, err := strconv.ParseUint(s, 10, 64)
return uint(v), err
}
// Uint32 returns request form as uint32
func (f *Queries) Uint32(key string) (uint32, error) {
s, err := f.String(key)
if err != nil {
return 0, err
}
v, err := strconv.ParseUint(s, 10, 32)
return uint32(v), err
}
// Uint64 returns request form as uint64
func (f *Queries) Uint64(key string) (uint64, error) {
s, err := f.String(key)
if err != nil {
return 0, err
}
return strconv.ParseUint(s, 10, 64)
}
// Bool returns request form as bool
func (f *Queries) Bool(key string) (bool, error) {
s, err := f.String(key)
if err != nil {
return false, err
}
return strconv.ParseBool(s)
}
// Float32 returns request form as float32
func (f *Queries) Float32(key string) (float32, error) {
s, err := f.String(key)
if err != nil {
return 0, err
}
v, err := strconv.ParseFloat(s, 64)
return float32(v), err
}
// Float64 returns request form as float64
func (f *Queries) Float64(key string) (float64, error) {
s, err := f.String(key)
if err != nil {
return 0, err
}
return strconv.ParseFloat(s, 64)
}
// MustString returns request form as string with default
func (f *Queries) MustString(key string, defaults ...string) string {
if v, ok := f.Values()[key]; ok {
return v[0]
}
if len(defaults) > 0 {
return defaults[0]
}
return ""
}
// MustStrings returns request form as strings with default
func (f *Queries) MustStrings(key string, defaults ...[]string) []string {
(*http.Request)(f).ParseMultipartForm(32 << 20)
if v, ok := f.Values()[key]; ok {
return v
}
if len(defaults) > 0 {
return defaults[0]
}
return []string{}
}
// MustEscape returns request form as escaped string with default
func (f *Queries) MustEscape(key string, defaults ...string) string {
if v, ok := f.Values()[key]; ok {
return template.HTMLEscapeString(v[0])
}
if len(defaults) > 0 {
return defaults[0]
}
return ""
}
// MustInt returns request form as int with default
func (f *Queries) MustInt(key string, defaults ...int) int {
if v, ok := f.Values()[key]; ok {
i, err := strconv.Atoi(v[0])
if err == nil {
return i
}
}
if len(defaults) > 0 {
return defaults[0]
}
return 0
}
// MustInt32 returns request form as int32 with default
func (f *Queries) MustInt32(key string, defaults ...int32) int32 {
if v, ok := f.Values()[key]; ok {
i, err := strconv.ParseInt(v[0], 10, 32)
if err == nil {
return int32(i)
}
}
if len(defaults) > 0 {
return defaults[0]
}
return 0
}
// MustInt64 returns request form as int64 with default
func (f *Queries) MustInt64(key string, defaults ...int64) int64 {
if v, ok := f.Values()[key]; ok {
i, err := strconv.ParseInt(v[0], 10, 64)
if err == nil {
return i
}
}
if len(defaults) > 0 {
return defaults[0]
}
return 0
}
// MustUint returns request form as uint with default
func (f *Queries) MustUint(key string, defaults ...uint) uint {
if v, ok := f.Values()[key]; ok {
i, err := strconv.ParseUint(v[0], 10, 64)
if err == nil {
return uint(i)
}
}
if len(defaults) > 0 {
return defaults[0]
}
return 0
}
// MustUint32 returns request form as uint32 with default
func (f *Queries) MustUint32(key string, defaults ...uint32) uint32 {
if v, ok := f.Values()[key]; ok {
i, err := strconv.ParseUint(v[0], 10, 32)
if err == nil {
return uint32(i)
}
}
if len(defaults) > 0 {
return defaults[0]
}
return 0
}
// MustUint64 returns request form as uint64 with default
func (f *Queries) MustUint64(key string, defaults ...uint64) uint64 {
if v, ok := f.Values()[key]; ok {
i, err := strconv.ParseUint(v[0], 10, 64)
if err == nil {
return i
}
}
if len(defaults) > 0 {
return defaults[0]
}
return 0
}
// MustFloat32 returns request form as float32 with default
func (f *Queries) MustFloat32(key string, defaults ...float32) float32 {
if v, ok := f.Values()[key]; ok {
i, err := strconv.ParseFloat(v[0], 32)
if err == nil {
return float32(i)
}
}
if len(defaults) > 0 {
return defaults[0]
}
return 0
}
// MustFloat64 returns request form as float64 with default
func (f *Queries) MustFloat64(key string, defaults ...float64) float64 {
if v, ok := f.Values()[key]; ok {
i, err := strconv.ParseFloat(v[0], 64)
if err == nil {
return i
}
}
if len(defaults) > 0 {
return defaults[0]
}
return 0
}
// MustBool returns request form as bool with default
func (f *Queries) MustBool(key string, defaults ...bool) bool {
if v, ok := f.Values()[key]; ok {
i, err := strconv.ParseBool(v[0])
if err == nil {
return i
}
}
if len(defaults) > 0 {
return defaults[0]
}
return false
}
// Query returns request form as string with default
func (ctx *Context) Query(key string, defaults ...string) string {
return (*Queries)(ctx.req).MustString(key, defaults...)
}
// QueryStrings returns request form as strings with default
func (ctx *Context) QueryStrings(key string, defaults ...[]string) []string {
return (*Queries)(ctx.req).MustStrings(key, defaults...)
}
// QueryEscape returns request form as escaped string with default
func (ctx *Context) QueryEscape(key string, defaults ...string) string {
return (*Queries)(ctx.req).MustEscape(key, defaults...)
}
// QueryInt returns request form as int with default
func (ctx *Context) QueryInt(key string, defaults ...int) int {
return (*Queries)(ctx.req).MustInt(key, defaults...)
}
// QueryInt32 returns request form as int32 with default
func (ctx *Context) QueryInt32(key string, defaults ...int32) int32 {
return (*Queries)(ctx.req).MustInt32(key, defaults...)
}
// QueryInt64 returns request form as int64 with default
func (ctx *Context) QueryInt64(key string, defaults ...int64) int64 {
return (*Queries)(ctx.req).MustInt64(key, defaults...)
}
// QueryUint returns request form as uint with default
func (ctx *Context) QueryUint(key string, defaults ...uint) uint {
return (*Queries)(ctx.req).MustUint(key, defaults...)
}
// QueryUint32 returns request form as uint32 with default
func (ctx *Context) QueryUint32(key string, defaults ...uint32) uint32 {
return (*Queries)(ctx.req).MustUint32(key, defaults...)
}
// QueryUint64 returns request form as uint64 with default
func (ctx *Context) QueryUint64(key string, defaults ...uint64) uint64 {
return (*Queries)(ctx.req).MustUint64(key, defaults...)
}
// QueryFloat32 returns request form as float32 with default
func (ctx *Context) QueryFloat32(key string, defaults ...float32) float32 {
return (*Queries)(ctx.req).MustFloat32(key, defaults...)
}
// FormFloat64 returns request form as float64 with default
func (ctx *Context) QueryFloat64(key string, defaults ...float64) float64 {
return (*Queries)(ctx.req).MustFloat64(key, defaults...)
}
// FormBool returns request form as bool with default
func (ctx *Context) QueryBool(key string, defaults ...bool) bool {
return (*Queries)(ctx.req).MustBool(key, defaults...)
}