-
Notifications
You must be signed in to change notification settings - Fork 0
/
check.go
350 lines (299 loc) · 9.91 KB
/
check.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
// Package valuefunc 检测结构体内部数据是否符合某条件
package valuefunc
import (
"errors"
"reflect"
"strings"
"sync"
)
var once sync.Once
func init() {
once.Do(func() {
DefaultValueFunc = New()
})
}
// DefaultValueFunc 提供一个默认
var DefaultValueFunc *ValueFunc
/////////////////////////////////////////////////////////////////////
const (
// NoCheckTagValue tag 不检测的空标志
NoCheckTagValue string = "-"
// CheckTagSplice tag 绑定的检测函数和传入的参数之间的分隔符
DefaultCheckTagSplice string = ":"
DefaultCheckTag string = "check"
)
var (
// checkTag tag 的标签
checkTag = DefaultCheckTag
// checkTagSplice tag 的标分隔符
checkTagSplice = DefaultCheckTagSplice
)
// SetCheckTag 设置标签的 tag
func SetCheckTag(tag string) {
if tag == "" {
return
}
checkTag = tag
}
// SetCheckTagSplice 设置标签的 分隔符
func SetCheckTagSplice(splice string) {
if splice == "" {
return
}
checkTagSplice = splice
}
var (
ErrNilValueFunc error = errors.New("check value func is nil")
ErrNilCheckFuncMap error = errors.New("check func map is nil")
ErrNilCheckFunc error = errors.New("check func is nil")
ErrNilCheckFuncTag error = errors.New("check func tag is empty")
ErrExistCheckFuncTag error = errors.New("check func tag is already exist")
ErrNilCheckInterface error = errors.New("check func input interface is nil")
ErrValueType error = errors.New("check func check value type wring")
)
//////////////////////////////////////////////////////////////////////////
// CheckFunc 检测 v 信息的方式
type CheckFunc func(v reflect.Value, args string) (msg string, err error)
// CheckFuncMap 对应 tag 绑定的检测函数的集合
type CheckFuncMap map[string]CheckFunc
type ValueFunc struct {
CheckFuncMap
sync.RWMutex
checkTag string
checkTagSplice string
}
func New() *ValueFunc {
return &ValueFunc{
RWMutex: sync.RWMutex{},
CheckFuncMap: make(CheckFuncMap),
checkTag: checkTag,
checkTagSplice: checkTagSplice,
}
}
func (vf *ValueFunc) SetCheckFuncMap(cfs CheckFuncMap) {
vf.Lock()
defer vf.Unlock()
if cfs == nil {
return
}
vf.CheckFuncMap = cfs
}
func (vf *ValueFunc) GetCheckFuncMap(tag string) CheckFuncMap {
vf.Lock()
defer vf.Unlock()
return vf.CheckFuncMap
}
func (vf *ValueFunc) SetCheckTag(tag string) {
if tag == "" {
return
}
vf.checkTag = tag
}
// SetCheckTagSplice 设置标签的 分隔符
func (vf *ValueFunc) SetCheckTagSplice(splice string) {
if splice == "" {
return
}
vf.checkTagSplice = splice
}
// Add 添加一个对应 tag 的检测函数
func (vf *ValueFunc) Add(tag string, f CheckFunc) error {
if vf == nil {
return ErrNilValueFunc
}
vf.Lock()
defer vf.Unlock()
if vf.CheckFuncMap == nil {
return ErrNilCheckFuncMap
}
if tag == "" {
return ErrNilCheckFuncTag
}
if f == nil {
return ErrNilCheckFunc
}
if _, ok := vf.CheckFuncMap[tag]; ok {
return ErrExistCheckFuncTag
}
vf.CheckFuncMap[tag] = f
return nil
}
/////////////////////////////////////////
func (vf *ValueFunc) AddInt64(tag string, f func(v int64, isNil bool, args string) (msg string, err error)) error {
return vf.Add(tag, addInt64(f))
}
func (vf *ValueFunc) AddInt32(tag string, f func(v int32, isNil bool, args string) (msg string, err error)) error {
return vf.Add(tag, addInt32(f))
}
func (vf *ValueFunc) AddInt16(tag string, f func(v int16, isNil bool, args string) (msg string, err error)) error {
return vf.Add(tag, addInt16(f))
}
func (vf *ValueFunc) AddInt8(tag string, f func(v int8, isNil bool, args string) (msg string, err error)) error {
return vf.Add(tag, addInt8(f))
}
func (vf *ValueFunc) AddInt(tag string, f func(v int, isNil bool, args string) (msg string, err error)) error {
return vf.Add(tag, addInt(f))
}
func (vf *ValueFunc) AddString(tag string, f func(v string, isNil bool, args string) (msg string, err error)) error {
return vf.Add(tag, addString(f))
}
func (vf *ValueFunc) AddBool(tag string, f func(v bool, isNil bool, args string) (msg string, err error)) error {
return vf.Add(tag, addBool(f))
}
func (vf *ValueFunc) AddUint(tag string, f func(v uint, isNil bool, args string) (msg string, err error)) error {
return vf.Add(tag, addUint(f))
}
func (vf *ValueFunc) AddUint8(tag string, f func(v uint8, isNil bool, args string) (msg string, err error)) error {
return vf.Add(tag, addUint8(f))
}
func (vf *ValueFunc) AddUint16(tag string, f func(v uint16, isNil bool, args string) (msg string, err error)) error {
return vf.Add(tag, addUint16(f))
}
func (vf *ValueFunc) AddUint32(tag string, f func(v uint32, isNil bool, args string) (msg string, err error)) error {
return vf.Add(tag, addUint32(f))
}
func (vf *ValueFunc) AddUint64(tag string, f func(v uint64, isNil bool, args string) (msg string, err error)) error {
return vf.Add(tag, addUint64(f))
}
func (vf *ValueFunc) AddFloat32(tag string, f func(v float32, isNil bool, args string) (msg string, err error)) error {
return vf.Add(tag, addFloat32(f))
}
func (vf *ValueFunc) AddFloat64(tag string, f func(v float64, isNil bool, args string) (msg string, err error)) error {
return vf.Add(tag, addFloat64(f))
}
func (vf *ValueFunc) AddInterface(tag string, f func(v interface{}, isNil bool, args string) (msg string, err error)) error {
return vf.Add(tag, addInterface(f))
}
func (vf *ValueFunc) SetValue(tag string, f func(v reflect.Value, canSet bool, args string) (msg string, err error)) error {
return vf.Add(tag, setValue(f))
}
// Check 通过 cm 检查 in 的信息
func (vf *ValueFunc) Check(in interface{}) (string, error) {
vf.Lock()
defer vf.Unlock()
return check(in, vf)
}
//////////////////////////////////////////////////////////////////////////////////
// Check 通过默认 DefaultValueFunc 检查 in 的信息
func Check(in interface{}) (string, error) {
return DefaultValueFunc.Check(in)
}
// Add 添加一个对应 tag 的检测函数
func Add(tag string, f CheckFunc) error {
return DefaultValueFunc.Add(tag, f)
}
func AddInt64(tag string, f func(v int64, isNil bool, args string) (msg string, err error)) error {
return DefaultValueFunc.AddInt64(tag, f)
}
func AddInt32(tag string, f func(v int32, isNil bool, args string) (msg string, err error)) error {
return DefaultValueFunc.AddInt32(tag, f)
}
func AddInt16(tag string, f func(v int16, isNil bool, args string) (msg string, err error)) error {
return DefaultValueFunc.AddInt16(tag, f)
}
func AddInt8(tag string, f func(v int8, isNil bool, args string) (msg string, err error)) error {
return DefaultValueFunc.AddInt8(tag, f)
}
func AddInt(tag string, f func(v int, isNil bool, args string) (msg string, err error)) error {
return DefaultValueFunc.AddInt(tag, f)
}
func AddString(tag string, f func(v string, isNil bool, args string) (msg string, err error)) error {
return DefaultValueFunc.AddString(tag, f)
}
func AddBool(tag string, f func(v bool, isNil bool, args string) (msg string, err error)) error {
return DefaultValueFunc.AddBool(tag, f)
}
func AddUint(tag string, f func(v uint, isNil bool, args string) (msg string, err error)) error {
return DefaultValueFunc.AddUint(tag, f)
}
func AddUint8(tag string, f func(v uint8, isNil bool, args string) (msg string, err error)) error {
return DefaultValueFunc.AddUint8(tag, f)
}
func AddUint16(tag string, f func(v uint16, isNil bool, args string) (msg string, err error)) error {
return DefaultValueFunc.AddUint16(tag, f)
}
func AddUint32(tag string, f func(v uint32, isNil bool, args string) (msg string, err error)) error {
return DefaultValueFunc.AddUint32(tag, f)
}
func AddUint64(tag string, f func(v uint64, isNil bool, args string) (msg string, err error)) error {
return DefaultValueFunc.AddUint64(tag, f)
}
func AddFloat32(tag string, f func(v float32, isNil bool, args string) (msg string, err error)) error {
return DefaultValueFunc.AddFloat32(tag, f)
}
func AddFloat64(tag string, f func(v float64, isNil bool, args string) (msg string, err error)) error {
return DefaultValueFunc.AddFloat64(tag, f)
}
func AddInterface(tag string, f func(v interface{}, isNil bool, args string) (msg string, err error)) error {
return DefaultValueFunc.AddInterface(tag, f)
}
func SetValue(tag string, f func(v reflect.Value, canSet bool, args string) (msg string, err error)) error {
return DefaultValueFunc.Add(tag, setValue(f))
}
//////////////////////////////////////////////////////////////////////////////////////////
// check 通过 vf.CheckFuncMap 检查 inter 的信息
func check(inter interface{}, vf *ValueFunc) (string, error) {
if inter == nil {
return "", ErrNilCheckInterface
}
if vf == nil {
return "", ErrNilCheckFunc
}
if vf.checkTag == "" {
vf.checkTag = checkTag
}
if vf.checkTagSplice == "" {
vf.checkTagSplice = checkTagSplice
}
if vf.CheckFuncMap == nil {
return "", ErrNilCheckFuncMap
}
sType := reflect.TypeOf(inter)
sValue := reflect.ValueOf(inter)
//fmt.Printf("%d\n", sType.NumField())
for i := 0; i < sType.NumField(); i++ {
stField := sType.Field(i)
stValue := sValue.Field(i)
checkFuncTag := stField.Tag.Get(vf.checkTag)
//fmt.Printf("%q --> %q --> %q\n", stField.Name, stField.Type, checkFuncTag)
if strings.HasPrefix(checkFuncTag, NoCheckTagValue) {
continue
}
switch stField.Type.Kind() {
case reflect.Func, reflect.Chan, reflect.UnsafePointer:
continue
case reflect.Struct:
msg, err := check(stValue.Interface(), vf)
if err != nil {
return msg, err
}
continue
case reflect.Ptr:
if stValue.IsNil() {
continue
}
switch stValue.Elem().Type().Kind() {
case reflect.Struct:
msg, err := check(stValue.Elem().Interface(), vf)
if err != nil {
return msg, err
}
}
}
// 正常的处理
indexI := strings.Index(checkFuncTag, vf.checkTagSplice)
var args string
var fName = checkFuncTag
if indexI > 0 {
fName = checkFuncTag[:indexI]
args = checkFuncTag[indexI+1:]
}
if f, ok := vf.CheckFuncMap[fName]; ok {
msg, err := f(stValue, args)
if err != nil {
return msg, err
}
}
}
return "", nil
}