-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwalk.go
124 lines (100 loc) · 3 KB
/
walk.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package validator
import (
"errors"
"github.com/typerandom/validator/core"
"reflect"
)
func canWalk(value reflect.Kind) bool {
switch value {
case reflect.Ptr, reflect.Array, reflect.Slice, reflect.Map, reflect.Struct:
return true
default:
return false
}
}
func walkValidateArray(context *context, normalized *core.NormalizedValue, parentField *core.ReflectedField) {
valueType := reflect.ValueOf(normalized.Value)
for i := 0; i < valueType.Len(); i++ {
value := valueType.Index(i)
if canWalk(value.Kind()) {
walkValidate(context, value.Interface(), parentField)
}
}
}
func walkValidateMap(context *context, normalized *core.NormalizedValue, parentField *core.ReflectedField) {
valueType := reflect.ValueOf(normalized.Value)
for _, key := range valueType.MapKeys() {
value := valueType.MapIndex(key)
if canWalk(value.Kind()) {
walkValidate(context, value.Interface(), parentField)
}
}
}
func walkValidateStruct(context *context, normalized *core.NormalizedValue, parentField *core.ReflectedField) {
fields, err := core.GetStructFields(normalized.Value, "validate", context.validator.displayNameTag)
if err != nil {
context.errors.AddPlain(err)
return
}
sourceStruct := reflect.Indirect(reflect.ValueOf(normalized.Value))
for _, field := range fields {
fieldValue := field.GetValue(sourceStruct)
normalizedFieldValue, err := core.Normalize(fieldValue)
if err != nil {
context.errors.AddPlain(err)
continue
}
field.Parent = parentField
context.setField(field)
context.setSource(normalized.Value)
context.setValue(normalizedFieldValue)
var mostRecentErrors core.ErrorList
for _, methods := range field.MethodGroups {
var errors core.ErrorList
for _, method := range methods {
validate, err := context.validator.registry.Get(method.Name)
if err != nil {
context.errors.AddPlain(err)
return
}
if err = validate(context, method.Arguments); err != nil {
errors.Add(core.NewError(field, method, err))
}
}
mostRecentErrors = errors
if !errors.Any() {
break
}
}
if mostRecentErrors.Any() {
context.errors.AddMany(mostRecentErrors)
}
if canWalk(normalizedFieldValue.OriginalKind) {
walkValidate(context, normalizedFieldValue, field)
}
}
}
func walkValidate(context *context, value interface{}, parentField *core.ReflectedField) {
var normalized *core.NormalizedValue
if typedValue, ok := value.(*core.NormalizedValue); ok {
normalized = typedValue
} else {
var err error
normalized, err = core.Normalize(value)
if err != nil {
context.errors.AddPlain(err)
}
}
switch normalized.OriginalKind {
case reflect.Array, reflect.Slice:
walkValidateArray(context, normalized, parentField)
case reflect.Map:
walkValidateMap(context, normalized, parentField)
case reflect.Struct:
if !normalized.IsNil {
walkValidateStruct(context, normalized, parentField)
}
default:
context.errors.AddPlain(errors.New("Unable to directly validate type '" + normalized.OriginalKind.String() + "'."))
}
}