-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate.go
237 lines (203 loc) · 6.24 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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package tagvalidate
import (
"fmt"
"log"
"reflect"
)
type FieldCheck struct {
tag_prefix string `empty:"true" regx:"^[a-zA-Z]\\w+$"`
}
var msgmap = map[int]string{
0: "",
1: "Must be a struct!",
2: "Custom check func didn't pass.",
3: "Validate error.",
}
func GetError(id int, fieldname string, tagname string, tagvalve string, gotvalue interface{}) error {
var err error
pos := fmt.Sprintf("Field:%s tag:%s wanted:%s, Got: %#v", fieldname, tagname, tagvalve, gotvalue)
if id == 0 {
return nil
}
err = fmt.Errorf("%s @%s", msgmap[id], pos)
return err
}
func Check(instance interface{}) error {
var err error
ck := new(FieldCheck)
err = ck.ValidateStruct(instance)
return err
}
func (checker *FieldCheck) getTagName(name string) string {
return checker.tag_prefix + name
}
func (checker *FieldCheck) checkBoolField(val reflect.Value, field reflect.StructField) error {
return nil
}
func (checker *FieldCheck) checkBasicField(val reflect.Value, field reflect.StructField) error {
return nil
}
func reflectCall(ins interface{}, funcname string, arg1 reflect.Value) bool {
return reflectCallV(reflect.ValueOf(ins), funcname, arg1)
}
func reflectCallV(ins reflect.Value, funcname string, arg1 reflect.Value) bool {
var ret bool
ret = true
defer func() {
e := recover()
if e != nil {
if ee, ok := e.(error); ok {
log.Println(ee.Error())
}
}
}()
if method := ins.MethodByName(funcname); method.IsValid() {
mtype := method.Type()
args := make([]reflect.Value, mtype.NumIn())
args[0] = arg1
results := method.Call(args)
if len(results) > 0 {
ret = results[0].Bool()
}
}
return ret
}
func (checker *FieldCheck) getFieldTag(field reflect.StructField, tagname string) (string, bool) {
if tagvalue, ok := field.Tag.Lookup(checker.getTagName(tagname)); ok {
return tagvalue, true
}
return "", false
}
func (checker *FieldCheck) checkFloatField(val reflect.Value, field reflect.StructField) error {
var allowed = []string{
"eq", //(float/[]float)must in those values
"neq", //(float/[]float)must in those values
"zero", //(bool) if allow zero?
"max", //(float) strictly set max
"min", //(float) strictly set min
"func", //(string) given check func name under this struct
}
for _, tagname := range allowed {
if tagvalue, ok := field.Tag.Lookup(checker.getTagName(tagname)); ok {
println(tagname, tagvalue, ok)
} else {
println(tagname, tagvalue, ok)
}
}
return nil
}
func (checker *FieldCheck) checkStructField(val reflect.Value, field reflect.StructField) error {
return nil
}
func (checker *FieldCheck) checkMapField(val reflect.Value, field reflect.StructField) error {
return nil
}
func (checker *FieldCheck) checkPtrField(val reflect.Value, field reflect.StructField) error {
return nil
}
func (checker *FieldCheck) checkSliceField(val reflect.Value, field reflect.StructField) error {
return nil
}
func (checker *FieldCheck) checkFuncField(val reflect.Value, field reflect.StructField) error {
return nil
}
func getKind(val reflect.Value) reflect.Kind {
kind := val.Kind()
switch {
case kind >= reflect.Int && kind <= reflect.Int64:
return reflect.Int
case kind >= reflect.Uint && kind <= reflect.Uint64:
return reflect.Uint
case kind >= reflect.Float32 && kind <= reflect.Float64:
return reflect.Float32
default:
return kind
}
}
func (checker *FieldCheck) checkByType(val reflect.Value, field reflect.StructField) error {
var err error
dataKind := getKind(val)
switch dataKind {
case reflect.Bool:
err = checker.checkBoolField(val, field) //todo
case reflect.Interface:
err = checker.checkBasicField(val, field)
case reflect.String:
err = checker.checkStringField(val, field) // done
case reflect.Int:
err = checker.checkIntField(val, field) // done
case reflect.Uint:
err = checker.checkUintField(val, field) //todo
case reflect.Float32:
err = checker.checkFloatField(val, field) //todo
case reflect.Struct:
err = checker.checkStructField(val, field)
case reflect.Map:
err = checker.checkMapField(val, field)
case reflect.Ptr:
err = checker.checkPtrField(val, field)
case reflect.Slice:
err = checker.checkSliceField(val, field)
case reflect.Func:
err = checker.checkFuncField(val, field)
default:
return fmt.Errorf("Unsupported type: %s", dataKind)
}
return err
}
func (checker *FieldCheck) ValidateStructV(val reflect.Value) error {
// https://stackoverflow.com/questions/24348184/get-pointer-to-value-using-reflection
if !val.IsValid() { //ignore nil
return nil
}
if val.Kind() == reflect.Interface && !val.IsNil() {
elm := val.Elem()
if elm.Kind() == reflect.Ptr && !elm.IsNil() && elm.Elem().Kind() == reflect.Ptr {
val = elm
}
}
oldval := val
if val.Kind() == reflect.Ptr {
val = val.Elem()
}
for i := 0; i < val.NumField(); i++ {
valueField := val.Field(i)
typeField := val.Type().Field(i)
// address := "not-addressable"
if valueField.Kind() == reflect.Interface && !valueField.IsNil() {
elm := valueField.Elem()
if elm.Kind() == reflect.Ptr && !elm.IsNil() && elm.Elem().Kind() == reflect.Ptr {
valueField = elm
}
}
if valueField.Kind() == reflect.Ptr {
valueField = valueField.Elem()
}
// if valueField.CanAddr() {
// address = fmt.Sprintf("0x%X", valueField.Addr().Pointer())
// }
// fmt.Printf("Field Name: %s,\t Field Value: %v,\t Address: %v\t, Field type: %v\t, Field kind: %v\n", typeField.Name,
// valueField.Interface(), address, typeField.Type, valueField.Kind())
if tagvalue, ok := typeField.Tag.Lookup(checker.getTagName("func")); ok {
var checked bool
checked = reflectCallV(oldval, tagvalue, valueField)
if !checked {
return GetError(2, typeField.Name, "func", tagvalue, valueField.Interface())
}
}
err := checker.checkByType(valueField, typeField)
if err != nil {
return err
}
if valueField.Kind() == reflect.Struct {
err = checker.ValidateStructV(valueField)
if err != nil {
return err
}
}
}
return nil
}
func (checker *FieldCheck) ValidateStruct(v interface{}) error {
return checker.ValidateStructV(reflect.ValueOf(v))
}