This repository has been archived by the owner on Oct 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
filter.go
1138 lines (951 loc) · 32.7 KB
/
filter.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
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package aci
/*
filter.go contains types, functions, methods and constants that pertain to basic
LDAP Search Filter concepts, as well as TargetRule filter-related abstracts that
bear the TargetAttrFilters or TargetFilter Keyword contexts.
*/
var (
badSearchFilter SearchFilter // for failed calls that return a SearchFilter only
badFilter string = `<invalid_search_filter>`
)
/*
SearchFilter is a struct type that embeds an LDAP search filter. Instances of this type
may be used in a variety of areas, from [LDAPURI] composition to [TargetFilter] rules.
*/
type SearchFilter struct {
*searchFilter
}
/*
searchFilter is a private (pointer!) type embedded within instances of SearchFilter.
*/
type searchFilter struct {
string
}
/*
IsZero returns a Boolean value indicative of whether the receiver is nil, or unset.
*/
func (r SearchFilter) IsZero() bool {
if r.searchFilter == nil {
return true
}
return len((*r.searchFilter).string) == 0
}
/*
Valid -- at the moment -- performs a naïve check on the receiver to determine whether
the value is defined. This method may, in the future, introduce more sophisticated
checks to increase its value, such as counting (unescaped) parenthetical openers and
closers to verify the effective 'balance' of the expression.
For now, this method is little more than an inverse counterpart to IsZero that returns
an instance of error instead of a Boolean.
*/
func (r SearchFilter) Valid() (err error) {
if r.IsZero() {
err = nilInstanceErr(r)
}
//TODO - add filter checks/decompiler? maybe. maybe not.
return
}
/*
TRM returns an instance of [TargetRuleMethods].
Each of the return instance's key values represent a single instance of
the [ComparisonOperator] type that is allowed for use in the creation of
[TargetRule] instances which bear the receiver instance as an expression
value. The value for each key is the actual [TargetRuleMethod] instance for
OPTIONAL use in the creation of a [TargetRule] instance.
This is merely a convenient alternative to maintaining knowledge of which
[ComparisonOperator] instances apply to which types. Instances of this type
are also used to streamline package unit tests.
Please note that if the receiver is in an aberrant state, or if it has not
yet been initialized, the execution of ANY of the return instance's value
methods will return bogus [TargetRule] instances. While this is useful in unit
testing, the end user must only execute this method IF and WHEN the receiver
has been properly populated and prepared for such activity.
*/
func (r SearchFilter) TRM() TargetRuleMethods {
return newTargetRuleMethods(targetRuleFuncMap{
Eq: r.Eq,
Ne: r.Ne,
})
}
/*
Filter initializes (and optionally sets) a new instance of [SearchFilter].
Instances of this kind are used in [LDAPURI] instances, as well as certain
target rules.
*/
func Filter(x ...string) (r SearchFilter) {
r = SearchFilter{newSearchFilter()}
if len(x) > 0 {
r.searchFilter.set(x[0])
}
return
}
/*
newSearchFilter is a private function called by Filter during an attempt
to create a new instance of SearchFilter, which is returned as a pointer
reference.
*/
func newSearchFilter() (f *searchFilter) {
f = new(searchFilter)
return
}
/*
Keyword returns the [Keyword] associated with the receiver instance. In
the context of this type instance, the [Keyword] returned is always [TargetFilter].
*/
func (r SearchFilter) Keyword() Keyword {
return TargetFilter
}
/*
String is a stringer method that returns the string representation of
an LDAP Search Filter.
*/
func (r SearchFilter) String() string {
if r.searchFilter == nil {
return ``
}
return r.searchFilter.string
}
/*
Compare returns a Boolean value indicative of a SHA-1 comparison
between the receiver (r) and input value x.
*/
func (r SearchFilter) Compare(x any) bool {
return compareHashInstance(r, x)
}
/*
Set assigns the provided value as the LDAP Search Filter instance within the
receiver. Note that this should only be done once, as filters cannot easily
built "incrementally" by the user.
*/
func (r *SearchFilter) Set(x string) *SearchFilter {
if r.searchFilter == nil {
r.searchFilter = newSearchFilter()
}
r.searchFilter.set(x)
return r
}
/*
set is a private method executed by SearchFilter.Set.
*/
func (r *searchFilter) set(x string) {
if len(x) == 0 {
return
}
r.string = x
}
/*
Eq initializes and returns a new [TargetRule] instance configured to express the evaluation of the receiver value as Equal-To a [TargetFilter] [TargetKeyword] context.
*/
func (r SearchFilter) Eq() TargetRule {
if r.IsZero() {
return badTargetRule
}
return TR(TargetFilter, Eq, r)
}
/*
Ne initializes and returns a new [TargetRule] instance configured to express the evaluation of the receiver value as Not-Equal-To a [TargetFilter] [TargetKeyword] context.
Negated equality [TargetRule] instances should be used with caution.
*/
func (r SearchFilter) Ne() TargetRule {
if r.IsZero() {
return badTargetRule
}
return TR(TargetFilter, Ne, r)
}
/*
AttributeFilter is a struct type that embeds an [AttributeType] and filter-style [TargetRule].
Instances of this type are a component in the creation of [TargetRule] definitions based upon the [TargetAttrFilters] [TargetKeyword] context.
*/
type AttributeFilter struct {
*atf
}
/*
atf is the embedded type (as a pointer!) within instances of AttributeFilter.
*/
type atf struct {
AttributeType // single LDAP AttributeType
SearchFilter // single LDAP Search Filter
}
/*
AttributeOperation defines either an Add Operation or a Delete Operation.
Constants of this type are used in [AttributeFilterOperation] instances.
*/
type AttributeOperation uint8
/*
[AttributeOperation] constants are used to initialize and return [AttributeFilter] instances based on one (1) of the possible two (2) constants defined below.
*/
const (
noAOp AttributeOperation = iota
AddOp // add=
DelOp // delete=
)
/*
AF initializes, optionally sets and returns a new instance of [AttributeFilter], which is a critical component of the [TargetAttrFilters] Target Rule.
Input values must be either a [SearchFilter] or an [AttributeType].
*/
func AF(x ...any) AttributeFilter {
return AttributeFilter{newAttrFilter(x...)}
}
/*
newAttrFilter is a private function called by AF during an attempt to create a new instance of [AttributeFilter].
*/
func newAttrFilter(x ...any) *atf {
a := new(atf)
a.set(x...)
return a
}
/*
Set assigns the provided address component to the receiver and returns the receiver instance in fluent-form.
Multiple values can be provided in variadic form, or piecemeal.
*/
func (r *AttributeFilter) Set(x ...any) *AttributeFilter {
if r.IsZero() {
r.atf = new(atf)
}
r.atf.set(x...)
return r
}
/*
AttributeType returns the underlying instance of [AttributeType], or a bogus [AttributeType] if unset.
*/
func (r AttributeFilter) AttributeType() AttributeType {
if r.IsZero() {
return badAttributeType
}
return r.atf.AttributeType
}
/*
SearchFilter returns the underlying instance of [SearchFilter], or a bogus [SearchFilter] if unset.
*/
func (r AttributeFilter) SearchFilter() SearchFilter {
if r.IsZero() {
return badSearchFilter
}
return r.atf.SearchFilter
}
/*
Compare returns a Boolean value indicative of a SHA-1 comparison between the receiver (r) and input value x.
*/
func (r AttributeFilter) Compare(x any) bool {
return compareHashInstance(r, x)
}
/*
set is a private method called by AttributeFilter.Set.
*/
func (r *atf) set(x ...any) {
for i := 0; i < len(x); i++ {
switch tv := x[i].(type) {
case string:
if isIdentifier(tv) {
r.AttributeType = AT(tv)
} else {
r.SearchFilter = Filter(tv)
}
case AttributeType:
r.AttributeType = tv
case SearchFilter:
r.SearchFilter = tv
}
}
}
/*
String is a stringer method that returns the string representation of the receiver instance.
*/
func (r AttributeFilter) String() string {
if err := r.Valid(); err != nil {
return ``
}
return sprintf("%s:%s", r.atf.AttributeType, r.atf.SearchFilter)
}
/*
Keyword returns the [TargetKeyword] associated with the receiver instance enveloped as a [Keyword]. In the context of this type instance, the [TargetKeyword] returned is always [TargetFilter].
*/
func (r AttributeFilter) Keyword() Keyword {
return TargetAttrFilters
}
/*
Valid returns an error indicative of whether the receiver is in an aberrant state.
*/
func (r AttributeFilter) Valid() (err error) {
if r.IsZero() {
err = nilInstanceErr(r)
return
}
if r.atf.SearchFilter.IsZero() {
err = illegalSyntaxPerTypeErr(r, TargetAttrFilters)
} else if r.atf.AttributeType.IsZero() {
err = illegalSyntaxPerTypeErr(r, TargetAttrFilters)
}
return
}
/*
IsZero returns a Boolean value indicative of whether the receiver is nil, or unset.
*/
func (r AttributeFilter) IsZero() bool {
if r.atf == nil {
return true
}
return r.atf.SearchFilter.IsZero() &&
r.atf.AttributeType.IsZero()
}
/*
String is a stringer method that returns the string representation of the receiver instance.
*/
func (r AttributeOperation) String() string {
if r == DelOp {
return `delete`
}
return `add`
}
/*
AFOs returns a freshly initialized instance of [AttributeFilterOperations], configured to store one (1) or more [AttributeFilterOperation] instances for the purpose of crafting [TargetRule] instances which bear the [TargetAttrFilters] [TargetKeyword] context.
Optionally, the caller may choose to submit one (1) or more (valid) instances of the [AttributeFilterOperation] type (or its string equivalent) during initialization. This is merely a more convenient alternative to separate initialization and push procedures.
Instances of this design are not generally needed elsewhere.
Values are automatically joined using [stackage.List] with [AttributeFilterOperations.SetDelimiter] for comma delimitation by default, though semicolon delimitation is also permitted.
*/
func AFOs(x ...any) (f AttributeFilterOperations) {
// create a native stackage.Stack
// and configure before typecast.
_f := stackList().
SetDelimiter(rune(44)).
SetID(targetRuleID).
NoPadding(!StackPadding).
SetCategory(TargetAttrFilters.String())
// cast _f as a proper AttributeFilterOperations
// instance (f). We do it this way to gain
// access to the method for the *specific
// instance* being created (f), thus allowing
// a custom presentation policy to be set.
f = AttributeFilterOperations(_f)
// Set custom Presentation/Push policies
// per go-stackage signatures.
//_f.SetPresentationPolicy(f.presentationPolicy).
_f.SetPushPolicy(f.pushPolicy)
// Assuming one (1) or more items were
// submitted during the call, (try to)
// push them into our initialized stack.
// Note that any failed push(es) will
// have no impact on the validity of
// the return instance.
_f.Push(x...)
return
}
/*
F returns the appropriate instance creator function for crafting individual [AttributeFilterOperation] instances for submission to the receiver. This is merely a convenient alternative to maintaining knowledge as to which function applies to the current receiver instance.
As there is only one possibility for instances of this design, the [AFO] function is returned.
*/
func (r AttributeFilterOperations) F() func(...any) AttributeFilterOperation {
return AFO
}
/*
Keyword returns the [TargetKeyword] associated with the receiver instance enveloped as a [Keyword]. In the context of this type instance, the [TargetKeyword] returned is always [TargetAttrFilters].
*/
func (r AttributeFilterOperations) Keyword() Keyword {
return TargetAttrFilters
}
/*
Compare returns a Boolean value indicative of a SHA-1 comparison between the receiver (r) and input value x.
*/
func (r AttributeFilterOperations) Compare(x any) bool {
return compareHashInstance(r, x)
}
/*
Contains returns a Boolean value indicative of whether the type and its value were located within the receiver.
Valid input types are [AttributeFilterOperation] or a valid string equivalent.
Case is significant in the matching process.
*/
func (r AttributeFilterOperations) Contains(x any) bool {
return r.contains(x)
}
/*
contains is a private method called by the [AttributeFilterOperations.Contains] method, et al.
Case is significant in the matching process.
*/
func (r AttributeFilterOperations) contains(x any) bool {
if r.Len() == 0 {
return false
}
var candidate string
switch tv := x.(type) {
case string:
candidate = tv
case AttributeFilterOperation:
candidate = tv.String()
default:
return false
}
for i := 0; i < r.Len(); i++ {
// case is significant here.
if r.Index(i).String() == candidate {
return true
}
}
return false
}
/*
SetDelimiter controls the delimitation scheme employed by the receiver.
Some vendors use semicolon (ASCII #59) for delimitation for expressions that include values of this kind. This alternative scheme can be set using the [AttributeFilterOperationsSemiDelim] integer constant (1).
Other vendors use a comma (ASCII #44) for delimitation of the same form of expression. This delimitation scheme represents the default (most common) behavior, but can be set using the [AttributeFilterOperationsCommaDelim] integer constant (0), or when run in niladic fashion.
*/
func (r AttributeFilterOperations) SetDelimiter(i ...int) AttributeFilterOperations {
_r := r.cast()
var (
// default delimiter is a comma
def string = string(rune(44)) // `,`
// alternative delimiter is a semicolon
alt string = string(rune(59)) // `;`
)
if len(i) == 0 {
// caller requests the default
// delimitation scheme (niladic
// exec).
_r.SetDelimiter(def)
return r
}
// perform integer switch, looking
// for a particular constant value
switch i[0] {
case AttributeFilterOperationsSemiDelim:
// Caller requests alternative
// delimitation scheme.
_r.SetDelimiter(alt)
default:
// caller requests the default
// delimitation scheme.
_r.SetDelimiter(def)
}
return r
}
/*
Push wraps the [stackage.Stack.Push] method. This method shall attempt to add the provided input values (x) -- which may contain one (1) or more instances of [AttributeFilterOperation] or its string equivalent -- to the receiver instance.
*/
func (r AttributeFilterOperations) Push(x ...any) AttributeFilterOperations {
if len(x) == 0 {
return r
}
_r := r.cast()
for i := 0; i < len(x); i++ {
switch tv := x[i].(type) {
case string:
if afo, err := parseAttributeFilterOperation(tv); err == nil {
if afo.Len() > 0 {
_r.Push(afo)
}
}
default:
_r.Push(tv)
}
}
return r
}
/*
Pop wraps the [stackage.Stack.Pop] method.
*/
func (r AttributeFilterOperations) Pop() (afo AttributeFilterOperation) {
slice, _ := r.cast().Pop()
if assert, ok := slice.(AttributeFilterOperation); ok {
afo = assert
}
return
}
/*
Len wraps the [stackage.Stack.Len] method.
*/
func (r AttributeFilterOperations) Len() int {
return r.cast().Len()
}
/*
Index wraps the [stackage.Stack.Index] method. Note that the Boolean OK value returned by [stackage] by default will be shadowed and not obtainable by the caller.
*/
func (r AttributeFilterOperations) Index(idx int) (afo AttributeFilterOperation) {
slice, _ := r.cast().Index(idx)
if assert, ok := slice.(AttributeFilterOperation); ok {
afo = assert
}
return
}
/*
IsZero wraps the [stackage.Stack.IsZero] method.
*/
func (r AttributeFilterOperations) IsZero() bool {
return r.cast().IsZero()
}
/*
Valid wraps the [stackage.Stack.Valid] method.
*/
func (r AttributeFilterOperations) Valid() error {
return r.cast().Valid()
}
/*
Kind returns the categorical label assigned to the receiver.
*/
func (r AttributeFilterOperations) Kind() string {
return TargetAttrFilters.String()
}
/*
String is a stringer method that returns the string representation of the receiver instance.
*/
func (r AttributeFilterOperations) String() string {
return r.cast().String()
}
/*
Eq initializes and returns a new [TargetRule] instance configured to express the evaluation of the receiver value as Equal-To a [TargetAttrFilters] [TargetKeyword] context.
*/
func (r AttributeFilterOperations) Eq() TargetRule {
if r.IsZero() {
return badTargetRule
}
return TR(TargetAttrFilters, Eq, r)
}
/*
Ne performs no useful task, as negated equality comparison does not apply to [TargetRule] instances that bear the [TargetAttrFilters] [TargetKeyword] context.
This method exists solely to convey this message and conform to Go's interface qualifying signature. When executed, this method will return a bogus [TargetRule].
Negated equality [TargetRule] instances should be used with caution.
*/
func (r AttributeFilterOperations) Ne() TargetRule { return badTargetRule }
/*
TRM returns an instance of [TargetRuleMethods].
Each of the return instance's key values represent a single instance of the [ComparisonOperator] type that is allowed for use in the creation of [TargetRule] instances which bear the receiver instance as an expression value. The value for each key is the actual [TargetRuleMethod] instance for OPTIONAL use in the creation of a [TargetRule] instance.
This is merely a convenient alternative to maintaining knowledge of which [ComparisonOperator] instances apply to which types. Instances of this type are also used to streamline package unit tests.
Please note that if the receiver is in an aberrant state, or if it has not yet been initialized, the execution of ANY of the return instance's value methods will return bogus [TargetRule] instances. While this is useful in unit testing, the end user must only execute this method IF and WHEN the receiver has been properly populated and prepared for such activity.
*/
func (r AttributeFilterOperations) TRM() TargetRuleMethods {
return newTargetRuleMethods(targetRuleFuncMap{
Eq: r.Eq,
Ne: r.Ne,
})
}
/*
pushPolicy conforms to [stackage.PushPolicy] closure signature. This method is used to govern attempts to push instances into a stack, allowing or rejecting attempts based upon instance type and other conditions. An error is returned to the caller revealing the outcome of the attempt.
*/
func (r AttributeFilterOperations) pushPolicy(x ...any) (err error) {
if len(x) == 0 {
return
} else if x[0] == nil {
err = nilInstanceErr(x[0])
return
}
if r.contains(x[0]) {
err = pushErrorNotUnique(r, x[0], TargetAttrFilters)
return
}
switch tv := x[0].(type) {
case AttributeFilterOperation:
// because codecov :/
xerr := tv.Valid()
err = pushErrorNilOrZero(r, tv, TargetAttrFilters, xerr)
if xerr == nil && tv.Len() > 0 {
err = nil
}
default:
err = pushErrorBadType(r, tv, TargetAttrFilters)
}
return
}
/*
pushPolicy conforms to [stackage.PushPolicy] closure signature. This method is used to govern attempts to push instances into a stack, allowing or rejecting attempts based upon instance type and other conditions. An error is returned to the caller revealing the outcome of the attempt.
*/
func (r AttributeFilterOperation) pushPolicy(x ...any) (err error) {
if len(x) == 0 {
return
} else if x[0] == nil {
err = nilInstanceErr(x[0])
return
}
if r.contains(x[0]) {
err = pushErrorNotUnique(r, x[0], TargetAttrFilters)
return
}
switch tv := x[0].(type) {
case string:
if len(tv) == 0 {
err = pushErrorNilOrZero(r, tv, TargetAttrFilters)
}
case AttributeFilter:
if err = tv.Valid(); err != nil {
err = pushErrorNilOrZero(r, tv, TargetAttrFilters, err)
}
default:
err = pushErrorBadType(r, tv, TargetAttrFilters)
}
return
}
/*
Compare returns a Boolean value indicative of a SHA-1 comparison between the receiver (r) and input value x.
*/
func (r AttributeFilterOperation) Compare(x any) bool {
return compareHashInstance(r, x)
}
/*
Push wraps the [stackage.Stack.Push] method.
*/
func (r AttributeFilterOperation) Push(x ...any) AttributeFilterOperation {
if len(x) == 0 {
return r
}
_r := r.cast()
for i := 0; i < len(x); i++ {
switch tv := x[i].(type) {
case string:
if af, err := parseAttributeFilter(tv); err == nil {
_r.Push(af)
}
// Don't push AO; it was pushed just
// because its a convenient way to
// set the operation on the stack
// receiver instance. But we can't
// put it in our pushPolicy because
// we'd have to return a bogus error
// to prevent a push, which would
// almost definitely confuse people
// reading error logs ...
case AttributeOperation:
r.setCategory(tv.makeLabel())
default:
_r.Push(tv)
}
}
return r
}
/*
Pop wraps the [stackage.Stack.Pop] method.
*/
func (r AttributeFilterOperation) Pop() (af AttributeFilter) {
slice, _ := r.cast().Pop()
if assert, ok := slice.(AttributeFilter); ok {
af = assert
}
return
}
/*
F returns the appropriate instance creator function for crafting individual [AttributeFilter] instances for submission to the receiver. This is merely a convenient alternative to maintaining knowledge as to which function applies to the current receiver instance.
As there is only one possibility for instances of this design, the [AF] function is always returned.
*/
func (r AttributeFilterOperation) F() func(...any) AttributeFilter {
return AF
}
/*
Keyword returns the [TargetKeyword] associated with the receiver instance enveloped as a [Keyword]. In the context of this type instance, the [TargetAttrFilters] [TargetKeyword] context is always returned.
*/
func (r AttributeFilterOperation) Keyword() Keyword {
return TargetAttrFilters
}
/*
Len wraps the [stackage.Stack.Len] method.
*/
func (r AttributeFilterOperation) Len() int {
return r.cast().Len()
}
/*
Index wraps the [stackage.Stack.Index] method. Note that the Boolean OK value returned by [stackage] by default will be shadowed and not obtainable by the caller.
*/
func (r AttributeFilterOperation) Index(idx int) (af AttributeFilter) {
slice, _ := r.cast().Index(idx)
if assert, ok := slice.(AttributeFilter); ok {
af = assert
}
return
}
/*
Contains returns a Boolean value indicative of whether the type and its value were located within the receiver.
Valid input types are [AttributeFilter] or a valid string equivalent.
Case is significant in the matching process.
*/
func (r AttributeFilterOperation) Contains(x any) bool {
return r.contains(x)
}
/*
contains is a private method called by the AttributeFilterOperation Contains method, et al.
Case is significant in the matching process.
*/
func (r AttributeFilterOperation) contains(x any) bool {
if r.Len() == 0 {
return false
}
var candidate string
switch tv := x.(type) {
case string:
candidate = tv
case AttributeFilter:
candidate = tv.String()
default:
return false
}
for i := 0; i < r.Len(); i++ {
// case is significant here.
if r.Index(i).String() == candidate {
return true
}
}
return false
}
/*
IsZero wraps the [stackage.Stack.IsZero] method.
*/
func (r AttributeFilterOperation) IsZero() bool {
return r.cast().IsZero()
}
/*
Valid wraps the [stackage.Stack.Valid] method.
*/
func (r AttributeFilterOperation) Valid() (err error) {
return r.cast().Valid()
}
/*
Kind returns the categorical label assigned to the receiver.
*/
func (r AttributeFilterOperation) Kind() string {
// we'll just cheat and send the string form
// of the keyword, as we leverage the underlying
// category method for the operation. See the
// getCategory method below...
return TargetAttrFilters.String()
}
func (r AttributeFilterOperation) getCategory() string {
return r.cast().Category()
}
/*
String is a stringer method that returns the string representation of the receiver instance.
*/
func (r AttributeFilterOperation) String() (s string) {
if r.IsZero() {
return
}
_r := r.cast()
_r.SetPresentationPolicy(nil)
s = sprintf("%s=%s", r.Operation(), _r.String())
_r.SetPresentationPolicy(r.presentationPolicy)
return
}
/*
presentationPolicy -- when set via the [stackage.Stack.SetPresentationPolicy] method -- shall usurp the standard String method behavior exhibited by the receiver in favor of the provided closure's own Stringer implementation. It can be necessary to do this at times if [stackage]'s basic stringer method generates output text in a way other than what is desired.
See [stackage.PresentationPolicy] documentation for details.
*/
func (r AttributeFilterOperation) presentationPolicy(_ ...any) string {
return r.String()
}
/*
Eq initializes and returns a new [TargetRule] instance configured to express the evaluation of the receiver value as Equal-To a [TargetAttrFilters] [TargetKeyword] context.
*/
func (r AttributeFilterOperation) Eq() TargetRule {
if r.IsZero() {
return badTargetRule
}
return TR(TargetAttrFilters, Eq, r.String()) // TODO: revisit this Stringer nonsense
}
/*
Ne performs no useful task, as negated equality comparison does not apply to [TargetRule] instances that bear the [TargetAttrFilters] [TargetKeyword] context.
This method exists solely to convey this message and conform to Go's interface qualifying signature. When executed, this method will return a bogus [TargetRule].
Negated equality [TargetRule] instances should be used with caution.
*/
func (r AttributeFilterOperation) Ne() TargetRule { return badTargetRule }
/*
TRM returns an instance of [TargetRuleMethods].
Each of the return instance's key values represent a single instance of the [ComparisonOperator] type that is allowed for use in the creation of [TargetRule] instances which bear the receiver instance as an expression value. The value for each key is the actual [TargetRuleMethod] instance for OPTIONAL use in the creation of a [TargetRule] instance.
This is merely a convenient alternative to maintaining knowledge of which [ComparisonOperator] instances apply to which types. Instances of this type are also used to streamline package unit tests.
Please note that if the receiver is in an aberrant state, or if it has not yet been initialized, the execution of ANY of the return instance's value methods will return bogus [TargetRule] instances. While this is useful in unit testing, the end user must only execute this method IF and WHEN the receiver has been properly populated and prepared for such activity.
*/
func (r AttributeFilterOperation) TRM() TargetRuleMethods {
return newTargetRuleMethods(targetRuleFuncMap{
Eq: r.Eq,
Ne: r.Ne,
})
}
/*
AFO returns a freshly initialized instance of [AttributeFilterOperation], configured to store one (1) or more [AttributeFilter] instances for the purpose of crafting [TargetRule] instances which bear the [TargetAttrFilters] [TargetKeyword] context. Instances of this design are not generally needed outside of that context.
Optionally, the caller may choose to submit one (1) or more (valid) instances of the [AttributeFilter] type (or its string equivalent) during initialization. This is merely a more convenient alternative to separate init and push procedures.
Multiple values are automatically ANDed using [stackage.And] using the symbolic AND operator (&&).
See also the [AttributeFilterOperations] type, and its [AFOs] function, for the multi-valued incarnation of this type.
*/
func AFO(x ...any) (f AttributeFilterOperation) {
// create a native stackage.Stack
// and configure before typecast.
_f := stackAnd().
Symbol(`&&`).
SetID(targetRuleID).
NoPadding(!StackPadding).
SetCategory(TargetAttrFilters.String())
f = AttributeFilterOperation(_f)
_f.SetPresentationPolicy(f.presentationPolicy).
SetPushPolicy(f.pushPolicy)
f.Push(x...)
return
}
/*
AFO returns an instance of [AttributeFilterOperation] based upon the input [AttributeFilter] instances.
The instance of [AttributeFilterOperation] contains an ANDed [TargetRule] instance using symbols (`&&`).
*/
func (r AttributeOperation) AFO(x ...any) (afo AttributeFilterOperation) {
afo = AFO()
afo.setCategory(r.makeLabel())
afo.Push(x...)
return
}
/*
makeLabel is a temporary hack to brand a particular AttributeFilterOperation stack as either Add or Delete in its operational nature.
*/
func (r AttributeOperation) makeLabel() string {
return sprintf("%s_%s", TargetAttrFilters, r) // TODO: Find an alternative. I really don't like this.
}
/*
setCategory assigns the categorical string label (cat) to the receiver.
*/
func (r AttributeFilterOperation) setCategory(cat string) {
r.cast().SetCategory(cat)
}
/*
Operation returns [AddOp] or [DelOp] as extracted from the receiver's categorical label. If invalid, an invalid [AttributeOperation] value is returned.
*/
func (r AttributeFilterOperation) Operation() AttributeOperation {
switch x := trimPfx(r.getCategory(), TargetAttrFilters.String()+`_`); lc(x) {
case `add`:
return AddOp
case `delete`:
return DelOp
}
return noAOp
}
/*
hasAttributeFilterOperationPrefix returns a Boolean value indicative of whether the input string value (raw) begins with a known [AttributeOperation] prefix.
*/
func hasAttributeFilterOperationPrefix(raw string) bool {
switch {
case hasPfx(raw, `add=`):
return true
case hasPfx(raw, `delete=`):
return true
}
return false
}
func afosDelim(delim int) (char rune) {
char = rune(44) // ASCII #44 [comma, default]
// If delim is anything except one (1)
// use the default, else use semicolon.
if delim == 1 {
char = rune(59) // ASCII #59 [semicolon]
}
return
}
/*
parseAttributeFilterOperations processes the raw input value into an instance of [AttributeFilterOperations], which is returned alongside an error instance.
*/
func parseAttributeFilterOperations(raw string, delim int) (afos AttributeFilterOperations, err error) {
char := afosDelim(delim)
// Scan the raw input value and count the number of