-
Notifications
You must be signed in to change notification settings - Fork 1
/
form_values.go
222 lines (206 loc) · 4.47 KB
/
form_values.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
package form_validator
import (
"fmt"
"strconv"
)
func getFormValue(name string, c *Config) interface{} {
for _, v := range c.Fields {
if v.Name == name {
return v.Value
}
}
return nil
}
// GetString gets any string types from the form values
//
//
// myStr, _ = GetString("name", &c)
//
func GetString(name string, c *Config) (string, error) {
return fmt.Sprintf("%v", getFormValue(name, c)), nil
}
// GetBool gets any bool types from the form values
//
//
// myBool, _ = GetBool("is_happy", &c)
//
func GetBool(name string, c *Config) (bool, error) {
var err error
b := fmt.Sprintf("%v", getFormValue(name, c))
if b == "true" || b == "True" {
return true, nil
}
i, err := strconv.Atoi(b)
if i == 1 {
return true, err
}
return false, err
}
// GetFloat32 gets any int types from the form values
//
//
// myFloat32, _ = GetFloat32("age", &c)
//
func GetFloat32(name string, c *Config) (float32, error) {
var err error
b := fmt.Sprintf("%v", getFormValue(name, c))
float, err := strconv.ParseFloat(b, 32)
if err != nil {
return 0, err
}
return float32(float), err
}
// GetFloat64 gets any int types from the form values
//
//
// myFloat64, _ = GetFloat64("age", &c)
//
func GetFloat64(name string, c *Config) (float64, error) {
var err error
b := fmt.Sprintf("%v", getFormValue(name, c))
float, err := strconv.ParseFloat(b, 64)
if err != nil {
return 0, err
}
return float, err
}
// GetInt gets any int types from the form values
//
//
// myInt, _ = GetInt("age", &c)
//
func GetInt(name string, c *Config) (int, error) {
var err error
b := fmt.Sprintf("%v", getFormValue(name, c))
i, err := strconv.Atoi(b)
if err != nil {
return 0, err
}
return i, err
}
// GetInt8 gets any int types from the form values
//
//
// myInt8, _ = GetInt8("age", &c)
//
func GetInt8(name string, c *Config) (int8, error) {
var err error
b := fmt.Sprintf("%v", getFormValue(name, c))
u, err := strconv.ParseInt(b, 10, 8)
if err != nil {
return 0, err
}
return int8(u), err
}
// GetInt16 gets any int types from the form values
//
//
// myInt16, _ = GetInt16("age", &c)
//
func GetInt16(name string, c *Config) (int16, error) {
var err error
b := fmt.Sprintf("%v", getFormValue(name, c))
u, err := strconv.ParseInt(b, 10, 16)
if err != nil {
return 0, err
}
return int16(u), err
}
// GetInt32 gets any int types from the form values
//
//
// myInt32, _ = GetInt32("age", &c)
//
func GetInt32(name string, c *Config) (int32, error) {
var err error
b := fmt.Sprintf("%v", getFormValue(name, c))
u, err := strconv.ParseInt(b, 10, 32)
if err != nil {
return 0, err
}
return int32(u), err
}
// GetInt64 gets any int types from the form values
//
//
// myInt64, _ = GetInt64("age", &c)
//
func GetInt64(name string, c *Config) (int64, error) {
var err error
b := fmt.Sprintf("%v", getFormValue(name, c))
u, err := strconv.ParseInt(b, 10, 64)
if err != nil {
return 0, err
}
return int64(u), err
}
// GetUint gets any uint types from the form values
//
//
// GetUint, _ = GetUint("age", &c)
//
func GetUint(name string, c *Config) (uint, error) {
var err error
b := fmt.Sprintf("%v", getFormValue(name, c))
u, err := strconv.ParseUint(b, 10, 64)
if err != nil {
return 0, err
}
return uint(u), err
}
// GetUint8 gets any uint types from the form values
//
//
// GetUint8, _ = GetUint8("age", &c)
//
func GetUint8(name string, c *Config) (uint8, error) {
var err error
b := fmt.Sprintf("%v", getFormValue(name, c))
u, err := strconv.ParseUint(b, 10, 8)
if err != nil {
return 0, err
}
return uint8(u), err
}
// GetUint16 gets any uint types from the form values
//
//
// GetUint16, _ = GetUint16("age", &c)
//
func GetUint16(name string, c *Config) (uint16, error) {
var err error
b := fmt.Sprintf("%v", getFormValue(name, c))
u, err := strconv.ParseUint(b, 10, 16)
if err != nil {
return 0, err
}
return uint16(u), err
}
// GetUint32 gets any uint types from the form values
//
//
// GetUint32, _ = GetUint32("age", &c)
//
func GetUint32(name string, c *Config) (uint32, error) {
var err error
b := fmt.Sprintf("%v", getFormValue(name, c))
u, err := strconv.ParseUint(b, 10, 32)
if err != nil {
return 0, err
}
return uint32(u), err
}
// GetUint64 gets any uint types from the form values
//
//
// GetUint64, _ = GetUint64("age", &c)
//
func GetUint64(name string, c *Config) (uint64, error) {
var err error
b := fmt.Sprintf("%v", getFormValue(name, c))
u, err := strconv.ParseUint(b, 10, 32)
if err != nil {
return 0, err
}
return u, err
}