This repository has been archived by the owner on Sep 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
/
param.go
164 lines (136 loc) · 3.14 KB
/
param.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
package param
import (
"fmt"
"net/http"
"strconv"
"strings"
"github.com/urlooker/web/http/errors"
)
func String(r *http.Request, key string, defVal string) string {
if val, ok := r.URL.Query()[key]; ok {
if val[0] == "" {
return defVal
}
return strings.TrimSpace(val[0])
}
if r.Form == nil {
err := r.ParseForm()
if err != nil {
panic(errors.BadRequestError())
}
}
val := r.Form.Get(key)
if val == "" {
return defVal
}
return strings.TrimSpace(val)
}
func MustString(r *http.Request, key string, displayName ...string) string {
val := String(r, key, "")
if val == "" {
name := key
if len(displayName) > 0 {
name = displayName[0]
}
panic(errors.BadRequestError(fmt.Sprintf("%s is necessary", name)))
}
return val
}
func Int64(r *http.Request, key string, defVal int64) int64 {
raw := String(r, key, "")
if raw == "" {
return defVal
}
val, err := strconv.ParseInt(raw, 10, 64)
if err != nil {
return defVal
}
return val
}
func MustInt64(r *http.Request, key string, displayName ...string) int64 {
raw := String(r, key, "")
if raw == "" {
name := key
if len(displayName) > 0 {
name = displayName[0]
}
panic(errors.BadRequestError(fmt.Sprintf("%s is necessary", name)))
}
val, err := strconv.ParseInt(raw, 10, 64)
if err != nil {
panic(errors.BadRequestError())
}
return val
}
func Int(r *http.Request, key string, defVal int) int {
raw := String(r, key, "")
if raw == "" {
return defVal
}
val, err := strconv.Atoi(raw)
if err != nil {
return defVal
}
return val
}
func MustInt(r *http.Request, key string, displayName ...string) int {
name := key
if len(displayName) > 0 {
name = displayName[0]
}
raw := String(r, key, "")
if raw == "" {
panic(errors.BadRequestError(fmt.Sprintf("%s is necessary", name)))
}
val, err := strconv.Atoi(raw)
if err != nil {
panic(errors.BadRequestError(fmt.Sprintf("%s should be integer", name)))
}
return val
}
func Float64(r *http.Request, key string, defVal float64) float64 {
raw := String(r, key, "")
if raw == "" {
return defVal
}
val, err := strconv.ParseFloat(raw, 64)
if err != nil {
return defVal
}
return val
}
func MustFloat64(r *http.Request, key string, displayName ...string) float64 {
raw := String(r, key, "")
if raw == "" {
name := key
if len(displayName) > 0 {
name = displayName[0]
}
panic(errors.BadRequestError(fmt.Sprintf("%s is necessary", name)))
}
val, err := strconv.ParseFloat(raw, 64)
if err != nil {
panic(errors.BadRequestError())
}
return val
}
func Bool(r *http.Request, key string, defVal bool) bool {
raw := String(r, key, "")
if raw == "true" || raw == "1" || raw == "on" || raw == "checked" || raw == "yes" {
return true
} else if raw == "false" || raw == "0" || raw == "off" || raw == "" || raw == "no" {
return false
} else {
return defVal
}
}
func MustBool(r *http.Request, key string) bool {
raw := String(r, key, "")
if raw == "true" || raw == "1" || raw == "on" || raw == "checked" || raw == "yes" {
return true
} else if raw == "false" || raw == "0" || raw == "off" || raw == "" || raw == "no" {
return false
} else {
panic(errors.BadRequestError())
}
}