/
registry.go
144 lines (108 loc) · 3.01 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
package container
import (
"codnect.io/reflector"
"fmt"
"sync"
)
type DefinitionRegistry interface {
Register(def *Definition) error
Remove(name string) error
Contains(name string) bool
Find(name string) (*Definition, bool)
Definitions() []*Definition
DefinitionNames() []string
DefinitionNamesByType(requiredType reflector.Type) []string
Count() int
}
type definitionRegistry struct {
definitionMap map[string]*Definition
muDefinitions sync.RWMutex
}
func NewDefinitionRegistry(defs []*Definition) DefinitionRegistry {
registry := &definitionRegistry{
definitionMap: make(map[string]*Definition),
muDefinitions: sync.RWMutex{},
}
for _, def := range defs {
registry.definitionMap[def.Name()] = def
}
return registry
}
func (c *definitionRegistry) Register(def *Definition) error {
if def == nil {
return fmt.Errorf("container: definition should not be nil")
}
defer c.muDefinitions.Unlock()
c.muDefinitions.Lock()
if _, exists := c.definitionMap[def.Name()]; exists {
return fmt.Errorf("container: definition with name %s already exists", def.Name())
}
c.definitionMap[def.Name()] = def
return nil
}
func (c *definitionRegistry) Remove(name string) error {
defer c.muDefinitions.Unlock()
c.muDefinitions.Lock()
if _, exists := c.definitionMap[name]; !exists {
return fmt.Errorf("container: no found definition with name %s", name)
}
delete(c.definitionMap, name)
return nil
}
func (c *definitionRegistry) Contains(name string) bool {
defer c.muDefinitions.Unlock()
c.muDefinitions.Lock()
_, exists := c.definitionMap[name]
return exists
}
func (c *definitionRegistry) Find(name string) (*Definition, bool) {
defer c.muDefinitions.Unlock()
c.muDefinitions.Lock()
if def, exists := c.definitionMap[name]; exists {
return def, true
}
return nil, false
}
func (c *definitionRegistry) Definitions() []*Definition {
defer c.muDefinitions.Unlock()
c.muDefinitions.Lock()
defs := make([]*Definition, 0)
for _, def := range c.definitionMap {
defs = append(defs, def)
}
return defs
}
func (c *definitionRegistry) DefinitionNames() []string {
defer c.muDefinitions.Unlock()
c.muDefinitions.Lock()
names := make([]string, 0)
for name, _ := range c.definitionMap {
names = append(names, name)
}
return names
}
func (c *definitionRegistry) DefinitionNamesByType(requiredType reflector.Type) []string {
defer c.muDefinitions.Unlock()
c.muDefinitions.Lock()
names := make([]string, 0)
if requiredType == nil {
return names
}
for name, def := range c.definitionMap {
instanceType := def.Type()
if instanceType.CanConvert(requiredType) {
names = append(names, name)
} else if reflector.IsPointer(instanceType) && !reflector.IsPointer(requiredType) && !reflector.IsInterface(requiredType) {
ptrType := reflector.ToPointer(instanceType)
if ptrType.Elem().CanConvert(requiredType) {
names = append(names, name)
}
}
}
return names
}
func (c *definitionRegistry) Count() int {
defer c.muDefinitions.Unlock()
c.muDefinitions.Lock()
return len(c.definitionMap)
}