-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintfield.go
67 lines (62 loc) · 1.84 KB
/
intfield.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 tagvalidate
import (
"log"
"reflect"
"strconv"
)
var int_allowed = map[string](func(int64, string) bool){
"eq": func(v int64, t string) bool {
if veq, err := strconv.Atoi(t); err == nil {
return int64(veq) == v
} else {
return true
}
}, //(int/[]int)must in those values
"neq": func(v int64, t string) bool {
if veq, err := strconv.Atoi(t); err == nil {
return int64(veq) != v
} else {
return true
}
}, //(int/[]int)must in those values
"zero": func(v int64, t string) bool { return (t == "true") || ((t == "false") && (v != 0)) }, //(bool) if allow zero?
"max": func(v int64, t string) bool {
if ml, err := strconv.ParseInt(t, 10, 64); err == nil {
return ml >= v
} else {
return true
}
}, //(int) strictly set max
"min": func(v int64, t string) bool {
if ml, err := strconv.ParseInt(t, 10, 64); err == nil {
return ml <= v
} else {
log.Println("Convert error:", t)
return true
}
}, //(int) strictly set min
"func": func(v int64, t string) bool { return true }, //(string) given check func name under this struct
}
func (checker *FieldCheck) checkIntField(val reflect.Value, field reflect.StructField) error {
empty_tag := "zero"
empty_value := int64(0)
valreal := val.Int()
table := int_allowed
empty, _ := checker.getFieldTag(field, empty_tag)
if empty == "true" && valreal == empty_value {
return nil
}
for tagname, tagfunc := range table {
if tagvalue, ok := field.Tag.Lookup(checker.getTagName(tagname)); ok {
checked := tagfunc(valreal, tagvalue)
// println(ok, tagname, tagvalue, val.String(), checked)
if !checked {
return GetError(3, field.Name, tagname, tagvalue, valreal)
}
}
}
return nil
}
func (checker *FieldCheck) checkUintField(val reflect.Value, field reflect.StructField) error {
return nil
}