-
Notifications
You must be signed in to change notification settings - Fork 0
/
params.go
94 lines (78 loc) · 2.06 KB
/
params.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
package water
import (
"strconv"
)
type Params map[string]string
// Param returns the value of the URL param.
// It is a shortcut for c.Params.String(key)
// router.GET("/user/<id>", func(c *water.Context) {
// // a GET request to /user/john
// id := c.Param("id") // id == "john"
// })
func (ctx *Context) Param(key string) string {
return ctx.Params.String(key)
}
// String returns value by given param name.
// panic if param not exits
func (p Params) String(name string) string {
if v, ok := p[name]; ok {
return v
} else {
panic("Params not exist: " + name)
}
}
// Bool return bool with error.
func (p Params) Bool(name string) (bool, error) {
return strconv.ParseBool(p.String(name))
}
// Int return int with error.
func (p Params) Int(name string) (int, error) {
return strconv.Atoi(p.String(name))
}
// Int64 returns int64 with error.
func (p Params) Int64(name string) (int64, error) {
return strconv.ParseInt(p.String(name), 10, 64)
}
// Uint returns uint with error.
func (p Params) Uint(name string) (uint, error) {
v, err := strconv.ParseUint(p.String(name), 10, 64)
return uint(v), err
}
// Uint64 returns uint64 with error.
func (p Params) Uint64(name string) (uint64, error) {
return strconv.ParseUint(p.String(name), 10, 64)
}
// Float64 returns float64 with error.
func (p Params) Float64(name string) (float64, error) {
return strconv.ParseFloat(p.String(name), 64)
}
// MustBool returns bool.
func (p Params) MustBool(name string) bool {
v, _ := p.Bool(name)
return v
}
// MustInt returns int.
func (p Params) MustInt(name string) int {
v, _ := p.Int(name)
return v
}
// MustInt64 returns int64.
func (p Params) MustInt64(name string) int64 {
v, _ := p.Int64(name)
return v
}
// MustUint returns uint.
func (p Params) MustUint(name string) uint {
v, _ := p.Uint(name)
return uint(v)
}
// MustUint64 returns uint64.
func (p Params) MustUint64(name string) uint64 {
v, _ := p.Uint64(name)
return v
}
// MustFloat64 returns float64l
func (p Params) MustFloat64(name string) float64 {
v, _ := p.Float64(name)
return v
}