-
Notifications
You must be signed in to change notification settings - Fork 0
/
rules.go
630 lines (512 loc) · 11.9 KB
/
rules.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
// SPDX-License-Identifier: Apache-2.0
// Copyright 2023 The Prime Citizens
package cli
import "io"
type ViolationCode uint32
const (
ViolationCodeNoViolation ViolationCode = iota
ViolationCodeEmptyAllOf // for RuleAllOf: none present.
ViolationCodePartialAllOf // for RuleAllOf: at least one present but not all present.
ViolationCodePartialAllOrNone // for RuleAllOrNone: at least one present but not all present.
ViolationCodeExcessiveOneOf // for RuleOneOf: more than one present.
ViolationCodeEmptyOneOf // for RuleOneOf: none present.
ViolationCodeEmptyAnyOf // for RuleAnyOf: none present.
)
type Violation struct {
Key string
Reason ViolationCode
}
// An Inspector is an external verifier used to check rule enforcement.
type Inspector interface {
// CheckFlagValueChanged returns ture if the flag's value has been changed.
CheckFlagValueChanged(key string) bool
}
// Rule
type Rule interface {
// Requires returns true if this rule requires Inspector.CheckFlagValueChanged() to
// return true for this key to avoid violation.
Requires(key string) bool
// Contains returns true if the rule has something to do with the key.
Contains(key string) bool
// NthEx returns (i-th violation, true), if any, otherwise (_, false).
NthEx(f Inspector, i int) (Violation, bool)
// WriteFlagRule writes text representation of the rule.
//
// If len(keys) > 0, only add rule applies to at least one of the keys,
// otherwise add all.
WriteFlagRule(out io.Writer, keys ...string) (int, error)
}
func RuleContainsAny[R Rule](rule R, keys ...string) bool {
for _, arg := range keys {
if rule.Contains(arg) {
return true
}
}
return false
}
func RuleRequiresAny[R Rule](rule R, keys ...string) bool {
for _, arg := range keys {
if rule.Requires(arg) {
return true
}
}
return false
}
// MergeFlagRules merges multiple Rules into one.
func MergeFlagRules(rules ...Rule) Rule {
switch len(rules) {
case 0:
return RuleAny{}
case 1:
return rules[0]
default:
return &MultiRule{Rules: rules}
}
}
// RuleAny
type RuleAny struct{}
func (RuleAny) Requires(key string) bool { return false }
func (RuleAny) Contains(key string) bool { return false }
func (RuleAny) NthEx(Inspector, int) (Violation, bool) { return Violation{}, false }
func (RuleAny) WriteFlagRule(io.Writer, ...string) (int, error) { return 0, nil }
// MultiRule
type MultiRule struct {
Rules []Rule
}
func (r *MultiRule) Requires(key string) bool {
for _, rule := range r.Rules {
if rule.Requires(key) {
return true
}
}
return false
}
func (r *MultiRule) Contains(key string) bool {
for _, rule := range r.Rules {
if rule.Contains(key) {
return true
}
}
return false
}
// NthEx implements [Rule].
func (r *MultiRule) NthEx(f Inspector, i int) (Violation, bool) {
for _, rule := range r.Rules {
for j := 0; ; j++ {
v, ok := rule.NthEx(f, j)
if !ok {
break
}
if i == 0 {
return v, true
}
i--
}
}
return Violation{}, false
}
func (r *MultiRule) WriteFlagRule(out io.Writer, keys ...string) (n int, err error) {
var x int
for _, rule := range r.Rules {
if len(keys) != 0 && !RuleContainsAny(rule, keys...) {
x = 0
continue
}
if x > 0 {
// last rule wrote at least one tag
x, err = wstr(out, " & ")
n += x
if err != nil {
return
}
}
x, err = rule.WriteFlagRule(out, keys...)
n += x
if err != nil {
return
}
}
return
}
// AllOf creates a *RuleAllOf from provided keys.
func AllOf(keys ...string) *RuleAllOf {
return &RuleAllOf{Keys: keys}
}
type RuleAllOf struct {
Keys []string
}
func (r *RuleAllOf) Requires(key string) bool { return sliceContains(r.Keys, key) }
func (r *RuleAllOf) Contains(key string) bool { return sliceContains(r.Keys, key) }
// NthEx implements [Rule].
func (r *RuleAllOf) NthEx(f Inspector, i int) (Violation, bool) {
if i < 0 || r == nil {
return Violation{}, false
}
var (
someNotSet, someSet bool
idxFirstNotSet int
)
for j, name := range r.Keys {
if f.CheckFlagValueChanged(name) {
someSet = true
if someNotSet {
break
}
continue
}
if !someNotSet {
idxFirstNotSet = j
someNotSet = true
}
if someSet {
break
}
}
if someNotSet {
for cur, j := 0, idxFirstNotSet; j < len(r.Keys); j++ {
if f.CheckFlagValueChanged(r.Keys[j]) {
continue
}
if cur == i {
if someSet {
return Violation{Key: r.Keys[j], Reason: ViolationCodePartialAllOf}, true
}
return Violation{Key: r.Keys[j], Reason: ViolationCodeEmptyAllOf}, true
}
cur++
}
}
return Violation{}, false
}
func (r *RuleAllOf) WriteFlagRule(out io.Writer, keys ...string) (n int, err error) {
if len(keys) == 0 || RuleContainsAny(r, keys...) {
return formatFlagRuleTags(out, "allof[", "]", r.Keys)
}
return 0, nil
}
// AllOrNone creates a *RuleAllOrNone from provided keys.
func AllOrNone(keys ...string) *RuleAllOrNone {
return &RuleAllOrNone{Keys: keys}
}
// RuleAllOrNone represents a group of keys MUST either all set or all not set.
type RuleAllOrNone struct {
Keys []string
}
func (r *RuleAllOrNone) Requires(key string) bool { return false }
func (r *RuleAllOrNone) Contains(key string) bool { return sliceContains(r.Keys, key) }
// NthEx implements [Rule].
func (r *RuleAllOrNone) NthEx(f Inspector, i int) (Violation, bool) {
if i < 0 || r == nil {
return Violation{}, false
}
var (
someSet, someNotSet bool
idxFirstNotSet int
)
for j, name := range r.Keys {
if f.CheckFlagValueChanged(name) {
someSet = true
if someNotSet {
break
}
continue
}
if !someNotSet {
idxFirstNotSet = j
someNotSet = true
if someSet {
break
}
}
}
if someSet && someNotSet { // violation presents
for cur, j := 0, idxFirstNotSet; j < len(r.Keys); j++ {
if f.CheckFlagValueChanged(r.Keys[j]) {
continue
}
if cur == i {
return Violation{Key: r.Keys[j], Reason: ViolationCodePartialAllOrNone}, true
}
cur++
}
}
return Violation{}, false
}
func (r *RuleAllOrNone) WriteFlagRule(out io.Writer, keys ...string) (int, error) {
if len(keys) == 0 || RuleContainsAny(r, keys...) {
return formatFlagRuleTags(out, "allOrNone[", "]", r.Keys)
}
return 0, nil
}
// OneOf creates a *RuleOneOf from provided keys.
func OneOf(keys ...string) *RuleOneOf {
return &RuleOneOf{Keys: keys}
}
// RuleOneOf defines a group of mutually exclusive flags.
type RuleOneOf struct {
Keys []string
}
func (r *RuleOneOf) Requires(key string) bool { return singleRequire(r.Keys, key) }
func (r *RuleOneOf) Contains(key string) bool { return sliceContains(r.Keys, key) }
// NthEx implements [Rule].
func (r *RuleOneOf) NthEx(f Inspector, i int) (Violation, bool) {
if i < 0 || r == nil {
return Violation{}, false
}
var (
someSet bool
idxSecondSet int
)
for j, name := range r.Keys {
if !f.CheckFlagValueChanged(name) {
continue
}
if !someSet {
someSet = true
} else {
idxSecondSet = j
break
}
}
if !someSet {
if i < len(r.Keys) && i >= 0 {
return Violation{Key: r.Keys[i], Reason: ViolationCodeEmptyOneOf}, true
}
return Violation{}, false
}
if idxSecondSet != 0 {
for cur, j := 0, idxSecondSet; j < len(r.Keys); j++ {
if !f.CheckFlagValueChanged(r.Keys[j]) {
continue
}
if cur == i {
return Violation{Key: r.Keys[j], Reason: ViolationCodeExcessiveOneOf}, true
}
cur++
}
}
return Violation{}, false
}
func (r *RuleOneOf) WriteFlagRule(out io.Writer, keys ...string) (int, error) {
if len(keys) == 0 || RuleContainsAny(r, keys...) {
return formatFlagRuleTags(out, "oneof[", "]", r.Keys)
}
return 0, nil
}
// AnyOf creates a *RuleAnyOf from provided keys.
func AnyOf(keys ...string) *RuleAnyOf {
return &RuleAnyOf{Keys: keys}
}
// RuleAnyOf represents a group of flags that at least one MUST present.
type RuleAnyOf struct {
Keys []string
}
func (r *RuleAnyOf) Requires(key string) bool { return singleRequire(r.Keys, key) }
func (r *RuleAnyOf) Contains(key string) bool { return sliceContains(r.Keys, key) }
// NthEx implements [Rule].
func (r *RuleAnyOf) NthEx(f Inspector, i int) (Violation, bool) {
if i < 0 || r == nil {
return Violation{}, false
}
var someSet bool
for _, name := range r.Keys {
if f.CheckFlagValueChanged(name) {
someSet = true
break
}
}
if !someSet && i < len(r.Keys) && i >= 0 {
return Violation{Key: r.Keys[i], Reason: ViolationCodeEmptyAnyOf}, true
}
return Violation{}, false
}
func (r *RuleAnyOf) WriteFlagRule(out io.Writer, keys ...string) (int, error) {
if len(keys) == 0 || RuleContainsAny(r, keys...) {
return formatFlagRuleTags(out, "anyof[", "]", r.Keys)
}
return 0, nil
}
// DependOn creates a *RuleDepends from the given condition and branches.
func DependOn[X, Y, Z Rule](ifX X, thenY Y, elseZ Z) *RuleDepends[X, Y, Z] {
return &RuleDepends[X, Y, Z]{
If: ifX,
Then: thenY,
Else: elseZ,
}
}
// RuleDepends represents a group of flags required only when dependent rules
// are met.
type RuleDepends[X, Y, Z Rule] struct {
// If rules in `If` produces no violation, `Then` is used to decide
// this rule has been satisfied if `Then` produces no violation.
//
// `Else` is used as `Then` if `If` produced violation.
//
// It is valid that `If` contains no rule.
If X
Then Y
Else Z
}
func (r *RuleDepends[X, Y, Z]) Requires(key string) bool {
return r.Then.Requires(key) && r.Else.Requires(key)
}
func (r *RuleDepends[X, Y, Z]) Contains(key string) bool {
return r.If.Contains(key) || r.Then.Contains(key) || r.Else.Contains(key)
}
// NthEx implements [Rule].
func (r *RuleDepends[X, Y, Z]) NthEx(f Inspector, i int) (p Violation, ok bool) {
if i < 0 || r == nil {
return p, false
}
useThen := true
// only need to check whether there is violation, so passing `0` is fine.
_, ok = r.If.NthEx(f, 0)
if ok {
useThen = false
}
if useThen {
for k := 0; ; k++ {
p, ok = r.Then.NthEx(f, k)
if !ok {
break
}
if i == 0 {
return p, true
}
i--
}
} else {
for k := 0; ; k++ {
p, ok = r.Else.NthEx(f, k)
if !ok {
break
}
if i == 0 {
return p, true
}
i--
}
}
return Violation{}, false
}
func (r *RuleDepends[X, Y, Z]) WriteFlagRule(out io.Writer, keys ...string) (n int, err error) {
n, err = wstr(out, "(if ")
if err != nil {
return
}
x, err := r.If.WriteFlagRule(out, keys...)
n += x
if err != nil {
return
}
if x == 0 {
x, err = wstr(out, "nop")
n += x
if err != nil {
return
}
}
x, err = wstr(out, "; then ")
n += x
if err != nil {
return
}
x, err = r.Then.WriteFlagRule(out, keys...)
n += x
if err != nil {
return
}
if x == 0 {
x, err = wstr(out, "nop")
n += x
if err != nil {
return
}
}
x, err = wstr(out, "; else ")
n += x
if err != nil {
return
}
x, err = r.Else.WriteFlagRule(out)
n += x
if err != nil {
return
}
if x == 0 {
x, err = wstr(out, "nop)")
n += x
if err != nil {
return
}
}
return
}
func sliceContains(ss []string, keys ...string) bool {
for _, x := range ss {
for _, y := range keys {
if x == y {
return true
}
}
}
return false
}
func singleRequire(slice []string, keys ...string) bool {
hasKey := false
for _, k := range slice {
if len(k) == 0 {
continue
}
if !sliceContains(keys, k) {
return false
}
hasKey = true
}
return hasKey
}
func formatFlagRuleTags(
out io.Writer,
prefix, suffix string,
ruleKeys []string,
) (n int, err error) {
var (
x int
wrote bool
)
n, err = wstr(out, prefix)
if err != nil {
return
}
for _, key := range ruleKeys {
if len(key) == 0 {
continue
}
if wrote {
x, err = wstr(out, ", ")
n += x
if err != nil {
return
}
} else {
wrote = true
}
if IsShorthand(key) {
x, err = wstr(out, "-")
} else {
x, err = wstr(out, "--")
}
n += x
if err != nil {
return
}
x, err = wstr(out, key)
n += x
if err != nil {
return
}
}
x, err = wstr(out, suffix)
n += x
return
}