-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstruct.go
56 lines (48 loc) · 1.02 KB
/
struct.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
package govals
// StructVals is for store validation status
import (
"reflect"
)
type StructVals struct {
fields map[string]tagField
}
// Struct set new validator
func Struct(s interface{}) *StructVals {
gov := &StructVals{fields: make(map[string]tagField)}
val := reflect.ValueOf(s)
num := 0
if val.Kind() == reflect.Ptr {
num = val.Elem().NumField()
val = val.Elem()
} else {
num = val.NumField()
}
for i := 0; i < num; i++ {
tg := val.Type().Field(i).Tag.Get(tagName)
if tg == "-" {
continue
} else if tg == "" {
continue
}
tag := &tagField{
val: val.Field(i),
tagVal: tg,
tagType: val.Type().Field(i),
}
valid := tag.process()
ok, err := valid.validate()
tag.err = err
tag.status = ok
gov.fields[tag.tagType.Name] = *tag
}
return gov
}
// GetStatus return validation bool and error
// of validation fields
func (gov *StructVals) GetStatus(str string) (bool, error) {
val, ok := gov.fields[str]
if !ok {
return false, errFieldNotResgister
}
return val.status, val.err
}