-
Notifications
You must be signed in to change notification settings - Fork 1
/
flow_control.go
326 lines (268 loc) · 10 KB
/
flow_control.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
package validation
import (
"context"
"sync"
)
// WhenArgument is used to build conditional validation. Use the [When] function to initiate a conditional check.
// If the condition is true, then the arguments passed through the [WhenArgument.Then] function will be processed.
// Otherwise, the arguments passed through the [WhenArgument.Else] function will be processed.
type WhenArgument struct {
isTrue bool
path []PropertyPathElement
thenArguments []Argument
elseArguments []Argument
}
// When function is used to initiate conditional validation.
// If the condition is true, then the arguments passed through the [WhenArgument.Then] function will be processed.
// Otherwise, the arguments passed through the [WhenArgument.Else] function will be processed.
func When(isTrue bool) WhenArgument {
return WhenArgument{isTrue: isTrue}
}
// Then function is used to set a sequence of arguments to be processed if the condition is true.
func (arg WhenArgument) Then(arguments ...Argument) WhenArgument {
arg.thenArguments = arguments
return arg
}
// Else function is used to set a sequence of arguments to be processed if a condition is false.
func (arg WhenArgument) Else(arguments ...Argument) WhenArgument {
arg.elseArguments = arguments
return arg
}
// At returns a copy of [WhenArgument] with appended property path suffix.
func (arg WhenArgument) At(path ...PropertyPathElement) WhenArgument {
arg.path = append(arg.path, path...)
return arg
}
func (arg WhenArgument) setUp(ctx *executionContext) {
ctx.addValidation(arg.validate, arg.path...)
}
func (arg WhenArgument) validate(ctx context.Context, validator *Validator) (*ViolationList, error) {
var err error
if arg.isTrue {
err = validator.Validate(ctx, arg.thenArguments...)
} else {
err = validator.Validate(ctx, arg.elseArguments...)
}
return unwrapViolationList(err)
}
// WhenGroupsArgument is used to build conditional validation based on groups. Use the [WhenGroups] function
// to initiate a conditional check. If validation group matches to the validator one,
// then the arguments passed through the [WhenGroupsArgument.Then] function will be processed.
// Otherwise, the arguments passed through the [WhenGroupsArgument.Else] function will be processed.
type WhenGroupsArgument struct {
groups []string
path []PropertyPathElement
thenArguments []Argument
elseArguments []Argument
}
// WhenGroups is used to build conditional validation based on groups. If validation group matches to
// the validator one, then the arguments passed through the [WhenGroupsArgument.Then] function will be processed.
// Otherwise, the arguments passed through the [WhenGroupsArgument.Else] function will be processed.
func WhenGroups(groups ...string) WhenGroupsArgument {
return WhenGroupsArgument{groups: groups}
}
// Then function is used to set a sequence of arguments to be processed if the validation group is active.
func (arg WhenGroupsArgument) Then(arguments ...Argument) WhenGroupsArgument {
arg.thenArguments = arguments
return arg
}
// Else function is used to set a sequence of arguments to be processed if the validation group is active.
func (arg WhenGroupsArgument) Else(arguments ...Argument) WhenGroupsArgument {
arg.elseArguments = arguments
return arg
}
// At returns a copy of [WhenGroupsArgument] with appended property path suffix.
func (arg WhenGroupsArgument) At(path ...PropertyPathElement) WhenGroupsArgument {
arg.path = append(arg.path, path...)
return arg
}
func (arg WhenGroupsArgument) setUp(ctx *executionContext) {
ctx.addValidation(arg.validate, arg.path...)
}
func (arg WhenGroupsArgument) validate(ctx context.Context, validator *Validator) (*ViolationList, error) {
var err error
if validator.IsIgnoredForGroups(arg.groups...) {
err = validator.Validate(ctx, arg.elseArguments...)
} else {
err = validator.Validate(ctx, arg.thenArguments...)
}
return unwrapViolationList(err)
}
// SequentialArgument can be used to interrupt validation process when the first violation is raised.
type SequentialArgument struct {
isIgnored bool
path []PropertyPathElement
arguments []Argument
}
// Sequentially function used to run validation process step-by-step.
func Sequentially(arguments ...Argument) SequentialArgument {
return SequentialArgument{arguments: arguments}
}
// At returns a copy of [SequentialArgument] with appended property path suffix.
func (arg SequentialArgument) At(path ...PropertyPathElement) SequentialArgument {
arg.path = append(arg.path, path...)
return arg
}
// When enables conditional validation of this argument. If the expression evaluates to false,
// then the argument will be ignored.
func (arg SequentialArgument) When(condition bool) SequentialArgument {
arg.isIgnored = !condition
return arg
}
func (arg SequentialArgument) setUp(ctx *executionContext) {
ctx.addValidation(arg.validate, arg.path...)
}
func (arg SequentialArgument) validate(ctx context.Context, validator *Validator) (*ViolationList, error) {
if arg.isIgnored {
return nil, nil
}
violations := &ViolationList{}
for _, argument := range arg.arguments {
err := violations.AppendFromError(validator.Validate(ctx, argument))
if err != nil {
return nil, err
}
if violations.len > 0 {
return violations, nil
}
}
return violations, nil
}
// AtLeastOneOfArgument can be used to set up validation process to check that the value satisfies
// at least one of the given constraints. The validation stops as soon as one constraint is satisfied.
type AtLeastOneOfArgument struct {
isIgnored bool
path []PropertyPathElement
arguments []Argument
}
// AtLeastOneOf can be used to set up validation process to check that the value satisfies
// at least one of the given constraints. The validation stops as soon as one constraint is satisfied.
func AtLeastOneOf(arguments ...Argument) AtLeastOneOfArgument {
return AtLeastOneOfArgument{arguments: arguments}
}
// At returns a copy of [AtLeastOneOfArgument] with appended property path suffix.
func (arg AtLeastOneOfArgument) At(path ...PropertyPathElement) AtLeastOneOfArgument {
arg.path = append(arg.path, path...)
return arg
}
// When enables conditional validation of this argument. If the expression evaluates to false,
// then the argument will be ignored.
func (arg AtLeastOneOfArgument) When(condition bool) AtLeastOneOfArgument {
arg.isIgnored = !condition
return arg
}
func (arg AtLeastOneOfArgument) setUp(ctx *executionContext) {
ctx.addValidation(arg.validate, arg.path...)
}
func (arg AtLeastOneOfArgument) validate(ctx context.Context, validator *Validator) (*ViolationList, error) {
if arg.isIgnored {
return nil, nil
}
violations := &ViolationList{}
for _, argument := range arg.arguments {
violation := validator.Validate(ctx, argument)
if violation == nil {
return nil, nil
}
err := violations.AppendFromError(violation)
if err != nil {
return nil, err
}
}
return violations, nil
}
// AllArgument can be used to interrupt validation process when the first violation is raised.
type AllArgument struct {
isIgnored bool
path []PropertyPathElement
arguments []Argument
}
// All runs validation for each argument. It works exactly as [Validator.Validate] method.
// It can be helpful to build complex validation process.
func All(arguments ...Argument) AllArgument {
return AllArgument{arguments: arguments}
}
// AtProperty can be used to group different kind of validations for a specific property.
// It creates an AllArgument option and executes validation on all the arguments.
func AtProperty(propertyName string, arguments ...Argument) AllArgument {
return All(arguments...).At(PropertyName(propertyName))
}
// At returns a copy of [AllArgument] with appended property path suffix.
func (arg AllArgument) At(path ...PropertyPathElement) AllArgument {
arg.path = append(arg.path, path...)
return arg
}
// When enables conditional validation of this argument. If the expression evaluates to false,
// then the argument will be ignored.
func (arg AllArgument) When(condition bool) AllArgument {
arg.isIgnored = !condition
return arg
}
func (arg AllArgument) setUp(ctx *executionContext) {
ctx.addValidation(arg.validate, arg.path...)
}
func (arg AllArgument) validate(ctx context.Context, validator *Validator) (*ViolationList, error) {
if arg.isIgnored {
return nil, nil
}
violations := &ViolationList{}
for _, argument := range arg.arguments {
err := violations.AppendFromError(validator.Validate(ctx, argument))
if err != nil {
return nil, err
}
}
return violations, nil
}
// AsyncArgument can be used to interrupt validation process when the first violation is raised.
type AsyncArgument struct {
isIgnored bool
path []PropertyPathElement
arguments []Argument
}
// Async implements async/await pattern and runs validation for each argument in a separate goroutine.
func Async(arguments ...Argument) AsyncArgument {
return AsyncArgument{arguments: arguments}
}
// At returns a copy of [AsyncArgument] with appended property path suffix.
func (arg AsyncArgument) At(path ...PropertyPathElement) AsyncArgument {
arg.path = append(arg.path, path...)
return arg
}
// When enables conditional validation of this argument. If the expression evaluates to false,
// then the argument will be ignored.
func (arg AsyncArgument) When(condition bool) AsyncArgument {
arg.isIgnored = !condition
return arg
}
func (arg AsyncArgument) setUp(ctx *executionContext) {
ctx.addValidation(arg.validate, arg.path...)
}
func (arg AsyncArgument) validate(ctx context.Context, validator *Validator) (*ViolationList, error) {
if arg.isIgnored {
return nil, nil
}
ctx, cancel := context.WithCancel(ctx)
defer cancel()
waiter := &sync.WaitGroup{}
waiter.Add(len(arg.arguments))
errs := make(chan error)
for _, argument := range arg.arguments {
go func(argument Argument) {
defer waiter.Done()
errs <- validator.Validate(ctx, argument)
}(argument)
}
go func() {
waiter.Wait()
close(errs)
}()
violations := &ViolationList{}
for violation := range errs {
err := violations.AppendFromError(violation)
if err != nil {
return nil, err
}
}
return violations, nil
}