-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
67 lines (58 loc) · 1.59 KB
/
util.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
package valval
import "reflect"
func NewStringValidator(inner func(string) error) ValidatorFunc {
return func(val interface{}) error {
if val == nil {
return nil
}
rv := reflect.ValueOf(val)
if rv.Kind() == reflect.String {
return inner(rv.String())
}
return typeMissmatchError("string")
}
}
func NewFloatValidator(inner func(float64) error) ValidatorFunc {
return func(val interface{}) error {
if val == nil {
return nil
}
rv := reflect.ValueOf(val)
switch rv.Kind() {
case reflect.Float32, reflect.Float64:
return inner(rv.Float())
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return inner(float64(rv.Int()))
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return inner(float64(rv.Uint()))
}
return typeMissmatchError("number")
}
}
func NewIntValidator(inner func(int64) error) ValidatorFunc {
return func(val interface{}) error {
if val == nil {
return nil
}
rv := reflect.ValueOf(val)
switch rv.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return inner(rv.Int())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return inner(int64(rv.Uint()))
}
return typeMissmatchError("integer")
}
}
func NewBoolValidator(inner func(bool) error) ValidatorFunc {
return func(val interface{}) error {
if val == nil {
return nil
}
rv := reflect.ValueOf(val)
if rv.Kind() == reflect.Bool {
return inner(rv.Bool())
}
return typeMissmatchError("integer")
}
}