-
Notifications
You must be signed in to change notification settings - Fork 1
/
tools.go
581 lines (498 loc) · 14.6 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
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
package echotool
import (
"bytes"
"io/ioutil"
"net/http"
"net/url"
"os"
"reflect"
"runtime"
"strconv"
"strings"
"github.com/google/go-querystring/query"
"github.com/labstack/echo/v4"
"github.com/popeyeio/handy"
"github.com/rs/xid"
"github.com/songzhaoliang/echotool/json"
)
type RunFunc func() (interface{}, error)
type CallbackFunc func(error)
func HeaderBool(c echo.Context, key string) (bool, error) {
if v := c.Request().Header.Get(key); handy.IsEmptyStr(v) {
return false, NewHeaderEmptyError(key)
} else {
return strconv.ParseBool(v)
}
}
func MustHeaderBool(c echo.Context, key string, cbs ...CallbackFunc) bool {
result := MustDoCallback(func() (interface{}, error) {
return HeaderBool(c, key)
}, CodeBadRequest, cbs...)
return result.(bool)
}
func HeaderInt64(c echo.Context, key string) (int64, error) {
if v := c.Request().Header.Get(key); handy.IsEmptyStr(v) {
return 0, NewHeaderEmptyError(key)
} else {
return strconv.ParseInt(v, 10, 64)
}
}
func MustHeaderInt64(c echo.Context, key string, cbs ...CallbackFunc) int64 {
result := MustDoCallback(func() (interface{}, error) {
return HeaderInt64(c, key)
}, CodeBadRequest, cbs...)
return result.(int64)
}
func HeaderUint64(c echo.Context, key string) (uint64, error) {
if v := c.Request().Header.Get(key); handy.IsEmptyStr(v) {
return 0, NewHeaderEmptyError(key)
} else {
return strconv.ParseUint(v, 10, 64)
}
}
func MustHeaderUInt64(c echo.Context, key string, cbs ...CallbackFunc) uint64 {
result := MustDoCallback(func() (interface{}, error) {
return HeaderUint64(c, key)
}, CodeBadRequest, cbs...)
return result.(uint64)
}
func HeaderString(c echo.Context, key string) (string, error) {
if v := c.Request().Header.Get(key); handy.IsEmptyStr(v) {
return handy.StrEmpty, NewHeaderEmptyError(key)
} else {
return v, nil
}
}
func MustHeaderString(c echo.Context, key string, cbs ...CallbackFunc) string {
result := MustDoCallback(func() (interface{}, error) {
return HeaderString(c, key)
}, CodeBadRequest, cbs...)
return result.(string)
}
func ParamBool(c echo.Context, key string) (bool, error) {
if v := c.Param(key); handy.IsEmptyStr(v) {
return false, NewParamEmptyError(key)
} else {
return strconv.ParseBool(v)
}
}
func MustParamBool(c echo.Context, key string, cbs ...CallbackFunc) bool {
result := MustDoCallback(func() (interface{}, error) {
return ParamBool(c, key)
}, CodeBadRequest, cbs...)
return result.(bool)
}
func ParamInt64(c echo.Context, key string) (int64, error) {
if v := c.Param(key); handy.IsEmptyStr(v) {
return 0, NewParamEmptyError(key)
} else {
return strconv.ParseInt(v, 10, 64)
}
}
func MustParamInt64(c echo.Context, key string, cbs ...CallbackFunc) int64 {
result := MustDoCallback(func() (interface{}, error) {
return ParamInt64(c, key)
}, CodeBadRequest, cbs...)
return result.(int64)
}
func ParamUint64(c echo.Context, key string) (uint64, error) {
if v := c.Param(key); handy.IsEmptyStr(v) {
return 0, NewParamEmptyError(key)
} else {
return strconv.ParseUint(v, 10, 64)
}
}
func MustParamUInt64(c echo.Context, key string, cbs ...CallbackFunc) uint64 {
result := MustDoCallback(func() (interface{}, error) {
return ParamUint64(c, key)
}, CodeBadRequest, cbs...)
return result.(uint64)
}
func ParamString(c echo.Context, key string) (string, error) {
if v := c.Param(key); handy.IsEmptyStr(v) {
return handy.StrEmpty, NewParamEmptyError(key)
} else {
return v, nil
}
}
func MustParamString(c echo.Context, key string, cbs ...CallbackFunc) string {
result := MustDoCallback(func() (interface{}, error) {
return ParamString(c, key)
}, CodeBadRequest, cbs...)
return result.(string)
}
func QueryBool(c echo.Context, key string) (bool, error) {
if v := c.QueryParam(key); handy.IsEmptyStr(v) {
return false, NewQueryEmptyError(key)
} else {
return strconv.ParseBool(v)
}
}
func MustQueryBool(c echo.Context, key string, cbs ...CallbackFunc) bool {
result := MustDoCallback(func() (interface{}, error) {
return QueryBool(c, key)
}, CodeBadRequest, cbs...)
return result.(bool)
}
func QueryInt64(c echo.Context, key string) (int64, error) {
if v := c.QueryParam(key); handy.IsEmptyStr(v) {
return 0, NewQueryEmptyError(key)
} else {
return strconv.ParseInt(v, 10, 64)
}
}
func MustQueryInt64(c echo.Context, key string, cbs ...CallbackFunc) int64 {
result := MustDoCallback(func() (interface{}, error) {
return QueryInt64(c, key)
}, CodeBadRequest, cbs...)
return result.(int64)
}
func QueryUint64(c echo.Context, key string) (uint64, error) {
if v := c.QueryParam(key); handy.IsEmptyStr(v) {
return 0, NewQueryEmptyError(key)
} else {
return strconv.ParseUint(v, 10, 64)
}
}
func MustQueryUInt64(c echo.Context, key string, cbs ...CallbackFunc) uint64 {
result := MustDoCallback(func() (interface{}, error) {
return QueryUint64(c, key)
}, CodeBadRequest, cbs...)
return result.(uint64)
}
func QueryString(c echo.Context, key string) (string, error) {
if v := c.QueryParam(key); handy.IsEmptyStr(v) {
return handy.StrEmpty, NewQueryEmptyError(key)
} else {
return v, nil
}
}
func MustQueryString(c echo.Context, key string, cbs ...CallbackFunc) string {
result := MustDoCallback(func() (interface{}, error) {
return QueryString(c, key)
}, CodeBadRequest, cbs...)
return result.(string)
}
func PostFormBool(c echo.Context, key string) (bool, error) {
if v := c.Request().PostFormValue(key); handy.IsEmptyStr(v) {
return false, NewPostFormEmptyError(key)
} else {
return strconv.ParseBool(v)
}
}
func MustPostFormBool(c echo.Context, key string, cbs ...CallbackFunc) bool {
result := MustDoCallback(func() (interface{}, error) {
return PostFormBool(c, key)
}, CodeBadRequest, cbs...)
return result.(bool)
}
func PostFormInt64(c echo.Context, key string) (int64, error) {
if v := c.Request().PostFormValue(key); handy.IsEmptyStr(v) {
return 0, NewPostFormEmptyError(key)
} else {
return strconv.ParseInt(v, 10, 64)
}
}
func MustPostFormInt64(c echo.Context, key string, cbs ...CallbackFunc) int64 {
result := MustDoCallback(func() (interface{}, error) {
return PostFormInt64(c, key)
}, CodeBadRequest, cbs...)
return result.(int64)
}
func PostFormUint64(c echo.Context, key string) (uint64, error) {
if v := c.Request().PostFormValue(key); handy.IsEmptyStr(v) {
return 0, NewPostFormEmptyError(key)
} else {
return strconv.ParseUint(v, 10, 64)
}
}
func MustPostFormUInt64(c echo.Context, key string, cbs ...CallbackFunc) uint64 {
result := MustDoCallback(func() (interface{}, error) {
return PostFormUint64(c, key)
}, CodeBadRequest, cbs...)
return result.(uint64)
}
func PostFormString(c echo.Context, key string) (string, error) {
if v := c.Request().PostFormValue(key); handy.IsEmptyStr(v) {
return handy.StrEmpty, NewPostFormEmptyError(key)
} else {
return v, nil
}
}
func MustPostFormString(c echo.Context, key string, cbs ...CallbackFunc) string {
result := MustDoCallback(func() (interface{}, error) {
return PostFormString(c, key)
}, CodeBadRequest, cbs...)
return result.(string)
}
func FormBool(c echo.Context, key string) (bool, error) {
if v := c.FormValue(key); handy.IsEmptyStr(v) {
return false, NewFormEmptyError(key)
} else {
return strconv.ParseBool(v)
}
}
func MustFormBool(c echo.Context, key string, cbs ...CallbackFunc) bool {
result := MustDoCallback(func() (interface{}, error) {
return FormBool(c, key)
}, CodeBadRequest, cbs...)
return result.(bool)
}
func FormInt64(c echo.Context, key string) (int64, error) {
if v := c.FormValue(key); handy.IsEmptyStr(v) {
return 0, NewFormEmptyError(key)
} else {
return strconv.ParseInt(v, 10, 64)
}
}
func MustFormInt64(c echo.Context, key string, cbs ...CallbackFunc) int64 {
result := MustDoCallback(func() (interface{}, error) {
return FormInt64(c, key)
}, CodeBadRequest, cbs...)
return result.(int64)
}
func FormUint64(c echo.Context, key string) (uint64, error) {
if v := c.FormValue(key); handy.IsEmptyStr(v) {
return 0, NewFormEmptyError(key)
} else {
return strconv.ParseUint(v, 10, 64)
}
}
func MustFormUint64(c echo.Context, key string, cbs ...CallbackFunc) uint64 {
result := MustDoCallback(func() (interface{}, error) {
return FormUint64(c, key)
}, CodeBadRequest, cbs...)
return result.(uint64)
}
func FormString(c echo.Context, key string) (string, error) {
if v := c.FormValue(key); handy.IsEmptyStr(v) {
return handy.StrEmpty, NewFormEmptyError(key)
} else {
return v, nil
}
}
func MustFormString(c echo.Context, key string, cbs ...CallbackFunc) string {
result := MustDoCallback(func() (interface{}, error) {
return FormString(c, key)
}, CodeBadRequest, cbs...)
return result.(string)
}
type FileInfo struct {
Name string
Content []byte
}
func MustFormFile(c echo.Context, key string, cbs ...CallbackFunc) *FileInfo {
result := MustDoCallback(func() (interface{}, error) {
fh, err := c.FormFile(key)
if err != nil {
return nil, err
}
f, err := fh.Open()
if err != nil {
return nil, err
}
defer f.Close()
content, err := ioutil.ReadAll(f)
if err != nil {
return nil, err
}
return &FileInfo{
Name: fh.Filename,
Content: content,
}, nil
}, CodeBadRequest, cbs...)
return result.(*FileInfo)
}
func EnvBool(c echo.Context, key string) (bool, error) {
if v := os.Getenv(key); handy.IsEmptyStr(v) {
return false, NewEnvEmptyError(key)
} else {
return strconv.ParseBool(v)
}
}
func MustEnvBool(c echo.Context, key string, cbs ...CallbackFunc) bool {
result := MustDoCallback(func() (interface{}, error) {
return EnvBool(c, key)
}, CodeInternalErr, cbs...)
return result.(bool)
}
func EnvInt64(c echo.Context, key string) (int64, error) {
if v := os.Getenv(key); handy.IsEmptyStr(v) {
return 0, NewEnvEmptyError(key)
} else {
return strconv.ParseInt(v, 10, 64)
}
}
func MustEnvInt64(c echo.Context, key string, cbs ...CallbackFunc) int64 {
result := MustDoCallback(func() (interface{}, error) {
return EnvInt64(c, key)
}, CodeInternalErr, cbs...)
return result.(int64)
}
func EnvUint64(c echo.Context, key string) (uint64, error) {
if v := os.Getenv(key); handy.IsEmptyStr(v) {
return 0, NewEnvEmptyError(key)
} else {
return strconv.ParseUint(v, 10, 64)
}
}
func MustEnvUint64(c echo.Context, key string, cbs ...CallbackFunc) uint64 {
result := MustDoCallback(func() (interface{}, error) {
return EnvUint64(c, key)
}, CodeInternalErr, cbs...)
return result.(uint64)
}
func EnvString(c echo.Context, key string) (string, error) {
if v := os.Getenv(key); handy.IsEmptyStr(v) {
return handy.StrEmpty, NewEnvEmptyError(key)
} else {
return v, nil
}
}
func MustEnvString(c echo.Context, key string, cbs ...CallbackFunc) string {
result := MustDoCallback(func() (interface{}, error) {
return EnvString(c, key)
}, CodeInternalErr, cbs...)
return result.(string)
}
func CookieBool(c echo.Context, key string) (bool, error) {
cookie, err := c.Cookie(key)
if err != nil {
return false, err
}
if v := cookie.Value; handy.IsEmptyStr(v) {
return false, NewCookieEmptyError(key)
} else {
return strconv.ParseBool(v)
}
}
func MustCookieBool(c echo.Context, key string, cbs ...CallbackFunc) bool {
result := MustDoCallback(func() (interface{}, error) {
return CookieBool(c, key)
}, CodeInternalErr, cbs...)
return result.(bool)
}
func CookieInt64(c echo.Context, key string) (int64, error) {
cookie, err := c.Cookie(key)
if err != nil {
return 0, err
}
if v := cookie.Value; handy.IsEmptyStr(v) {
return 0, NewCookieEmptyError(key)
} else {
return strconv.ParseInt(v, 10, 64)
}
}
func MustCookieInt64(c echo.Context, key string, cbs ...CallbackFunc) int64 {
result := MustDoCallback(func() (interface{}, error) {
return CookieInt64(c, key)
}, CodeInternalErr, cbs...)
return result.(int64)
}
func CookieUint64(c echo.Context, key string) (uint64, error) {
cookie, err := c.Cookie(key)
if err != nil {
return 0, err
}
if v := cookie.Value; handy.IsEmptyStr(v) {
return 0, NewCookieEmptyError(key)
} else {
return strconv.ParseUint(v, 10, 64)
}
}
func MustCookieUint64(c echo.Context, key string, cbs ...CallbackFunc) uint64 {
result := MustDoCallback(func() (interface{}, error) {
return CookieUint64(c, key)
}, CodeInternalErr, cbs...)
return result.(uint64)
}
func CookieString(c echo.Context, key string) (string, error) {
cookie, err := c.Cookie(key)
if err != nil {
return handy.StrEmpty, err
}
if v := cookie.Value; handy.IsEmptyStr(v) {
return handy.StrEmpty, NewEnvEmptyError(key)
} else {
return v, nil
}
}
func MustCookieString(c echo.Context, key string, cbs ...CallbackFunc) string {
result := MustDoCallback(func() (interface{}, error) {
return CookieString(c, key)
}, CodeInternalErr, cbs...)
return result.(string)
}
// EncodeValues needs tag "url" in fields of v.
func EncodeValues(v interface{}) (url.Values, error) {
return query.Values(v)
}
func MustEncodeValues(v interface{}, cbs ...CallbackFunc) url.Values {
result := MustDoCallback(func() (interface{}, error) {
return EncodeValues(v)
}, CodeEncodeErr, cbs...)
return result.(url.Values)
}
// EncodeJSON needs tag "json" in fields of v.
// Note: ReleaseBuffer needs to be called after EncodeJSON is called successfully.
func EncodeJSON(v interface{}) (*bytes.Buffer, error) {
buffer := AcquireBuffer()
if err := json.NewEncoder(buffer).Encode(v); err != nil {
ReleaseBuffer(buffer)
return nil, err
}
return buffer, nil
}
// MustEncodeJSON needs tag "json" in fields of v.
// Note: ReleaseBuffer needs to be called after MustEncodeJSON is called successfully.
func MustEncodeJSON(v interface{}, cbs ...CallbackFunc) *bytes.Buffer {
result := MustDoCallback(func() (interface{}, error) {
return EncodeJSON(v)
}, CodeEncodeErr, cbs...)
return result.(*bytes.Buffer)
}
func MustDo(run RunFunc, codes ...int) interface{} {
code := CodeDownstreamErr
if len(codes) > 0 {
code = codes[0]
}
return MustDoCallback(run, code)
}
// MustDoCallback will call cbs if and only if run has error.
func MustDoCallback(run RunFunc, code int, cbs ...CallbackFunc) interface{} {
if run == nil {
return nil
}
result, err := run()
if err == nil {
return result
}
for _, cb := range cbs {
cb(err)
}
if !IsEchotoolError(err) {
err = AcquireEchotoolError(code, err)
}
panic(err)
}
func GetRequestHost(req *http.Request) (host string) {
if host = req.Host; handy.IsEmptyStr(host) {
host = req.URL.Host
}
return
}
func GetUUID(c echo.Context) (u string) {
u, _ = handy.GetUUID()
return
}
func GetShortUUID(c echo.Context) string {
return xid.New().String()
}
func GetHostname() (host string) {
host, _ = os.Hostname()
return
}
func GetHandlerName(f HandlerFunc) string {
name := runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
return name[strings.LastIndex(name, handy.StrDot)+1:]
}