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
/
pb.go
378 lines (314 loc) · 8.83 KB
/
pb.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
package aci
/*
pb.go contains PermissionBindRules types and methods. PermissionBindRules combine Permissions with BindRules, and are a key component in the formation of a complete Instruction.
*/
/*
invalid value constants used as stringer method returns when something goes wrong :/
*/
const (
// badPB is supplied during PermissionBindRule string representation
// attempts that fail
badPB = `<invalid_pbrule>`
)
var (
badPermissionBindRule PermissionBindRule // for failed calls that return a PermissionBindRule only
badPermissionBindRules PermissionBindRules // for failed calls that return a PermissionBindRules only
)
/*
PermissionBindRule contains one (1) [Permission] instance and one (1) [BindRules] instance. Instances of this type are used within an [Instruction] instance.
Users may create instances of this type using the [PBR] package level function.
*/
type PermissionBindRule struct {
*permissionBindRule
}
type permissionBindRule struct {
P Permission
B BindContext // BindRule -or- BindRules are allowed
}
func newPBR(P Permission, B BindContext) *permissionBindRule {
return &permissionBindRule{
P: P,
B: B,
}
}
/*
PBR returns an instance of [PermissionBindRule], bearing the [Permission] P and the [BindRule] B. The values P and B shall undergo validity checks per the conditions of the [PermissionBindRule] Valid method automatically. A bogus [PermissionBindRule] is returned if such checks fail.
Instances of this kind are intended for submission (via Push) into instances of [PermissionBindRules].
Generally, an [Instruction] only has a single [PermissionBindRule], though multiple instances of this type are allowed per the syntax specification honored by this package.
*/
func PBR(P Permission, B BindContext) (r PermissionBindRule) {
_r := PermissionBindRule{
newPBR(P, B),
}
if err := _r.Valid(); err == nil {
r = _r
}
return
}
/*
Valid returns an error instance should any of the following conditions evaluate as true:
- Valid returns an error for P
- Valid returns an error for B
- Len returns zero (0) for B
*/
func (r PermissionBindRule) Valid() (err error) {
return r.valid()
}
/*
IsZero returns a Boolean value indicative of whether the receiver instance is nil, or unset.
*/
func (r PermissionBindRule) IsZero() bool {
if r.permissionBindRule == nil {
return true
}
return r.permissionBindRule.P.IsZero() &&
r.permissionBindRule.B == nil
}
/*
Set assigns one (1) or more values (x) to the receiver. Valid types for input are [Permission], [BindContext] or their string equivalents.
*/
func (r *PermissionBindRule) Set(x ...any) PermissionBindRule {
if r.IsZero() {
r.permissionBindRule = new(permissionBindRule)
}
r.permissionBindRule.set(x...)
return *r
}
/*
set is a private method called by PermissionBindRule.Set.
*/
func (r *permissionBindRule) set(x ...any) {
// Iterate each of the user-specified
// input values ...
for i := 0; i < len(x); i++ {
switch tv := x[i].(type) {
case string:
_r, err := parsePermissionBindRule(tv)
if err != nil {
return
}
*r = (*_r.permissionBindRule)
case Permission:
if err := tv.Valid(); err != nil {
return
}
r.P = tv
case BindContext:
if err := tv.Valid(); err != nil {
return
}
r.B = tv
}
}
}
/*
Kind returns the string literal `pbr`.
*/
func (r PermissionBindRule) Kind() string {
return pbrRuleID
}
/*
valid is a private method invoked by PermissionBindRule.Valid.
*/
func (r PermissionBindRule) valid() (err error) {
if r.IsZero() {
return nilInstanceErr(r)
}
if err = r.P.Valid(); err != nil {
return
}
if r.B == nil {
return nilInstanceErr(r.B)
}
return
}
/*
String is a stringer method that returns the string representation of the receiver.
*/
func (r PermissionBindRule) String() string {
return r.string()
}
/*
Compare returns a Boolean value indicative of a SHA-1 comparison between the receiver (r) and input value x.
*/
func (r PermissionBindRule) Compare(x any) bool {
return compareHashInstance(r, x)
}
/*
string is a private method called by PermissionBindRule.String.
*/
func (r PermissionBindRule) string() (s string) {
s = badPB
if err := r.valid(); err == nil {
s = sprintf("%s %s;",
r.permissionBindRule.P,
r.permissionBindRule.B)
}
return
}
/*
Valid wraps the [stackage.Stack.Valid] method.
*/
func (r PermissionBindRules) Valid() (err error) {
err = r.cast().Valid()
return
}
/*
IsZero wraps the [stackage.Stack.IsZero] method.
*/
func (r PermissionBindRules) IsZero() bool {
return r.cast().IsZero()
}
/*
Len wraps the [stackage.Stack.Len] method.
*/
func (r PermissionBindRules) Len() int {
return r.cast().Len()
}
/*
Category wraps the [stackage.Stack.ID] method.
*/
func (r PermissionBindRules) Kind() string {
return pbrsRuleID
}
/*
Index wraps the [stackage.Stack.Index] method and performs type assertion in order to return an instance of [PermissionBindRule].
*/
func (r PermissionBindRules) Index(idx int) (pbr PermissionBindRule) {
x, _ := r.cast().Index(idx)
if assert, asserted := x.(PermissionBindRule); asserted {
pbr = assert
}
return
}
/*
String is a stringer method that returns the string representation of the receiver instance.
This method wraps the [stackage.Stack.String] method.
*/
func (r PermissionBindRules) String() string {
return r.cast().String()
}
/*
Compare returns a Boolean value indicative of a SHA-1 comparison between the receiver (r) and input value x.
*/
func (r PermissionBindRules) Compare(x any) bool {
return compareHashInstance(r, x)
}
/*
Push wraps the [stackage.Stack.Push] method.
*/
func (r PermissionBindRules) Push(x ...any) PermissionBindRules {
_r := r.cast()
// iterate variadic input arguments
for i := 0; i < len(x); i++ {
switch tv := x[i].(type) {
case string:
pbr, err := parsePermissionBindRule(tv)
if err != nil {
return r
}
_r.Push(pbr)
default:
_r.Push(tv)
}
}
return r
}
/*
Pop wraps the [stackage.Stack.Pop] method. An instance of [PermissionBindRule], which may or may not be nil, is returned following a call of this method.
Within the context of the receiver type, a [PermissionBindRule], if non-nil, can only represent a [PermissionBindRule] instance.
*/
func (r PermissionBindRules) Pop() (pbr PermissionBindRule) {
x, _ := r.cast().Pop()
if assert, ok := x.(PermissionBindRule); ok {
pbr = assert
}
return
}
/*
permissionBindRulesPushPolicy conforms to the PushPolicy interface signature defined within the [stackage] package. This private function is called during Push attempts to a PermissionBindRules instance.
*/
func (r PermissionBindRules) 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], nil)
return
}
switch tv := x[0].(type) {
case PermissionBindRule:
if err = tv.Valid(); err != nil {
err = pushErrorNilOrZero(PermissionBindRules{}, tv, nil, err)
}
default:
err = pushErrorBadType(PermissionBindRules{}, tv, nil)
}
return
}
/*
Contains returns a Boolean value indicative of whether value x, if a string or [PermissionBindRule] instance, already resides within the receiver instance.
Case is not significant in the matching process.
*/
func (r PermissionBindRules) Contains(x any) bool {
return r.contains(x)
}
/*
contains is a private method called by PermissionBindRules.Contains.
*/
func (r PermissionBindRules) contains(x any) bool {
if r.Len() == 0 {
return false
}
var candidate PermissionBindRule
switch tv := x.(type) {
case string:
pbr, _ := parsePermissionBindRule(tv)
candidate = pbr
case PermissionBindRule:
candidate = tv
}
for i := 0; i < r.Len(); i++ {
// case is not significant here.
if eq(r.Index(i).String(), candidate.String()) {
return true
}
}
return false
}
/*
PBRs returns a freshly initialized instance of [PermissionBindRules], configured to store one (1) or more instances of [PermissionBindRule].
Instances of this kind are used as a component in top-level [Instruction] assembly.
*/
func PBRs(x ...any) (pbr PermissionBindRules) {
// create a native stackage.Stack
// and configure before typecast.
_p := stackList().
NoNesting(true).
SetID(pbrsRuleID).
SetDelimiter(rune(32)).
SetCategory(pbrsRuleID).
NoPadding(true)
// cast _p as a proper PermissionBindRules
// instance (pbr). We do it this way to gain
// access to the method for the *specific
// instance* being created (pbr), thus allowing
// things like uniqueness checks, etc., to
// occur during push attempts, providing more
// helpful and non-generalized feedback.
pbr = PermissionBindRules(_p)
_p.SetPushPolicy(pbr.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.
_p.Push(x...)
return
}
const pbrRuleID = `pbr`
const pbrsRuleID = `pbrs`