/
registry.go
669 lines (573 loc) · 23.5 KB
/
registry.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
package ast
import (
"errors"
"fmt"
"strings"
"sync"
"github.com/influx6/faux/metrics"
"github.com/influx6/moz/gen"
)
// TypeAnnotationGenerator defines a function which generates specific code related to the giving
// Annotation for a non-struct, non-interface type declaration. This allows you to apply and create
// new sources specifically for a giving type(non-struct, non-interface).
// It is responsible to fully contain all operations required to both generator any source and write such to
type TypeAnnotationGenerator func(string, AnnotationDeclaration, TypeDeclaration, PackageDeclaration, Package) ([]gen.WriteDirective, error)
// FunctionAnnotationGenerator defines a function which generates specific code related to the giving
// Annotation. This allows you to generate a new source file containg source code for a giving struct type.
// It is responsible to fully contain all operations required to both generator any source and write such to.
type FunctionAnnotationGenerator func(string, AnnotationDeclaration, FuncDeclaration, PackageDeclaration, Package) ([]gen.WriteDirective, error)
// StructAnnotationGenerator defines a function which generates specific code related to the giving
// Annotation. This allows you to generate a new source file containg source code for a giving struct type.
// It is responsible to fully contain all operations required to both generator any source and write such to.
type StructAnnotationGenerator func(string, AnnotationDeclaration, StructDeclaration, PackageDeclaration, Package) ([]gen.WriteDirective, error)
// InterfaceAnnotationGenerator defines a function which generates specific code related to the giving
// Annotation. This allows you to generate a new source file containg source code for a giving interface type.
// It is responsible to fully contain all operations required to both generator any source and write such to
// appropriate files as intended, meta-data about package, and file paths are already include in the PackageDeclaration.
type InterfaceAnnotationGenerator func(string, AnnotationDeclaration, InterfaceDeclaration, PackageDeclaration, Package) ([]gen.WriteDirective, error)
// PackageAnnotationGenerator defines a function which generates specific code related to the giving
// Annotation for a package. This allows you to apply and create new sources specifically because of a
// package wide annotation.
// It is responsible to fully contain all operations required to both generator any source and write such to
// All generators are expected to return
type PackageAnnotationGenerator func(string, AnnotationDeclaration, PackageDeclaration, Package) ([]gen.WriteDirective, error)
//===========================================================================================================
// Annotations defines a struct which contains a map of all annotation code generator.
type Annotations struct {
Types map[string]TypeAnnotationGenerator
Structs map[string]StructAnnotationGenerator
Functions map[string]FunctionAnnotationGenerator
Packages map[string]PackageAnnotationGenerator
Interfaces map[string]InterfaceAnnotationGenerator
}
// AnnotationRegistry defines a structure which contains giving list of possible
// annotation generators for both package level and type level declaration.
type AnnotationRegistry struct {
metrics metrics.Metrics
ml sync.RWMutex
typeAnnotations map[string]TypeAnnotationGenerator
structAnnotations map[string]StructAnnotationGenerator
pkgAnnotations map[string]PackageAnnotationGenerator
interfaceAnnotations map[string]InterfaceAnnotationGenerator
functionAnnotations map[string]FunctionAnnotationGenerator
}
// NewAnnotationRegistry returns a new instance of a AnnotationRegistry.
func NewAnnotationRegistry() *AnnotationRegistry {
return &AnnotationRegistry{
metrics: metrics.New(),
typeAnnotations: make(map[string]TypeAnnotationGenerator),
structAnnotations: make(map[string]StructAnnotationGenerator),
pkgAnnotations: make(map[string]PackageAnnotationGenerator),
interfaceAnnotations: make(map[string]InterfaceAnnotationGenerator),
functionAnnotations: make(map[string]FunctionAnnotationGenerator),
}
}
// NewAnnotationRegistryWith returns a new instance of a AnnotationRegistry.
func NewAnnotationRegistryWith(log metrics.Metrics) *AnnotationRegistry {
return &AnnotationRegistry{
metrics: log,
typeAnnotations: make(map[string]TypeAnnotationGenerator),
structAnnotations: make(map[string]StructAnnotationGenerator),
pkgAnnotations: make(map[string]PackageAnnotationGenerator),
interfaceAnnotations: make(map[string]InterfaceAnnotationGenerator),
functionAnnotations: make(map[string]FunctionAnnotationGenerator),
}
}
// Clone returns a type which contains all copies of the generators provided by
// the AnnotationRegistry.
func (a *AnnotationRegistry) Clone() Annotations {
a.ml.RLock()
defer a.ml.RUnlock()
var cloned Annotations
cloned.Types = make(map[string]TypeAnnotationGenerator)
cloned.Structs = make(map[string]StructAnnotationGenerator)
cloned.Packages = make(map[string]PackageAnnotationGenerator)
cloned.Interfaces = make(map[string]InterfaceAnnotationGenerator)
cloned.Functions = make(map[string]FunctionAnnotationGenerator)
for name, item := range a.pkgAnnotations {
cloned.Packages[name] = item
}
for name, item := range a.functionAnnotations {
cloned.Functions[name] = item
}
for name, item := range a.structAnnotations {
cloned.Structs[name] = item
}
for name, item := range a.typeAnnotations {
cloned.Types[name] = item
}
for name, item := range a.interfaceAnnotations {
cloned.Interfaces[name] = item
}
return cloned
}
// CopyStrategy defines a int type used to represent a copy strategy for
// cloning a AnnotationStrategy.
type CopyStrategy int
// Contains different copy strategy.
const (
OursOverTheirs CopyStrategy = iota + 1
TheirsOverOurs
)
// Copy copies over all available type generators from the provided AnnotationRegistry with
// the CopyStrategy.
func (a *AnnotationRegistry) Copy(registry *AnnotationRegistry, strategy CopyStrategy) {
cloned := registry.Clone()
a.ml.Lock()
defer a.ml.Unlock()
for name, item := range cloned.Packages {
_, ok := a.pkgAnnotations[name]
if !ok || (ok && strategy == TheirsOverOurs) {
a.pkgAnnotations[name] = item
}
}
for name, item := range cloned.Functions {
_, ok := a.functionAnnotations[name]
if !ok || (ok && strategy == TheirsOverOurs) {
a.functionAnnotations[name] = item
}
}
for name, item := range cloned.Types {
_, ok := a.typeAnnotations[name]
if !ok || (ok && strategy == TheirsOverOurs) {
a.typeAnnotations[name] = item
}
}
for name, item := range cloned.Structs {
_, ok := a.structAnnotations[name]
if !ok || (ok && strategy == TheirsOverOurs) {
a.structAnnotations[name] = item
}
}
for name, item := range cloned.Interfaces {
_, ok := a.interfaceAnnotations[name]
if !ok || (ok && strategy == TheirsOverOurs) {
a.interfaceAnnotations[name] = item
}
}
}
// MustPackage returns the annotation generator associated with the giving annotation name.
func (a *AnnotationRegistry) MustPackage(annotation string) PackageAnnotationGenerator {
annon, err := a.GetPackage(annotation)
if err == nil {
return annon
}
panic(err)
}
// AnnotationWriteDirective defines a type which provides a WriteDiretive and the associated
// name.
type AnnotationWriteDirective struct {
gen.WriteDirective
Annotation string
}
// ParseDeclr runs the generators suited for each declaration and type returning a slice of
// Annotationgen.WriteDirective that delivers the content to be created for each piece.
func (a *AnnotationRegistry) ParseDeclr(pkg Package, declr PackageDeclaration, toDir string) ([]AnnotationWriteDirective, error) {
var directives []AnnotationWriteDirective
// Generate directives for package level
for _, annotation := range declr.Annotations {
a.metrics.Emit(metrics.Info("Directive Generation"),
metrics.With("Level", "Package"), metrics.With("Annotaton", annotation.Name), metrics.With("Params", annotation.Params), metrics.With("Arguments", annotation.Arguments), metrics.With("Template", annotation.Template))
generator, err := a.GetPackage(annotation.Name)
if err != nil {
continue
}
drs, err := generator(toDir, annotation, declr, pkg)
if err != nil {
a.metrics.Emit(metrics.Error(errors.New("Directive Generation")),
metrics.With("error", err), metrics.With("Level", "Package"), metrics.With("Annotaton", annotation.Name), metrics.With("Params", annotation.Params), metrics.With("Arguments", annotation.Arguments), metrics.With("Template", annotation.Template))
return nil, err
}
a.metrics.Emit(metrics.Info("Directive Generation: Success"),
metrics.With("Level", "Package"),
metrics.With("Directive", len(drs)),
metrics.With("Annotaton", annotation.Name),
metrics.With("Params", annotation.Params),
metrics.With("Arguments", annotation.Arguments),
metrics.With("Template", annotation.Template))
for _, directive := range drs {
directives = append(directives, AnnotationWriteDirective{
WriteDirective: directive,
Annotation: annotation.Name,
})
}
}
for _, inter := range declr.Interfaces {
for _, annotation := range inter.Annotations {
a.metrics.Emit(metrics.Info("Directive Generation"),
metrics.With("Level", "Interface"),
metrics.With("Annotaton", annotation.Name),
metrics.With("Interface", inter.Object.Name.Name),
metrics.With("Params", annotation.Params),
metrics.With("Arguments", annotation.Arguments),
metrics.With("Template", annotation.Template))
generator, err := a.GetInterfaceType(annotation.Name)
if err != nil {
a.metrics.Emit(metrics.Error(errors.New("Directive Generation")),
metrics.With("error", err),
metrics.With("Level", "Interface"),
metrics.With("Annotaton", annotation.Name),
metrics.With("Interface", inter.Object.Name.Name),
metrics.With("Params", annotation.Params),
metrics.With("Arguments", annotation.Arguments),
metrics.With("Template", annotation.Template))
continue
}
drs, err := generator(toDir, annotation, inter, declr, pkg)
if err != nil {
a.metrics.Emit(metrics.Error(errors.New("Directive Generation")),
metrics.With("error", err),
metrics.With("Level", "Interface"),
metrics.With("Annotaton", annotation.Name),
metrics.With("Interface", inter.Object.Name.Name),
metrics.With("Params", annotation.Params),
metrics.With("Arguments", annotation.Arguments),
metrics.With("Template", annotation.Template))
return nil, err
}
a.metrics.Emit(metrics.Info("Directive Generation: Success"),
metrics.With("Level", "Interface"),
metrics.With("Directive", len(drs)),
metrics.With("Annotaton", annotation.Name),
metrics.With("nterface", inter.Object.Name.Name),
metrics.With("Params", annotation.Params),
metrics.With("Arguments", annotation.Arguments),
metrics.With("Template", annotation.Template))
for _, directive := range drs {
directives = append(directives, AnnotationWriteDirective{
WriteDirective: directive,
Annotation: annotation.Name,
})
}
}
}
for _, structs := range declr.Structs {
for _, annotation := range structs.Annotations {
a.metrics.Emit(metrics.Info("Directive Generation"),
metrics.With("Level", "Struct"),
metrics.With("Annotaton", annotation.Name),
metrics.With("Struct", structs.Object.Name.Name),
metrics.With("Params", annotation.Params),
metrics.With("Arguments", annotation.Arguments),
metrics.With("Template", annotation.Template))
generator, err := a.GetStructType(annotation.Name)
if err != nil {
a.metrics.Emit(metrics.Error(errors.New("Directive Generation")),
metrics.With("error", err),
metrics.With("Level", "Struct"),
metrics.With("Annotaton", annotation.Name),
metrics.With("Struct", structs.Object.Name.Name),
metrics.With("Params", annotation.Params),
metrics.With("Arguments", annotation.Arguments),
metrics.With("Template", annotation.Template))
continue
}
drs, err := generator(toDir, annotation, structs, declr, pkg)
if err != nil {
a.metrics.Emit(metrics.Error(errors.New("Directive Generation")),
metrics.With("error", err),
metrics.With("Level", "Struct"),
metrics.With("Annotaton", annotation.Name),
metrics.With("Struct", structs.Object.Name.Name),
metrics.With("Params", annotation.Params),
metrics.With("Arguments", annotation.Arguments),
metrics.With("Template", annotation.Template))
return nil, err
}
a.metrics.Emit(metrics.Info("Directive Generation: Success"),
metrics.With("Level", "Struct"),
metrics.With("Directive", len(drs)),
metrics.With("Annotaton", annotation.Name),
metrics.With("Struct", structs.Object.Name.Name),
metrics.With("Params", annotation.Params),
metrics.With("Arguments", annotation.Arguments),
metrics.With("Template", annotation.Template))
for _, directive := range drs {
directives = append(directives, AnnotationWriteDirective{
WriteDirective: directive,
Annotation: annotation.Name,
})
}
}
}
for _, typ := range declr.Functions {
for _, annotation := range typ.Annotations {
a.metrics.Emit(metrics.Info("Directive Generation"),
metrics.With("Level", "Type"),
metrics.With("Annotaton", annotation.Name),
metrics.With("Function", typ.FuncDeclr.Name.Name),
metrics.With("Params", annotation.Params),
metrics.With("Arguments", annotation.Arguments),
metrics.With("Template", annotation.Template))
generator, err := a.GetFunctionType(annotation.Name)
if err != nil {
a.metrics.Emit(metrics.Error(errors.New("Directive Generation")),
metrics.With("error", err),
metrics.With("Level", "Type"),
metrics.With("Annotaton", annotation.Name),
metrics.With("Function", typ.FuncDeclr.Name.Name),
metrics.With("Params", annotation.Params),
metrics.With("Arguments", annotation.Arguments),
metrics.With("Template", annotation.Template))
continue
}
drs, err := generator(toDir, annotation, typ, declr, pkg)
if err != nil {
a.metrics.Emit(metrics.Error(errors.New("Directive Generation")),
metrics.With("error", err),
metrics.With("Level", "Type"),
metrics.With("Annotaton", annotation.Name),
metrics.With("Function", typ.FuncDeclr.Name.Name),
metrics.With("Params", annotation.Params),
metrics.With("Arguments", annotation.Arguments),
metrics.With("Template", annotation.Template))
return nil, err
}
a.metrics.Emit(metrics.Info("Directive Generation: Success"),
metrics.With("Level", "Type"),
metrics.With("Directive", len(drs)),
metrics.With("Annotaton", annotation.Name),
metrics.With("Function", typ.FuncDeclr.Name.Name),
metrics.With("Params", annotation.Params),
metrics.With("Arguments", annotation.Arguments),
metrics.With("Template", annotation.Template))
for _, directive := range drs {
directives = append(directives, AnnotationWriteDirective{
WriteDirective: directive,
Annotation: annotation.Name,
})
}
}
}
for _, typ := range declr.Types {
for _, annotation := range typ.Annotations {
a.metrics.Emit(metrics.Info("Directive Generation"),
metrics.With("Level", "Type"),
metrics.With("Annotaton", annotation.Name),
metrics.With("Type", typ.Object.Name.Name),
metrics.With("Params", annotation.Params),
metrics.With("Arguments", annotation.Arguments),
metrics.With("Template", annotation.Template))
generator, err := a.GetType(annotation.Name)
if err != nil {
a.metrics.Emit(metrics.Error(errors.New("Directive Generation")),
metrics.With("error", err),
metrics.With("Level", "Type"),
metrics.With("Annotaton", annotation.Name),
metrics.With("Type", typ.Object.Name.Name),
metrics.With("Params", annotation.Params),
metrics.With("Arguments", annotation.Arguments),
metrics.With("Template", annotation.Template))
continue
}
drs, err := generator(toDir, annotation, typ, declr, pkg)
if err != nil {
a.metrics.Emit(metrics.Error(errors.New("Directive Generation")),
metrics.With("error", err),
metrics.With("Level", "Type"),
metrics.With("Annotaton", annotation.Name),
metrics.With("Type", typ.Object.Name.Name),
metrics.With("Params", annotation.Params),
metrics.With("Arguments", annotation.Arguments),
metrics.With("Template", annotation.Template))
return nil, err
}
a.metrics.Emit(metrics.Info("Directive Generation: Success"),
metrics.With("Level", "Type"),
metrics.With("Directive", len(drs)),
metrics.With("Annotaton", annotation.Name),
metrics.With("Type", typ.Object.Name.Name),
metrics.With("Params", annotation.Params),
metrics.With("Arguments", annotation.Arguments),
metrics.With("Template", annotation.Template))
for _, directive := range drs {
directives = append(directives, AnnotationWriteDirective{
WriteDirective: directive,
Annotation: annotation.Name,
})
}
}
}
return directives, nil
}
// GetPackage returns the annotation generator associated with the giving annotation name.
func (a *AnnotationRegistry) GetPackage(annotation string) (PackageAnnotationGenerator, error) {
annotation = strings.TrimPrefix(annotation, "@")
var annon PackageAnnotationGenerator
var ok bool
a.ml.RLock()
{
annon, ok = a.pkgAnnotations[annotation]
}
a.ml.RUnlock()
if !ok {
return nil, fmt.Errorf("Package Annotation @%s not found", annotation)
}
return annon, nil
}
// MustInterfaceType returns the annotation generator associated with the giving annotation name.
func (a *AnnotationRegistry) MustInterfaceType(annotation string) InterfaceAnnotationGenerator {
annon, err := a.GetInterfaceType(annotation)
if err == nil {
return annon
}
panic(err)
}
// MustFunctionType returns the annotation generator associated with the giving annotation name.
func (a *AnnotationRegistry) MustFunctionType(annotation string) FunctionAnnotationGenerator {
annon, err := a.GetFunctionType(annotation)
if err == nil {
return annon
}
panic(err)
}
// GetFunctionType returns the annotation generator associated with the giving annotation name.
func (a *AnnotationRegistry) GetFunctionType(annotation string) (FunctionAnnotationGenerator, error) {
annotation = strings.TrimPrefix(annotation, "@")
var annon FunctionAnnotationGenerator
var ok bool
a.ml.RLock()
{
annon, ok = a.functionAnnotations[annotation]
}
a.ml.RUnlock()
if !ok {
return nil, fmt.Errorf("Function/Method Annotation @%s not found", annotation)
}
return annon, nil
}
// GetInterfaceType returns the annotation generator associated with the giving annotation name.
func (a *AnnotationRegistry) GetInterfaceType(annotation string) (InterfaceAnnotationGenerator, error) {
annotation = strings.TrimPrefix(annotation, "@")
var annon InterfaceAnnotationGenerator
var ok bool
a.ml.RLock()
{
annon, ok = a.interfaceAnnotations[annotation]
}
a.ml.RUnlock()
if !ok {
return nil, fmt.Errorf("Interface Annotation @%s not found", annotation)
}
return annon, nil
}
// MustStructType returns the annotation generator associated with the giving annotation name.
func (a *AnnotationRegistry) MustStructType(annotation string) StructAnnotationGenerator {
annon, err := a.GetStructType(annotation)
if err == nil {
return annon
}
panic(err)
}
// GetStructType returns the annotation generator associated with the giving annotation name.
func (a *AnnotationRegistry) GetStructType(annotation string) (StructAnnotationGenerator, error) {
annotation = strings.TrimPrefix(annotation, "@")
var annon StructAnnotationGenerator
var ok bool
a.ml.RLock()
{
annon, ok = a.structAnnotations[annotation]
}
a.ml.RUnlock()
if !ok {
return nil, fmt.Errorf("Struct Annotation @%s not found", annotation)
}
return annon, nil
}
// MustType returns the annotation generator associated with the giving annotation name.
func (a *AnnotationRegistry) MustType(annotation string) TypeAnnotationGenerator {
annon, err := a.GetType(annotation)
if err == nil {
return annon
}
panic(err)
}
// GetType returns the annotation generator associated with the giving annotation name.
func (a *AnnotationRegistry) GetType(annotation string) (TypeAnnotationGenerator, error) {
annotation = strings.TrimPrefix(annotation, "@")
var annon TypeAnnotationGenerator
var ok bool
a.ml.RLock()
{
annon, ok = a.typeAnnotations[annotation]
}
a.ml.RUnlock()
if !ok {
return nil, fmt.Errorf("Type Annotation @%s not found", annotation)
}
return annon, nil
}
// Register which adds the generator depending on it's type into the appropriate
// registry. It only supports the following generators:
// 1. TypeAnnotationGenerator (see Package ast#TypeAnnotationGenerator)
// 2. StructAnnotationGenerator (see Package ast#StructAnnotationGenerator)
// 3. InterfaceAnnotationGenerator (see Package ast#InterfaceAnnotationGenerator)
// 4. PackageAnnotationGenerator (see Package ast#PackageAnnotationGenerator)
// Any other type will cause the return of an error.
func (a *AnnotationRegistry) Register(name string, generator interface{}) error {
switch gen := generator.(type) {
case PackageAnnotationGenerator:
a.RegisterPackage(name, gen)
return nil
case func(string, AnnotationDeclaration, PackageDeclaration, Package) ([]gen.WriteDirective, error):
a.RegisterPackage(name, gen)
return nil
case TypeAnnotationGenerator:
a.RegisterType(name, gen)
return nil
case func(string, AnnotationDeclaration, TypeDeclaration, PackageDeclaration, Package) ([]gen.WriteDirective, error):
a.RegisterType(name, gen)
return nil
case StructAnnotationGenerator:
a.RegisterStructType(name, gen)
return nil
case func(string, AnnotationDeclaration, StructDeclaration, PackageDeclaration, Package) ([]gen.WriteDirective, error):
a.RegisterStructType(name, gen)
return nil
case InterfaceAnnotationGenerator:
a.RegisterInterfaceType(name, gen)
return nil
case func(string, AnnotationDeclaration, InterfaceDeclaration, PackageDeclaration, Package) ([]gen.WriteDirective, error):
a.RegisterInterfaceType(name, gen)
return nil
default:
return fmt.Errorf("Generator type for %q not supported: %#v", name, generator)
}
}
// RegisterInterfaceType adds a interface type level annotation generator into the registry.
func (a *AnnotationRegistry) RegisterInterfaceType(annotation string, generator InterfaceAnnotationGenerator) {
annotation = strings.TrimPrefix(annotation, "@")
a.ml.Lock()
{
a.interfaceAnnotations[annotation] = generator
}
a.ml.Unlock()
}
// RegisterStructType adds a struct type level annotation generator into the registry.
func (a *AnnotationRegistry) RegisterStructType(annotation string, generator StructAnnotationGenerator) {
annotation = strings.TrimPrefix(annotation, "@")
a.ml.Lock()
{
a.structAnnotations[annotation] = generator
}
a.ml.Unlock()
}
// RegisterType adds a type(non-struct, non-interface) level annotation generator into the registry.
func (a *AnnotationRegistry) RegisterType(annotation string, generator TypeAnnotationGenerator) {
annotation = strings.TrimPrefix(annotation, "@")
a.ml.Lock()
{
a.typeAnnotations[annotation] = generator
}
a.ml.Unlock()
}
// RegisterPackage adds a package level annotation generator into the registry.
func (a *AnnotationRegistry) RegisterPackage(annotation string, generator PackageAnnotationGenerator) {
annotation = strings.TrimPrefix(annotation, "@")
a.ml.Lock()
{
a.pkgAnnotations[annotation] = generator
}
a.ml.Unlock()
}
//===========================================================================================================