forked from graniticio/granitic
/
bool.go
377 lines (268 loc) · 8.09 KB
/
bool.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
// Copyright 2016 Granitic. All rights reserved.
// Use of this source code is governed by an Apache 2.0 license that can be found in the LICENSE file at the root of this project.
package validate
import (
"errors"
"fmt"
"github.com/graniticio/granitic/ioc"
rt "github.com/graniticio/granitic/reflecttools"
"github.com/graniticio/granitic/types"
"strconv"
"strings"
)
const boolRuleCode = "BOOL"
const (
boolOpRequiredCode = commonOpRequired
boolOpStopAllCode = commonOpStopAll
boolOpIsCode = "IS"
boolOpMexCode = commonOpMex
)
type boolValidationOperation uint
const (
boolOpUnsupported = iota
boolOpRequired
boolOpStopAll
boolOpIs
boolOpMex
)
// NewBoolValidationRule creates a new BoolValidationRule to check the named field and the supplied default error code.
func NewBoolValidationRule(field, defaultErrorCode string) *BoolValidationRule {
bv := new(BoolValidationRule)
bv.defaultErrorCode = defaultErrorCode
bv.field = field
bv.codesInUse = types.NewOrderedStringSet([]string{})
bv.dependsFields = determinePathFields(field)
bv.operations = make([]*boolOperation, 0)
bv.codesInUse.Add(bv.defaultErrorCode)
return bv
}
// A ValidationRule for checking a bool or NilableBool field on an object. See the method definitions on this type for
// the supported operations.
type BoolValidationRule struct {
stopAll bool
codesInUse types.StringSet
dependsFields types.StringSet
defaultErrorCode string
field string
missingRequiredCode string
required bool
requiredValue *types.NilableBool
requiredValueCode string
operations []*boolOperation
}
type boolOperation struct {
OpType boolValidationOperation
ErrCode string
MExFields types.StringSet
}
// IsSet returns true if the field to be validation is a bool or is a NilableBool which has been explicitly set.
func (bv *BoolValidationRule) IsSet(field string, subject interface{}) (bool, error) {
value, err := bv.extractValue(field, subject)
if err != nil {
return false, err
}
if value == nil || !value.IsSet() {
return false, nil
} else {
return true, nil
}
}
// See ValidationRule.Validate
func (bv *BoolValidationRule) Validate(vc *ValidationContext) (result *ValidationResult, unexpected error) {
f := bv.field
if vc.OverrideField != "" {
f = vc.OverrideField
}
var value *types.NilableBool
sub := vc.Subject
r := NewValidationResult()
if vc.DirectSubject {
b, found := sub.(*types.NilableBool)
if !found {
m := fmt.Sprintf("Direct validation requested for %s but supplied value is not a *NilableBool", f)
return nil, errors.New(m)
}
value = b
} else {
set, err := bv.IsSet(f, sub)
if err != nil {
return nil, err
} else if !set {
r.Unset = true
if bv.required {
r.AddForField(f, []string{bv.missingRequiredCode})
}
return r, nil
}
//Ignoring error as called previously during IsSet
value, _ = bv.extractValue(f, sub)
}
if bv.requiredValue != nil && value.Bool() != bv.requiredValue.Bool() {
r.AddForField(f, []string{bv.requiredValueCode})
}
err := bv.runOperations(f, value.Bool(), vc, r)
return r, err
}
func (bv *BoolValidationRule) runOperations(field string, b bool, vc *ValidationContext, r *ValidationResult) error {
ec := types.NewEmptyOrderedStringSet()
for _, op := range bv.operations {
switch op.OpType {
case boolOpMex:
checkMExFields(op.MExFields, vc, ec, op.ErrCode)
}
}
r.AddForField(field, ec.Contents())
return nil
}
func (bv *BoolValidationRule) extractValue(f string, s interface{}) (*types.NilableBool, error) {
v, err := rt.FindNestedField(rt.ExtractDotPath(f), s)
if err != nil {
return nil, err
}
if rt.NilPointer(v) {
return nil, nil
}
b, found := v.Interface().(bool)
if found {
return types.NewNilableBool(b), nil
}
nsb, found := v.Interface().(*types.NilableBool)
if found {
return nsb, nil
}
m := fmt.Sprintf("%s is not a bool or *NilableBool.", f)
return nil, errors.New(m)
}
// See ValidationRule.StopAllOnFail
func (bv *BoolValidationRule) StopAllOnFail() bool {
return bv.stopAll
}
// See ValidationRule.CodesInUse
func (bv *BoolValidationRule) CodesInUse() types.StringSet {
return bv.codesInUse
}
// See ValidationRule.DependsOnFields
func (bv *BoolValidationRule) DependsOnFields() types.StringSet {
return bv.dependsFields
}
// StopAll indicates that no further rules should be rule if this one fails.
func (bv *BoolValidationRule) StopAll() *BoolValidationRule {
bv.stopAll = true
return bv
}
// Required adds a check see if the field under validation has been set.
func (bv *BoolValidationRule) Required(code ...string) *BoolValidationRule {
bv.required = true
bv.missingRequiredCode = bv.chooseErrorCode(code)
return bv
}
// Is adds a check to see if the field is set to the supplied value.
func (bv *BoolValidationRule) Is(v bool, code ...string) *BoolValidationRule {
bv.requiredValue = types.NewNilableBool(v)
bv.requiredValueCode = bv.chooseErrorCode(code)
return bv
}
// MEx adds a check to see if any other of the fields with which this field is mutually exclusive have been set.
func (bv *BoolValidationRule) MEx(fields types.StringSet, code ...string) *BoolValidationRule {
op := new(boolOperation)
op.ErrCode = bv.chooseErrorCode(code)
op.OpType = boolOpMex
op.MExFields = fields
bv.addOperation(op)
return bv
}
func (bv *BoolValidationRule) addOperation(o *boolOperation) {
bv.operations = append(bv.operations, o)
}
func (bv *BoolValidationRule) chooseErrorCode(v []string) string {
if len(v) > 0 {
bv.codesInUse.Add(v[0])
return v[0]
} else {
return bv.defaultErrorCode
}
}
func (bv *BoolValidationRule) operation(c string) (boolValidationOperation, error) {
switch c {
case boolOpRequiredCode:
return boolOpRequired, nil
case boolOpStopAllCode:
return boolOpStopAll, nil
case boolOpIsCode:
return boolOpIs, nil
case boolOpMexCode:
return boolOpMex, nil
}
m := fmt.Sprintf("Unsupported bool validation operation %s", c)
return boolOpUnsupported, errors.New(m)
}
func newBoolValidationRuleBuilder(ec string, cf ioc.ComponentByNameFinder) *boolValidationRuleBuilder {
bv := new(boolValidationRuleBuilder)
bv.componentFinder = cf
bv.defaultErrorCode = ec
return bv
}
type boolValidationRuleBuilder struct {
defaultErrorCode string
componentFinder ioc.ComponentByNameFinder
}
func (vb *boolValidationRuleBuilder) parseRule(field string, rule []string) (ValidationRule, error) {
defaultErrorcode := determineDefaultErrorCode(boolRuleCode, rule, vb.defaultErrorCode)
bv := NewBoolValidationRule(field, defaultErrorcode)
for _, v := range rule {
ops := decomposeOperation(v)
opCode := ops[0]
if isTypeIndicator(boolRuleCode, opCode) {
continue
}
op, err := bv.operation(opCode)
if err != nil {
return nil, err
}
switch op {
case boolOpRequired:
err = vb.markRequired(field, ops, bv)
case boolOpStopAll:
bv.StopAll()
case boolOpIs:
err = vb.captureRequiredValue(field, ops, bv)
case boolOpMex:
err = vb.captureExclusiveFields(field, ops, bv)
}
if err != nil {
return nil, err
}
}
return bv, nil
}
func (vb *boolValidationRuleBuilder) captureExclusiveFields(field string, ops []string, bv *BoolValidationRule) error {
_, err := paramCount(ops, "MEX", field, 2, 3)
if err != nil {
return err
}
members := strings.SplitN(ops[1], setMemberSep, -1)
fields := types.NewOrderedStringSet(members)
bv.MEx(fields, extractVargs(ops, 3)...)
return nil
}
func (vb *boolValidationRuleBuilder) captureRequiredValue(field string, ops []string, bv *BoolValidationRule) error {
_, err := paramCount(ops, "Is", field, 2, 3)
if err != nil {
return err
}
b, err := strconv.ParseBool(ops[1])
if err != nil {
m := fmt.Sprintf("Value %s prbvided as part of a BOOL/IS operation could not be interpreted as a bool\n", ops[1])
return errors.New(m)
}
bv.Is(b, extractVargs(ops, 3)...)
return nil
}
func (vb *boolValidationRuleBuilder) markRequired(field string, ops []string, bv *BoolValidationRule) error {
_, err := paramCount(ops, "Required", field, 1, 2)
if err != nil {
return err
}
bv.Required(extractVargs(ops, 2)...)
return nil
}