-
Notifications
You must be signed in to change notification settings - Fork 12
/
composite.go
383 lines (332 loc) · 8.1 KB
/
composite.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
package checker
import (
"fmt"
"reflect"
"strconv"
"strings"
)
type fieldRule struct {
baseRule
subRule Rule
}
func (r *fieldRule) Prompt(prompt string) Rule {
r.prompt = prompt
return r
}
func (r fieldRule) Check(param interface{}) (bool, string) {
exprValue, kind := fetchField(param, &r)
if kind == reflect.Invalid {
return false,
fmt.Sprintf("[%s]:%s cannot be found", r.name, r.getCompleteFieldExpr())
}
if exprValue == nil {
return false,
fmt.Sprintf("[%s]:%s is nil", r.name, r.getCompleteFieldExpr())
}
r.subRule.setUpperFieldExpr(r.getCompleteFieldExpr())
r.subRule.setCache(r.getCache())
isValid, errLog := r.subRule.Check(exprValue)
if !isValid {
if prompt := r.getPrompt(); prompt != "" {
r.Prompt(prompt)
}
}
return isValid, errLog
}
// Field applies rule to fieldExpr
func Field(fieldExpr string, subRule Rule) *fieldRule {
return &fieldRule{
baseRule{
fieldExpr: fieldExpr,
name: "Field",
lowerRule: subRule,
},
subRule,
}
}
type lengthRule struct {
baseRule
le int
ge int
}
func (r *lengthRule) Prompt(prompt string) Rule {
r.prompt = prompt
return r
}
func (r lengthRule) Check(param interface{}) (bool, string) {
exprValue, kind := fetchField(param, &r)
if kind == reflect.Invalid {
return false,
fmt.Sprintf("[%s]:%s cannot be found", r.name, r.getCompleteFieldExpr())
}
if exprValue == nil {
return false,
fmt.Sprintf("[%s]:%s is nil", r.name, r.getCompleteFieldExpr())
}
if kind != reflect.Slice && kind != reflect.Array &&
kind != reflect.String && kind != reflect.Map {
return false,
fmt.Sprintf("[%s]:%s should be slice/array/string/map,actual is %s",
r.name, r.getCompleteFieldExpr(), kind.String())
}
lenValue := reflect.ValueOf(exprValue)
length := lenValue.Len()
if length < r.ge || length > r.le {
return false,
fmt.Sprintf("[%s]:%s length should be between %d and %d,actual is %d",
r.name, r.getCompleteFieldExpr(), r.le, r.ge, length)
}
return true, ""
}
// Length checks if the filed is slice/array/string/map
// and its length is between [ge,le]
func Length(fieldExpr string, ge int, le int) *lengthRule {
return &lengthRule{
baseRule{
fieldExpr: fieldExpr,
name: "Length",
},
le,
ge,
}
}
type arrayRule struct {
baseRule
innerRule Rule
}
func (r *arrayRule) Prompt(prompt string) Rule {
r.prompt = prompt
return r
}
func (r *arrayRule) Check(param interface{}) (bool, string) {
exprValue, kind := fetchField(param, r)
if kind == reflect.Invalid {
return false,
fmt.Sprintf("[%s]:%s cannot be found", r.name, r.getCompleteFieldExpr())
}
if exprValue == nil {
return false,
fmt.Sprintf("[%s]:%s is nil", r.name, r.getCompleteFieldExpr())
}
if kind != reflect.Slice && kind != reflect.Array {
return false,
fmt.Sprintf("[%s]:%s should be slice/array,actual is %s",
r.name, r.getCompleteFieldExpr(), kind.String())
}
arrayValue := reflect.ValueOf(exprValue)
for i := 0; i < arrayValue.Len(); i++ {
r.innerRule.setUpperFieldExpr(r.getCompleteFieldExpr() + "[" + strconv.Itoa(i) + "]")
idxValue := arrayValue.Index(i)
isValid, msg := r.innerRule.Check(idxValue.Interface())
if !isValid {
return false, msg
}
}
return true, ""
}
// Array checks if the filed is slice/array
// and its elements satisfy the innerRule
func Array(fieldExpr string, innerRule Rule) *arrayRule {
return &arrayRule{
baseRule{
fieldExpr: fieldExpr,
name: "Array",
lowerRule: innerRule,
},
innerRule,
}
}
// mapRule checks map's key and value
// keyRule or valueRule can be nil
type mapRule struct {
baseRule
keyRule Rule
valueRule Rule
}
func (r *mapRule) Prompt(prompt string) Rule {
r.prompt = prompt
return r
}
func (r *mapRule) Check(param interface{}) (bool, string) {
exprValue, kind := fetchField(param, r)
if kind == reflect.Invalid {
return false,
fmt.Sprintf("[%s]:%s cannot be found", r.name, r.getCompleteFieldExpr())
}
if exprValue == nil {
return false,
fmt.Sprintf("[%s]:%s is nil", r.name, r.getCompleteFieldExpr())
}
if kind != reflect.Map {
return false,
fmt.Sprintf("[%s]:%s should be kind map,actual is %v",
r.name, r.getCompleteFieldExpr(), kind)
}
refVal := reflect.ValueOf(exprValue)
for _, key := range refVal.MapKeys() {
keyStr := getKeyStr(key)
// check key
if r.keyRule != nil {
r.keyRule.setUpperFieldExpr(r.getCompleteFieldExpr() + "[" + keyStr + "]")
r.keyRule.setCache(r.getCache())
r.lowerRule = r.keyRule
isValid, errMsg := r.keyRule.Check(key.Interface())
if !isValid {
if prompt := r.getPrompt(); prompt != "" {
r.Prompt(prompt)
}
return false, errMsg
}
}
// check value
if r.valueRule != nil {
value := refVal.MapIndex(key)
r.valueRule.setUpperFieldExpr(r.getCompleteFieldExpr() + "[" + keyStr + "] 's value")
r.valueRule.setCache(r.getCache())
r.lowerRule = r.valueRule
isValid, errMsg := r.valueRule.Check(value.Interface())
if !isValid {
if prompt := r.getPrompt(); prompt != "" {
r.Prompt(prompt)
}
return false, errMsg
}
}
}
return true, ""
}
func getKeyStr(value reflect.Value) string {
res := ""
switch value.Kind() {
case reflect.Bool:
res = strconv.FormatBool(value.Bool())
case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int,
reflect.Int64:
res = strconv.FormatInt(value.Int(), 10)
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint,
reflect.Uint64:
res = strconv.FormatUint(value.Uint(), 10)
case reflect.Float32, reflect.Float64:
res = strconv.FormatFloat(value.Float(), 'f', -1, 64)
default:
res = value.String()
}
return res
}
// Map returns mapRule
// keyRule or valueRule can be nil
// if no need to check key/value
func Map(fieldExpr string, keyRule Rule, valueRule Rule) *mapRule {
return &mapRule{
baseRule{
fieldExpr: fieldExpr,
name: "Map",
},
keyRule,
valueRule,
}
}
type andRule struct {
baseRule
rules []Rule
}
func (r *andRule) Prompt(prompt string) Rule {
r.prompt = prompt
return r
}
func (r *andRule) Check(param interface{}) (bool, string) {
for i := 0; i < len(r.rules); i++ {
r.rules[i].setUpperFieldExpr(r.getCompleteFieldExpr())
r.lowerRule = r.rules[i]
r.rules[i].setCache(r.getCache())
isValid, msg := r.rules[i].Check(param)
if !isValid {
if prompt := r.getPrompt(); prompt != "" {
r.Prompt(prompt)
}
return isValid, msg
}
}
return true, ""
}
// And accepts slice of rules
// It passed when all rules passed
func And(rules ...Rule) *andRule {
return &andRule{
baseRule{
name: "And",
},
rules,
}
}
type orRule struct {
baseRule
rules []Rule
}
func (r *orRule) Prompt(prompt string) Rule {
r.prompt = prompt
return r
}
func (r *orRule) Check(param interface{}) (bool, string) {
messages := make([]string, 0, len(r.rules))
prompts := make([]string, 0, len(r.rules))
for i := 0; i < len(r.rules); i++ {
r.rules[i].setUpperFieldExpr(r.getCompleteFieldExpr())
r.lowerRule = r.rules[i]
r.rules[i].setCache(r.getCache())
isValid, msg := r.rules[i].Check(param)
if isValid {
return true, ""
}
if prompt := r.getPrompt(); prompt != "" {
prompts = append(prompts, prompt)
}
messages = append(messages, msg)
}
prompt := strings.Join(prompts, " or ")
r.Prompt(prompt)
return false,
fmt.Sprintf("%s, at least one ot them should be true",
strings.Join(messages, " or "))
}
// Or accepts slice of rules
// It failed when all rules failed
func Or(rules ...Rule) *orRule {
return &orRule{
baseRule{
name: "Or",
},
rules,
}
}
type notRule struct {
baseRule
innerRule Rule
}
func (r *notRule) Prompt(prompt string) Rule {
r.prompt = prompt
return r
}
func (r *notRule) Check(param interface{}) (bool, string) {
r.innerRule.setUpperFieldExpr(r.getCompleteFieldExpr())
isInnerValid, errMsg := r.innerRule.Check(param)
isValid := !isInnerValid
if !isValid {
if prompt := r.getPrompt(); prompt != "" {
r.Prompt(prompt)
}
return false,
fmt.Sprintf("[%s]:[%s] should not be true", r.name, errMsg)
}
return true, ""
}
// Not returns the opposite if innerRule
func Not(innerRule Rule) *notRule {
return ¬Rule{
baseRule{
name: "Not",
lowerRule: innerRule,
},
innerRule,
}
}