forked from kubernetes-retired/heapster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.go
183 lines (156 loc) · 3.59 KB
/
template.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
package main
var extpointsTemplate = `// generated by go-extpoints -- DO NOT EDIT
package {{.Package}}
import (
"reflect"
"runtime"
"strings"
"sync"
)
var extRegistry = ®istryType{m: make(map[string]*extensionPoint)}
type registryType struct {
sync.Mutex
m map[string]*extensionPoint
}
// Top level registration
func extensionTypes(extension interface{}) []string {
var ifaces []string
typ := reflect.TypeOf(extension)
for name, ep := range extRegistry.m {
if ep.iface.Kind() == reflect.Func && typ.AssignableTo(ep.iface) {
ifaces = append(ifaces, name)
}
if ep.iface.Kind() != reflect.Func && typ.Implements(ep.iface) {
ifaces = append(ifaces, name)
}
}
return ifaces
}
func RegisterExtension(extension interface{}, name string) []string {
extRegistry.Lock()
defer extRegistry.Unlock()
var ifaces []string
for _, iface := range extensionTypes(extension) {
if extRegistry.m[iface].register(extension, name) {
ifaces = append(ifaces, iface)
}
}
return ifaces
}
func UnregisterExtension(name string) []string {
extRegistry.Lock()
defer extRegistry.Unlock()
var ifaces []string
for iface, extpoint := range extRegistry.m {
if extpoint.unregister(name) {
ifaces = append(ifaces, iface)
}
}
return ifaces
}
// Base extension point
type extensionPoint struct {
sync.Mutex
iface reflect.Type
extensions map[string]interface{}
}
func newExtensionPoint(iface interface{}) *extensionPoint {
ep := &extensionPoint{
iface: reflect.TypeOf(iface).Elem(),
extensions: make(map[string]interface{}),
}
extRegistry.Lock()
extRegistry.m[ep.iface.Name()] = ep
extRegistry.Unlock()
return ep
}
func (ep *extensionPoint) lookup(name string) interface{} {
ep.Lock()
defer ep.Unlock()
ext, ok := ep.extensions[name]
if !ok {
return nil
}
return ext
}
func (ep *extensionPoint) all() map[string]interface{} {
ep.Lock()
defer ep.Unlock()
all := make(map[string]interface{})
for k, v := range ep.extensions {
all[k] = v
}
return all
}
func (ep *extensionPoint) register(extension interface{}, name string) bool {
ep.Lock()
defer ep.Unlock()
if name == "" {
typ := reflect.TypeOf(extension)
if typ.Kind() == reflect.Func {
nameParts := strings.Split(runtime.FuncForPC(
reflect.ValueOf(extension).Pointer()).Name(), ".")
name = nameParts[len(nameParts)-1]
} else {
name = typ.Elem().Name()
}
}
_, exists := ep.extensions[name]
if exists {
return false
}
ep.extensions[name] = extension
return true
}
func (ep *extensionPoint) unregister(name string) bool {
ep.Lock()
defer ep.Unlock()
_, exists := ep.extensions[name]
if !exists {
return false
}
delete(ep.extensions, name)
return true
}
{{range .ExtensionPoints}}// {{.Name}}
var {{.Var}} = &{{.Type}}{
newExtensionPoint(new({{.Name}})),
}
type {{.Type}} struct {
*extensionPoint
}
func (ep *{{.Type}}) Unregister(name string) bool {
return ep.unregister(name)
}
func (ep *{{.Type}}) Register(extension {{.Name}}, name string) bool {
return ep.register(extension, name)
}
func (ep *{{.Type}}) Lookup(name string) {{.Name}} {
ext := ep.lookup(name)
if ext == nil {
return nil
}
return ext.({{.Name}})
}
func (ep *{{.Type}}) Select(names []string) []{{.Name}} {
var selected []{{.Name}}
for _, name := range names {
selected = append(selected, ep.Lookup(name))
}
return selected
}
func (ep *{{.Type}}) All() map[string]{{.Name}} {
all := make(map[string]{{.Name}})
for k, v := range ep.all() {
all[k] = v.({{.Name}})
}
return all
}
func (ep *{{.Type}}) Names() []string {
var names []string
for k := range ep.all() {
names = append(names, k)
}
return names
}
{{end}}`