This repository has been archived by the owner on Sep 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
entitlement_gen.go
520 lines (473 loc) · 12.5 KB
/
entitlement_gen.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
// Generated by "sketch" utility. DO NOT EDIT
package resource
import (
"bytes"
"encoding/json"
"fmt"
"sort"
"sync"
"github.com/lestrrat-go/blackmagic"
)
func init() {
Register("Entitlement", "", Entitlement{})
RegisterBuilder("Entitlement", "", EntitlementBuilder{})
}
type Entitlement struct {
mu sync.RWMutex
display *string
primary *bool
typ *string
value *string
extra map[string]interface{}
}
// These constants are used when the JSON field name is used.
// Their use is not strictly required, but certain linters
// complain about repeated constants, and therefore internally
// this used throughout
const (
EntitlementDisplayKey = "display"
EntitlementPrimaryKey = "primary"
EntitlementTypeKey = "type"
EntitlementValueKey = "value"
)
// Get retrieves the value associated with a key
func (v *Entitlement) Get(key string, dst interface{}) error {
v.mu.RLock()
defer v.mu.RUnlock()
return v.getNoLock(key, dst, false)
}
// getNoLock is a utility method that is called from Get, MarshalJSON, etc, but
// it can be used from user-supplied code. Unlike Get, it avoids locking for
// each call, so the user needs to explicitly lock the object before using,
// but otherwise should be faster than sing Get directly
func (v *Entitlement) getNoLock(key string, dst interface{}, raw bool) error {
switch key {
case EntitlementDisplayKey:
if val := v.display; val != nil {
return blackmagic.AssignIfCompatible(dst, *val)
}
case EntitlementPrimaryKey:
if val := v.primary; val != nil {
return blackmagic.AssignIfCompatible(dst, *val)
}
case EntitlementTypeKey:
if val := v.typ; val != nil {
return blackmagic.AssignIfCompatible(dst, *val)
}
case EntitlementValueKey:
if val := v.value; val != nil {
return blackmagic.AssignIfCompatible(dst, *val)
}
default:
if v.extra != nil {
val, ok := v.extra[key]
if ok {
return blackmagic.AssignIfCompatible(dst, val)
}
}
}
return fmt.Errorf(`no such key %q`, key)
}
// Set sets the value of the specified field. The name must be a JSON
// field name, not the Go name
func (v *Entitlement) Set(key string, value interface{}) error {
v.mu.Lock()
defer v.mu.Unlock()
switch key {
case EntitlementDisplayKey:
converted, ok := value.(string)
if !ok {
return fmt.Errorf(`expected value of type string for field display, got %T`, value)
}
v.display = &converted
case EntitlementPrimaryKey:
converted, ok := value.(bool)
if !ok {
return fmt.Errorf(`expected value of type bool for field primary, got %T`, value)
}
v.primary = &converted
case EntitlementTypeKey:
converted, ok := value.(string)
if !ok {
return fmt.Errorf(`expected value of type string for field type, got %T`, value)
}
v.typ = &converted
case EntitlementValueKey:
converted, ok := value.(string)
if !ok {
return fmt.Errorf(`expected value of type string for field value, got %T`, value)
}
v.value = &converted
default:
if v.extra == nil {
v.extra = make(map[string]interface{})
}
v.extra[key] = value
}
return nil
}
// Has returns true if the field specified by the argument has been populated.
// The field name must be the JSON field name, not the Go-structure's field name.
func (v *Entitlement) Has(name string) bool {
switch name {
case EntitlementDisplayKey:
return v.display != nil
case EntitlementPrimaryKey:
return v.primary != nil
case EntitlementTypeKey:
return v.typ != nil
case EntitlementValueKey:
return v.value != nil
default:
if v.extra != nil {
if _, ok := v.extra[name]; ok {
return true
}
}
return false
}
}
// Keys returns a slice of string comprising of JSON field names whose values
// are present in the object.
func (v *Entitlement) Keys() []string {
keys := make([]string, 0, 4)
if v.display != nil {
keys = append(keys, EntitlementDisplayKey)
}
if v.primary != nil {
keys = append(keys, EntitlementPrimaryKey)
}
if v.typ != nil {
keys = append(keys, EntitlementTypeKey)
}
if v.value != nil {
keys = append(keys, EntitlementValueKey)
}
if len(v.extra) > 0 {
for k := range v.extra {
keys = append(keys, k)
}
}
sort.Strings(keys)
return keys
}
// HasDisplay returns true if the field `display` has been populated
func (v *Entitlement) HasDisplay() bool {
v.mu.RLock()
defer v.mu.RUnlock()
return v.display != nil
}
// HasPrimary returns true if the field `primary` has been populated
func (v *Entitlement) HasPrimary() bool {
v.mu.RLock()
defer v.mu.RUnlock()
return v.primary != nil
}
// HasType returns true if the field `type` has been populated
func (v *Entitlement) HasType() bool {
v.mu.RLock()
defer v.mu.RUnlock()
return v.typ != nil
}
// HasValue returns true if the field `value` has been populated
func (v *Entitlement) HasValue() bool {
v.mu.RLock()
defer v.mu.RUnlock()
return v.value != nil
}
func (v *Entitlement) Display() string {
v.mu.RLock()
defer v.mu.RUnlock()
if val := v.display; val != nil {
return *val
}
return ""
}
func (v *Entitlement) Primary() bool {
v.mu.RLock()
defer v.mu.RUnlock()
if val := v.primary; val != nil {
return *val
}
return false
}
func (v *Entitlement) Type() string {
v.mu.RLock()
defer v.mu.RUnlock()
if val := v.typ; val != nil {
return *val
}
return ""
}
func (v *Entitlement) Value() string {
v.mu.RLock()
defer v.mu.RUnlock()
if val := v.value; val != nil {
return *val
}
return ""
}
// Remove removes the value associated with a key
func (v *Entitlement) Remove(key string) error {
v.mu.Lock()
defer v.mu.Unlock()
switch key {
case EntitlementDisplayKey:
v.display = nil
case EntitlementPrimaryKey:
v.primary = nil
case EntitlementTypeKey:
v.typ = nil
case EntitlementValueKey:
v.value = nil
default:
delete(v.extra, key)
}
return nil
}
func (v *Entitlement) Clone(dst interface{}) error {
v.mu.RLock()
defer v.mu.RUnlock()
var extra map[string]interface{}
if len(v.extra) > 0 {
extra = make(map[string]interface{})
for key, val := range v.extra {
extra[key] = val
}
}
return blackmagic.AssignIfCompatible(dst, &Entitlement{
display: v.display,
primary: v.primary,
typ: v.typ,
value: v.value,
extra: extra,
})
}
// MarshalJSON serializes Entitlement into JSON.
// All pre-declared fields are included as long as a value is
// assigned to them, as well as all extra fields. All of these
// fields are sorted in alphabetical order.
func (v *Entitlement) MarshalJSON() ([]byte, error) {
v.mu.RLock()
defer v.mu.RUnlock()
var buf bytes.Buffer
enc := json.NewEncoder(&buf)
buf.WriteByte('{')
for i, k := range v.Keys() {
var val interface{}
if err := v.getNoLock(k, &val, true); err != nil {
return nil, fmt.Errorf(`failed to retrieve value for field %q: %w`, k, err)
}
if i > 0 {
buf.WriteByte(',')
}
if err := enc.Encode(k); err != nil {
return nil, fmt.Errorf(`failed to encode map key name: %w`, err)
}
buf.WriteByte(':')
if err := enc.Encode(val); err != nil {
return nil, fmt.Errorf(`failed to encode map value for %q: %w`, k, err)
}
}
buf.WriteByte('}')
return buf.Bytes(), nil
}
// UnmarshalJSON deserializes a piece of JSON data into Entitlement.
//
// Pre-defined fields must be deserializable via "encoding/json" to their
// respective Go types, otherwise an error is returned.
//
// Extra fields are stored in a special "extra" storage, which can only
// be accessed via `Get()` and `Set()` methods.
func (v *Entitlement) UnmarshalJSON(data []byte) error {
v.mu.Lock()
defer v.mu.Unlock()
v.display = nil
v.primary = nil
v.typ = nil
v.value = nil
dec := json.NewDecoder(bytes.NewReader(data))
var extra map[string]interface{}
LOOP:
for {
tok, err := dec.Token()
if err != nil {
return fmt.Errorf(`error reading JSON token: %w`, err)
}
switch tok := tok.(type) {
case json.Delim:
if tok == '}' { // end of object
break LOOP
}
// we should only get into this clause at the very beginning, and just once
if tok != '{' {
return fmt.Errorf(`expected '{', but got '%c'`, tok)
}
case string:
switch tok {
case EntitlementDisplayKey:
var val string
if err := dec.Decode(&val); err != nil {
return fmt.Errorf(`failed to decode value for %q: %w`, EntitlementDisplayKey, err)
}
v.display = &val
case EntitlementPrimaryKey:
var val bool
if err := dec.Decode(&val); err != nil {
return fmt.Errorf(`failed to decode value for %q: %w`, EntitlementPrimaryKey, err)
}
v.primary = &val
case EntitlementTypeKey:
var val string
if err := dec.Decode(&val); err != nil {
return fmt.Errorf(`failed to decode value for %q: %w`, EntitlementTypeKey, err)
}
v.typ = &val
case EntitlementValueKey:
var val string
if err := dec.Decode(&val); err != nil {
return fmt.Errorf(`failed to decode value for %q: %w`, EntitlementValueKey, err)
}
v.value = &val
default:
var val interface{}
if err := v.decodeExtraField(tok, dec, &val); err != nil {
return fmt.Errorf(`failed to decode value for %q: %w`, tok, err)
}
if extra == nil {
extra = make(map[string]interface{})
}
extra[tok] = val
}
}
}
if extra != nil {
v.extra = extra
}
return nil
}
type EntitlementBuilder struct {
mu sync.Mutex
err error
once sync.Once
object *Entitlement
}
// NewEntitlementBuilder creates a new EntitlementBuilder instance.
// EntitlementBuilder is safe to be used uninitialized as well.
func NewEntitlementBuilder() *EntitlementBuilder {
return &EntitlementBuilder{}
}
func (b *EntitlementBuilder) initialize() {
b.err = nil
b.object = &Entitlement{}
}
func (b *EntitlementBuilder) Display(in string) *EntitlementBuilder {
return b.SetField(EntitlementDisplayKey, in)
}
func (b *EntitlementBuilder) Primary(in bool) *EntitlementBuilder {
return b.SetField(EntitlementPrimaryKey, in)
}
func (b *EntitlementBuilder) Type(in string) *EntitlementBuilder {
return b.SetField(EntitlementTypeKey, in)
}
func (b *EntitlementBuilder) Value(in string) *EntitlementBuilder {
return b.SetField(EntitlementValueKey, in)
}
// SetField sets the value of any field. The name should be the JSON field name.
// Type check will only be performed for pre-defined types
func (b *EntitlementBuilder) SetField(name string, value interface{}) *EntitlementBuilder {
b.mu.Lock()
defer b.mu.Unlock()
b.once.Do(b.initialize)
if b.err != nil {
return b
}
if err := b.object.Set(name, value); err != nil {
b.err = err
}
return b
}
func (b *EntitlementBuilder) Build() (*Entitlement, error) {
b.mu.Lock()
defer b.mu.Unlock()
b.once.Do(b.initialize)
if b.err != nil {
return nil, b.err
}
obj := b.object
b.once = sync.Once{}
b.once.Do(b.initialize)
return obj, nil
}
func (b *EntitlementBuilder) MustBuild() *Entitlement {
object, err := b.Build()
if err != nil {
panic(err)
}
return object
}
func (b *EntitlementBuilder) From(in *Entitlement) *EntitlementBuilder {
b.mu.Lock()
defer b.mu.Unlock()
b.once.Do(b.initialize)
if b.err != nil {
return b
}
var cloned Entitlement
if err := in.Clone(&cloned); err != nil {
b.err = err
return b
}
b.object = &cloned
return b
}
// AsMap returns the resource as a Go map
func (v *Entitlement) AsMap(m map[string]interface{}) error {
v.mu.RLock()
defer v.mu.RUnlock()
for _, key := range v.Keys() {
var val interface{}
if err := v.getNoLock(key, &val, false); err != nil {
return fmt.Errorf(`failed to retrieve value for key %q: %w`, key, err)
}
m[key] = val
}
return nil
}
// GetExtension takes into account extension uri, and fetches
// the specified attribute from the extension object
func (v *Entitlement) GetExtension(name, uri string, dst interface{}) error {
if uri == "" {
return v.Get(name, dst)
}
var ext interface{}
if err := v.Get(uri, &ext); err != nil {
return fmt.Errorf(`failed to fetch extension %q: %w`, uri, err)
}
getter, ok := ext.(interface {
Get(string, interface{}) error
})
if !ok {
return fmt.Errorf(`extension does not implement Get(string, interface{}) error`)
}
return getter.Get(name, dst)
}
func (*Entitlement) decodeExtraField(name string, dec *json.Decoder, dst interface{}) error {
// we can get an instance of the resource object
if rx, ok := registry.LookupByURI(name); ok {
if err := dec.Decode(&rx); err != nil {
return fmt.Errorf(`failed to decode value for key %q: %w`, name, err)
}
if err := blackmagic.AssignIfCompatible(dst, rx); err != nil {
return err
}
} else {
if err := dec.Decode(dst); err != nil {
return fmt.Errorf(`failed to decode value for key %q: %w`, name, err)
}
}
return nil
}
func (b *Builder) Entitlement() *EntitlementBuilder {
return &EntitlementBuilder{}
}