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
/
names_gen.go
612 lines (561 loc) · 15.1 KB
/
names_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
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
// Generated by "sketch" utility. DO NOT EDIT
package resource
import (
"bytes"
"encoding/json"
"fmt"
"sort"
"sync"
"github.com/lestrrat-go/blackmagic"
)
func init() {
Register("Names", "", Names{})
RegisterBuilder("Names", "", NamesBuilder{})
}
type Names struct {
mu sync.RWMutex
familyName *string
formatted *string
givenName *string
honorificPrefix *string
honorificSuffix *string
middleName *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 (
NamesFamilyNameKey = "familyName"
NamesFormattedKey = "formatted"
NamesGivenNameKey = "givenName"
NamesHonorificPrefixKey = "honorificPrefix"
NamesHonorificSuffixKey = "honorificSuffix"
NamesMiddleNameKey = "middleName"
)
// Get retrieves the value associated with a key
func (v *Names) 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 *Names) getNoLock(key string, dst interface{}, raw bool) error {
switch key {
case NamesFamilyNameKey:
if val := v.familyName; val != nil {
return blackmagic.AssignIfCompatible(dst, *val)
}
case NamesFormattedKey:
if val := v.formatted; val != nil {
return blackmagic.AssignIfCompatible(dst, *val)
}
case NamesGivenNameKey:
if val := v.givenName; val != nil {
return blackmagic.AssignIfCompatible(dst, *val)
}
case NamesHonorificPrefixKey:
if val := v.honorificPrefix; val != nil {
return blackmagic.AssignIfCompatible(dst, *val)
}
case NamesHonorificSuffixKey:
if val := v.honorificSuffix; val != nil {
return blackmagic.AssignIfCompatible(dst, *val)
}
case NamesMiddleNameKey:
if val := v.middleName; 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 *Names) Set(key string, value interface{}) error {
v.mu.Lock()
defer v.mu.Unlock()
switch key {
case NamesFamilyNameKey:
converted, ok := value.(string)
if !ok {
return fmt.Errorf(`expected value of type string for field familyName, got %T`, value)
}
v.familyName = &converted
case NamesFormattedKey:
converted, ok := value.(string)
if !ok {
return fmt.Errorf(`expected value of type string for field formatted, got %T`, value)
}
v.formatted = &converted
case NamesGivenNameKey:
converted, ok := value.(string)
if !ok {
return fmt.Errorf(`expected value of type string for field givenName, got %T`, value)
}
v.givenName = &converted
case NamesHonorificPrefixKey:
converted, ok := value.(string)
if !ok {
return fmt.Errorf(`expected value of type string for field honorificPrefix, got %T`, value)
}
v.honorificPrefix = &converted
case NamesHonorificSuffixKey:
converted, ok := value.(string)
if !ok {
return fmt.Errorf(`expected value of type string for field honorificSuffix, got %T`, value)
}
v.honorificSuffix = &converted
case NamesMiddleNameKey:
converted, ok := value.(string)
if !ok {
return fmt.Errorf(`expected value of type string for field middleName, got %T`, value)
}
v.middleName = &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 *Names) Has(name string) bool {
switch name {
case NamesFamilyNameKey:
return v.familyName != nil
case NamesFormattedKey:
return v.formatted != nil
case NamesGivenNameKey:
return v.givenName != nil
case NamesHonorificPrefixKey:
return v.honorificPrefix != nil
case NamesHonorificSuffixKey:
return v.honorificSuffix != nil
case NamesMiddleNameKey:
return v.middleName != 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 *Names) Keys() []string {
keys := make([]string, 0, 6)
if v.familyName != nil {
keys = append(keys, NamesFamilyNameKey)
}
if v.formatted != nil {
keys = append(keys, NamesFormattedKey)
}
if v.givenName != nil {
keys = append(keys, NamesGivenNameKey)
}
if v.honorificPrefix != nil {
keys = append(keys, NamesHonorificPrefixKey)
}
if v.honorificSuffix != nil {
keys = append(keys, NamesHonorificSuffixKey)
}
if v.middleName != nil {
keys = append(keys, NamesMiddleNameKey)
}
if len(v.extra) > 0 {
for k := range v.extra {
keys = append(keys, k)
}
}
sort.Strings(keys)
return keys
}
// HasFamilyName returns true if the field `familyName` has been populated
func (v *Names) HasFamilyName() bool {
v.mu.RLock()
defer v.mu.RUnlock()
return v.familyName != nil
}
// HasFormatted returns true if the field `formatted` has been populated
func (v *Names) HasFormatted() bool {
v.mu.RLock()
defer v.mu.RUnlock()
return v.formatted != nil
}
// HasGivenName returns true if the field `givenName` has been populated
func (v *Names) HasGivenName() bool {
v.mu.RLock()
defer v.mu.RUnlock()
return v.givenName != nil
}
// HasHonorificPrefix returns true if the field `honorificPrefix` has been populated
func (v *Names) HasHonorificPrefix() bool {
v.mu.RLock()
defer v.mu.RUnlock()
return v.honorificPrefix != nil
}
// HasHonorificSuffix returns true if the field `honorificSuffix` has been populated
func (v *Names) HasHonorificSuffix() bool {
v.mu.RLock()
defer v.mu.RUnlock()
return v.honorificSuffix != nil
}
// HasMiddleName returns true if the field `middleName` has been populated
func (v *Names) HasMiddleName() bool {
v.mu.RLock()
defer v.mu.RUnlock()
return v.middleName != nil
}
func (v *Names) FamilyName() string {
v.mu.RLock()
defer v.mu.RUnlock()
if val := v.familyName; val != nil {
return *val
}
return ""
}
func (v *Names) Formatted() string {
v.mu.RLock()
defer v.mu.RUnlock()
if val := v.formatted; val != nil {
return *val
}
return ""
}
func (v *Names) GivenName() string {
v.mu.RLock()
defer v.mu.RUnlock()
if val := v.givenName; val != nil {
return *val
}
return ""
}
func (v *Names) HonorificPrefix() string {
v.mu.RLock()
defer v.mu.RUnlock()
if val := v.honorificPrefix; val != nil {
return *val
}
return ""
}
func (v *Names) HonorificSuffix() string {
v.mu.RLock()
defer v.mu.RUnlock()
if val := v.honorificSuffix; val != nil {
return *val
}
return ""
}
func (v *Names) MiddleName() string {
v.mu.RLock()
defer v.mu.RUnlock()
if val := v.middleName; val != nil {
return *val
}
return ""
}
// Remove removes the value associated with a key
func (v *Names) Remove(key string) error {
v.mu.Lock()
defer v.mu.Unlock()
switch key {
case NamesFamilyNameKey:
v.familyName = nil
case NamesFormattedKey:
v.formatted = nil
case NamesGivenNameKey:
v.givenName = nil
case NamesHonorificPrefixKey:
v.honorificPrefix = nil
case NamesHonorificSuffixKey:
v.honorificSuffix = nil
case NamesMiddleNameKey:
v.middleName = nil
default:
delete(v.extra, key)
}
return nil
}
func (v *Names) 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, &Names{
familyName: v.familyName,
formatted: v.formatted,
givenName: v.givenName,
honorificPrefix: v.honorificPrefix,
honorificSuffix: v.honorificSuffix,
middleName: v.middleName,
extra: extra,
})
}
// MarshalJSON serializes Names 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 *Names) 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 Names.
//
// 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 *Names) UnmarshalJSON(data []byte) error {
v.mu.Lock()
defer v.mu.Unlock()
v.familyName = nil
v.formatted = nil
v.givenName = nil
v.honorificPrefix = nil
v.honorificSuffix = nil
v.middleName = 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 NamesFamilyNameKey:
var val string
if err := dec.Decode(&val); err != nil {
return fmt.Errorf(`failed to decode value for %q: %w`, NamesFamilyNameKey, err)
}
v.familyName = &val
case NamesFormattedKey:
var val string
if err := dec.Decode(&val); err != nil {
return fmt.Errorf(`failed to decode value for %q: %w`, NamesFormattedKey, err)
}
v.formatted = &val
case NamesGivenNameKey:
var val string
if err := dec.Decode(&val); err != nil {
return fmt.Errorf(`failed to decode value for %q: %w`, NamesGivenNameKey, err)
}
v.givenName = &val
case NamesHonorificPrefixKey:
var val string
if err := dec.Decode(&val); err != nil {
return fmt.Errorf(`failed to decode value for %q: %w`, NamesHonorificPrefixKey, err)
}
v.honorificPrefix = &val
case NamesHonorificSuffixKey:
var val string
if err := dec.Decode(&val); err != nil {
return fmt.Errorf(`failed to decode value for %q: %w`, NamesHonorificSuffixKey, err)
}
v.honorificSuffix = &val
case NamesMiddleNameKey:
var val string
if err := dec.Decode(&val); err != nil {
return fmt.Errorf(`failed to decode value for %q: %w`, NamesMiddleNameKey, err)
}
v.middleName = &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 NamesBuilder struct {
mu sync.Mutex
err error
once sync.Once
object *Names
}
// NewNamesBuilder creates a new NamesBuilder instance.
// NamesBuilder is safe to be used uninitialized as well.
func NewNamesBuilder() *NamesBuilder {
return &NamesBuilder{}
}
func (b *NamesBuilder) initialize() {
b.err = nil
b.object = &Names{}
}
func (b *NamesBuilder) FamilyName(in string) *NamesBuilder {
return b.SetField(NamesFamilyNameKey, in)
}
func (b *NamesBuilder) Formatted(in string) *NamesBuilder {
return b.SetField(NamesFormattedKey, in)
}
func (b *NamesBuilder) GivenName(in string) *NamesBuilder {
return b.SetField(NamesGivenNameKey, in)
}
func (b *NamesBuilder) HonorificPrefix(in string) *NamesBuilder {
return b.SetField(NamesHonorificPrefixKey, in)
}
func (b *NamesBuilder) HonorificSuffix(in string) *NamesBuilder {
return b.SetField(NamesHonorificSuffixKey, in)
}
func (b *NamesBuilder) MiddleName(in string) *NamesBuilder {
return b.SetField(NamesMiddleNameKey, 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 *NamesBuilder) SetField(name string, value interface{}) *NamesBuilder {
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 *NamesBuilder) Build() (*Names, 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 *NamesBuilder) MustBuild() *Names {
object, err := b.Build()
if err != nil {
panic(err)
}
return object
}
func (b *NamesBuilder) From(in *Names) *NamesBuilder {
b.mu.Lock()
defer b.mu.Unlock()
b.once.Do(b.initialize)
if b.err != nil {
return b
}
var cloned Names
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 *Names) 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 *Names) 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 (*Names) 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) Names() *NamesBuilder {
return &NamesBuilder{}
}