-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate.go
90 lines (73 loc) · 1.99 KB
/
validate.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
package config
import (
"errors"
"reflect"
)
type validateFunc interface {
Validate() error
}
// Validate calls validateFunc for all types that implements it.
func Validate(i interface{}) error {
var value = reflect.ValueOf(i)
// if the reflected value is nil
if !value.IsValid() {
return errors.New("i value is nil")
}
// as i is actually an interface, just get the thing behind the interface
value = reflect.Indirect(value.Elem())
var errs = make(ValidationError)
// recursively walk through it to see if it's valid
validateRecursively(&value, "", errs)
if len(errs) > 0 {
return errs
}
return nil
}
func validateRecursively(v *reflect.Value, name string, errs ValidationError) {
switch v.Kind() {
case reflect.Ptr:
// we don't want to put default on nil pointed value so just leave here
if v.IsNil() {
break
}
// retrieve the pointed value
var pv = v.Elem()
// try to put a default recursively to the pointed value
validateRecursively(&pv, name, errs)
case reflect.Struct:
// try to put default on the whole structure
if err := validateValue(v); err != nil {
errs[name] = err
}
// try each fields, there may be empty fields that also want defaults
for i := 0; i < v.NumField(); i++ {
// get field
var (
childV = v.Field(i)
childField = v.Type().Field(i)
)
// ignore it if the field is unexported
if v.Type().Field(i).PkgPath != "" {
continue
}
// handle the child recursively
validateRecursively(&childV, appendConfigTreePath(name, childField.Name), errs)
}
default:
// for every other types, try to set default
if err := validateValue(v); err != nil {
errs[name] = err
}
}
}
func validateValue(v *reflect.Value) error {
// since we're gonna need the value to match an interface through the
// address of the struct, make sure we can do that
if !v.IsValid() || !v.CanInterface() || !v.CanAddr() {
return nil
}
if f, ok := v.Addr().Interface().(validateFunc); ok {
return f.Validate()
}
return nil
}