-
Notifications
You must be signed in to change notification settings - Fork 124
/
utils.go
57 lines (52 loc) · 1.08 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
package govalidator
import (
"fmt"
"reflect"
"strings"
)
// containsRequiredField check rules contain any required field
func isContainRequiredField(rules []string) bool {
for _, rule := range rules {
if rule == "required" {
return true
}
}
return false
}
// isRuleExist check if the provided rule name is exist or not
func isRuleExist(rule string) bool {
if strings.Contains(rule, ":") {
rule = strings.Split(rule, ":")[0]
}
extendedRules := []string{"size", "mime", "ext"}
for _, r := range extendedRules {
if r == rule {
return true
}
}
if _, ok := rulesFuncMap[rule]; ok {
return true
}
return false
}
// toString force data to be string
func toString(v interface{}) string {
str, ok := v.(string)
if !ok {
str = fmt.Sprintf("%v", v)
}
return str
}
// isEmpty check a type is Zero
func isEmpty(x interface{}) bool {
rt := reflect.TypeOf(x)
if rt == nil {
return true
}
rv := reflect.ValueOf(x)
switch rv.Kind() {
case reflect.Array, reflect.Map, reflect.Slice:
return rv.Len() == 0
}
return reflect.DeepEqual(x, reflect.Zero(rt).Interface())
}