-
Notifications
You must be signed in to change notification settings - Fork 19
/
registry.go
60 lines (50 loc) · 1.56 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
package runtime
import (
"fmt"
)
// Registry contains a map of objects info structures by their kind
type Registry struct {
Kinds map[string]*Info
}
// NewRegistry creates a new Registry
func NewRegistry() *Registry {
return &Registry{
Kinds: make(map[string]*Info),
}
}
// Append adds specified list of object Info into the registry
func (reg *Registry) Append(infos ...*Info) *Registry {
for _, info := range infos {
reg.validateInfo(info)
reg.Kinds[info.Kind] = info
}
return reg
}
// Get looks up object informational structure given its kind
func (reg *Registry) Get(kind Kind) *Info {
info, exist := reg.Kinds[kind]
if !exist {
panic(fmt.Sprintf("Kind '%s' isn't registered", kind))
}
return info
}
func (reg *Registry) validateInfo(info *Info) {
kind := info.Kind
if len(kind) == 0 {
panic(fmt.Sprintf("Kind can't be empty"))
}
if _, exist := reg.Kinds[kind]; exist {
panic(fmt.Sprintf("Kind can't be duplicated: %s", kind))
}
obj := info.New()
if _, ok := obj.(Storable); info.Storable && !ok {
panic(fmt.Sprintf("Kind '%s' registered as Storable but doesn't implement corresponding interface", kind))
} /* else if !info.Storable && ok {
log.Debugf("Kind '%s' registered as non-Storable but implements corresponding interface", kind)
} */
if _, ok := obj.(Versioned); info.Versioned && !ok {
panic(fmt.Sprintf("Kind '%s' registered as Versioned but doesn't implement corresponding interface", kind))
} /* else if !info.Versioned && ok {
log.Debugf("Kind '%s' registered as non-Versioned but implements corresponding interface", kind)
} */
}