forked from bertildaniel/registrator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextpoints.go
141 lines (123 loc) · 2.85 KB
/
extpoints.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
// generated by go-extpoints -- DO NOT EDIT
package bridge
import (
"reflect"
"sync"
)
var registry = struct {
sync.Mutex
extpoints map[string]*extensionPoint
}{
extpoints: make(map[string]*extensionPoint),
}
type extensionPoint struct {
sync.Mutex
iface reflect.Type
components map[string]interface{}
}
func newExtensionPoint(iface interface{}) *extensionPoint {
ep := &extensionPoint{
iface: reflect.TypeOf(iface).Elem(),
components: make(map[string]interface{}),
}
registry.Lock()
defer registry.Unlock()
registry.extpoints[ep.iface.Name()] = ep
return ep
}
func (ep *extensionPoint) lookup(name string) (ext interface{}, ok bool) {
ep.Lock()
defer ep.Unlock()
ext, ok = ep.components[name]
return
}
func (ep *extensionPoint) all() map[string]interface{} {
ep.Lock()
defer ep.Unlock()
all := make(map[string]interface{})
for k, v := range ep.components {
all[k] = v
}
return all
}
func (ep *extensionPoint) register(component interface{}, name string) bool {
ep.Lock()
defer ep.Unlock()
if name == "" {
name = reflect.TypeOf(component).Elem().Name()
}
_, exists := ep.components[name]
if exists {
return false
}
ep.components[name] = component
return true
}
func (ep *extensionPoint) unregister(name string) bool {
ep.Lock()
defer ep.Unlock()
_, exists := ep.components[name]
if !exists {
return false
}
delete(ep.components, name)
return true
}
func implements(component interface{}) []string {
var ifaces []string
for name, ep := range registry.extpoints {
if reflect.TypeOf(component).Implements(ep.iface) {
ifaces = append(ifaces, name)
}
}
return ifaces
}
func Register(component interface{}, name string) []string {
registry.Lock()
defer registry.Unlock()
var ifaces []string
for _, iface := range implements(component) {
if ok := registry.extpoints[iface].register(component, name); ok {
ifaces = append(ifaces, iface)
}
}
return ifaces
}
func Unregister(name string) []string {
registry.Lock()
defer registry.Unlock()
var ifaces []string
for iface, extpoint := range registry.extpoints {
if ok := extpoint.unregister(name); ok {
ifaces = append(ifaces, iface)
}
}
return ifaces
}
// AdapterFactory
var AdapterFactories = &adapterFactoryExt{
newExtensionPoint(new(AdapterFactory)),
}
type adapterFactoryExt struct {
*extensionPoint
}
func (ep *adapterFactoryExt) Unregister(name string) bool {
return ep.unregister(name)
}
func (ep *adapterFactoryExt) Register(component AdapterFactory, name string) bool {
return ep.register(component, name)
}
func (ep *adapterFactoryExt) Lookup(name string) (AdapterFactory, bool) {
ext, ok := ep.lookup(name)
if !ok {
return nil, ok
}
return ext.(AdapterFactory), ok
}
func (ep *adapterFactoryExt) All() map[string]AdapterFactory {
all := make(map[string]AdapterFactory)
for k, v := range ep.all() {
all[k] = v.(AdapterFactory)
}
return all
}