This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 63
/
registry.go
98 lines (81 loc) · 2.3 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
package plugins
import (
"fmt"
"sync"
"sync/atomic"
)
type PluginID = string
const (
PluginIDWorkflowExecutor PluginID = "WorkflowExecutor"
PluginIDDataProxy PluginID = "DataProxy"
PluginIDUnaryServiceMiddleware PluginID = "UnaryServiceMiddleware"
)
type AtomicRegistry struct {
atomic.Value
}
// Store stores the Registry to be retrieved later.
func (a *AtomicRegistry) Store(r *Registry) {
a.Value.Store(r)
}
// Load loads the stored Registry or nil if non exists
func (a *AtomicRegistry) Load() *Registry {
return a.Value.Load().(*Registry)
}
// NewAtomicRegistry creates a new envelope to hold a Registry object.
func NewAtomicRegistry(initialValue *Registry) AtomicRegistry {
val := atomic.Value{}
val.Store(initialValue)
return AtomicRegistry{
Value: val,
}
}
// Registry is a generic plugin registrar for dependency injection.
type Registry struct {
m sync.Map
mDefault sync.Map
}
// Register registers a new implementation for the pluginID. Only one plugin is allowed to be registered
// for a given ID.
func (r *Registry) Register(id PluginID, impl interface{}) error {
_, loaded := r.m.LoadOrStore(id, impl)
if loaded {
return fmt.Errorf("an existing implementation has already been registered for [%v]", id)
}
return nil
}
// RegisterDefault registers a new implementation for the pluginID. This is the implementation that will be used
// if no other plugin is registered for the ID.
func (r *Registry) RegisterDefault(id PluginID, impl interface{}) {
r.mDefault.Store(id, impl)
}
// Get retrieves a registered implementation for the ID. If one doesn't exist, it returns the default implementation.
// If the id isn't found, it returns nil.
func Get[T any](r *Registry, id PluginID) T {
obj := r.Get(id)
res, casted := obj.(T)
if !casted {
var zeroVal T
return zeroVal
}
return res
}
// Get retrieves a registered implementation for the ID. If one doesn't exist, it returns the default implementation.
// If the id isn't found, it returns nil.
func (r *Registry) Get(id PluginID) interface{} {
val, exists := r.m.Load(id)
if exists {
return val
}
val, exists = r.mDefault.Load(id)
if exists {
return val
}
return nil
}
// NewRegistry creates a new Registry
func NewRegistry() *Registry {
return &Registry{
m: sync.Map{},
mDefault: sync.Map{},
}
}