/
utils.go
317 lines (275 loc) · 8.21 KB
/
utils.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
package valdn
import (
"encoding/json"
"net"
"reflect"
"regexp"
)
const (
emailRegex = "(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])"
macRegex = "^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$"
urlRegex = "[-a-zA-Z0-9@:%._\\+~#=]{1,256}\\.[a-zA-Z0-9()]{1,6}\\b([-a-zA-Z0-9()@:%_\\+.~#?&//=]*)"
)
// IsEmpty reports weather val is empty or not.
func IsEmpty(val interface{}) bool {
t := reflect.TypeOf(val)
v := reflect.ValueOf(val)
switch t.Kind() {
case reflect.Map, reflect.Slice, reflect.Array:
return v.Len() == 0
default:
return v.IsZero()
}
}
// IsKind reports weather val's kind equals kind.
func IsKind(val interface{}, kind string) bool {
if k := reflect.TypeOf(val).Kind(); toString(k) == kind {
return true
}
return false
}
// IsKindIn reports weather val's kind is one of kinds.
func IsKindIn(val interface{}, kinds []string) bool {
kind := toString(reflect.TypeOf(val).Kind())
for _, k := range kinds {
if k == kind {
return true
}
}
return false
}
// IsType reports weather val's type equals typ.
func IsType(val interface{}, typ string) bool {
var typeInString string
if t := reflect.TypeOf(val); t.Kind() == reflect.Struct {
typeInString = t.Name()
} else {
typeInString = toString(t)
}
if typeInString == typ {
return true
}
return false
}
// IsTypeIn reports weather val's type is one of types.
func IsTypeIn(val interface{}, types []string) bool {
var typeInString string
if t := reflect.TypeOf(val); t.Kind() == reflect.Struct {
typeInString = t.Name()
} else {
typeInString = toString(t)
}
for _, t := range types {
if t == typeInString {
return true
}
}
return false
}
// IsString reports weather val's kind is string or not.
func IsString(val interface{}) bool {
return reflect.ValueOf(val).Kind() == reflect.String
}
// IsInt reports weather val's kind is int or not.
func IsInt(val interface{}) bool {
return reflect.ValueOf(val).Kind() == reflect.Int
}
// IsInt8 reports weather val's kind is int8 or not.
func IsInt8(val interface{}) bool {
return reflect.ValueOf(val).Kind() == reflect.Int8
}
// IsInt16 reports weather val's kind is int16 or not.
func IsInt16(val interface{}) bool {
return reflect.ValueOf(val).Kind() == reflect.Int16
}
// IsInt32 reports weather val's kind is int32 or not.
func IsInt32(val interface{}) bool {
return reflect.ValueOf(val).Kind() == reflect.Int32
}
// IsInt64 reports weather val's kind is int64 or not.
func IsInt64(val interface{}) bool {
return reflect.ValueOf(val).Kind() == reflect.Int64
}
// IsUint reports weather val's kind is uint or not.
func IsUint(val interface{}) bool {
return reflect.ValueOf(val).Kind() == reflect.Uint
}
// IsUint8 reports weather val's kind is uint8 or not.
func IsUint8(val interface{}) bool {
return reflect.ValueOf(val).Kind() == reflect.Uint8
}
// IsUint16 reports weather val's kind is uint16 or not.
func IsUint16(val interface{}) bool {
return reflect.ValueOf(val).Kind() == reflect.Uint16
}
// IsUint32 reports weather val's kind is uint32 or not.
func IsUint32(val interface{}) bool {
return reflect.ValueOf(val).Kind() == reflect.Uint32
}
// IsUint64 reports weather val's kind is uint64 or not.
func IsUint64(val interface{}) bool {
return reflect.ValueOf(val).Kind() == reflect.Uint64
}
// IsFloat32 reports weather val's kind is float32 or not.
func IsFloat32(val interface{}) bool {
return reflect.ValueOf(val).Kind() == reflect.Float32
}
// IsFloat64 reports weather val's kind is float64 or not.
func IsFloat64(val interface{}) bool {
return reflect.ValueOf(val).Kind() == reflect.Float64
}
// IsComplex64 reports weather val's kind is complex64 or not.
func IsComplex64(val interface{}) bool {
return reflect.ValueOf(val).Kind() == reflect.Complex64
}
// IsComplex128 reports weather val's kind is complex128 or not.
func IsComplex128(val interface{}) bool {
return reflect.ValueOf(val).Kind() == reflect.Complex128
}
// IsBool reports weather val's kind is bool or not.
func IsBool(val interface{}) bool {
return reflect.ValueOf(val).Kind() == reflect.Bool
}
// IsSlice reports weather val's kind is slice or not.
func IsSlice(val interface{}) bool {
return reflect.ValueOf(val).Kind() == reflect.Slice
}
// IsArray reports weather val's kind is array or not.
func IsArray(val interface{}) bool {
return reflect.ValueOf(val).Kind() == reflect.Array
}
// IsStruct reports weather val's kind is struct or not.
func IsStruct(val interface{}) bool {
return reflect.ValueOf(val).Kind() == reflect.Struct
}
// IsMap reports weather val's kind is map or not.
func IsMap(val interface{}) bool {
return reflect.ValueOf(val).Kind() == reflect.Map
}
// IsInteger reports weather val is integer or not.
func IsInteger(val interface{}) bool {
switch reflect.TypeOf(val).Kind() {
case reflect.Uint, reflect.Int, reflect.Uint8, reflect.Int8, reflect.Uint16, reflect.Int16, reflect.Uint32, reflect.Int32, reflect.Uint64, reflect.Int64:
return true
default:
return false
}
}
// IsUnsignedInteger reports weather val is unsigned integer or not.
func IsUnsignedInteger(val interface{}) bool {
switch reflect.TypeOf(val).Kind() {
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return true
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
vString := toString(val)
if vString[0] != '-' {
return true
}
return false
}
return false
}
// IsFloat reports weather val is float or not.
func IsFloat(val interface{}) bool {
switch reflect.TypeOf(val).Kind() {
case reflect.Float32, reflect.Float64:
return true
default:
return false
}
}
// IsUnsignedFloat reports weather val is unsigned float or not.
func IsUnsignedFloat(val interface{}) bool {
switch reflect.TypeOf(val).Kind() {
case reflect.Float32, reflect.Float64:
vString := toString(val)
if vString[0] != '-' {
return true
}
return false
default:
return false
}
}
// IsComplex reports weather val is complex number or not.
func IsComplex(val interface{}) bool {
switch reflect.TypeOf(val).Kind() {
case reflect.Complex64, reflect.Complex128:
return true
default:
return false
}
}
// IsNumeric reports weather val is (integer, float, complex) or not.
func IsNumeric(val interface{}) bool {
if !IsInteger(val) && !IsFloat(val) && !IsComplex(val) {
return false
}
return true
}
// IsCollection reports weather val's kins is one of (Array, Slice, Map, Struct) or not.
func IsCollection(val interface{}) bool {
switch reflect.TypeOf(val).Kind() {
case reflect.Array, reflect.Slice, reflect.Map, reflect.Struct:
return true
default:
return false
}
}
// IsEmail reports weather s is a valid email address or not.
func IsEmail(s string) bool {
r := regexp.MustCompile(emailRegex)
return r.MatchString(s)
}
// IsJSON reports weather s is a valid json or not.
func IsJSON(s string) bool {
var decodedJSON map[string]interface{}
err := json.Unmarshal([]byte(s), &decodedJSON)
return err == nil
}
// IsIPv4 reports weather s is a valid IPv4 or not.
func IsIPv4(s string) bool {
if net.ParseIP(s) == nil {
return false
}
for i := 0; i < len(s); i++ {
if s[i] == '.' {
return true
}
}
return false
}
// IsIPv6 reports weather s is a valid IPv6 or not.
func IsIPv6(s string) bool {
if net.ParseIP(s) == nil {
return false
}
for i := 0; i < len(s); i++ {
if s[i] == ':' {
return true
}
}
return false
}
// IsIP reports weather s is a valid IP or not.
func IsIP(s string) bool {
return net.ParseIP(s) != nil
}
// IsMAC reports weather s is a valid MAC address or not.
func IsMAC(s string) bool {
r := regexp.MustCompile(macRegex)
return r.MatchString(s)
}
// IsURL reports weather s is a valid URL or not.
func IsURL(s string) bool {
r := regexp.MustCompile(urlRegex)
return r.MatchString(s)
}
// IsFile reports weather v is a valid file or not.
func IsFile(v interface{}) bool {
_, err := getFileSize(v)
if err != nil {
return false
}
return true
}