-
Notifications
You must be signed in to change notification settings - Fork 2
/
container.go
187 lines (148 loc) · 3.58 KB
/
container.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
184
185
186
187
// Copyright 2018 Axel Etcheverry. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
package service
import (
"fmt"
"log"
"reflect"
"sync"
)
// Container interface.
type Container interface {
// SetValue static
SetValue(name string, v interface{})
// Set service
Set(name string, f ContainerFunc)
// Has service exists
Has(name string) bool
// Get service
Get(name string) interface{}
// GetKeys of all services
GetKeys() []string
// Fill dst
Fill(name string, dst interface{})
// Extend service
Extend(name string, f ExtenderFunc)
}
// ContainerFunc type.
type ContainerFunc func(container Container) interface{}
// ExtenderFunc type.
type ExtenderFunc interface{}
type container struct {
values map[string]ContainerFunc // contains the original closure to generate the service
extends map[string][]reflect.Value // contains the extends closure
services map[string]interface{} // contains the instantiated services
mtx *sync.RWMutex
}
// New constructor.
func New() Container {
return &container{
services: make(map[string]interface{}),
values: make(map[string]ContainerFunc),
extends: make(map[string][]reflect.Value),
mtx: &sync.RWMutex{},
}
}
// SetValue static.
func (c *container) SetValue(name string, v interface{}) {
c.mtx.Lock()
defer c.mtx.Unlock()
if _, ok := c.services[name]; ok {
log.Panic("Cannot overwrite initialized service")
}
c.services[name] = v
}
// Set service.
func (c *container) Set(name string, f ContainerFunc) {
c.mtx.Lock()
defer c.mtx.Unlock()
if _, ok := c.services[name]; ok {
log.Panic("Cannot overwrite initialized service")
}
c.values[name] = f
}
// Has service exists.
func (c *container) Has(name string) bool {
c.mtx.RLock()
defer c.mtx.RUnlock()
if _, ok := c.values[name]; ok {
return true
}
if _, ok := c.services[name]; ok {
return true
}
return false
}
// Get service.
func (c *container) Get(name string) interface{} {
c.mtx.RLock()
v, ok := c.services[name]
c.mtx.RUnlock()
if ok {
return v
}
c.mtx.RLock()
f, ok := c.values[name]
c.mtx.RUnlock()
if !ok {
panic(fmt.Sprintf("The service does not exist: %s", name))
}
v = f(c)
c.mtx.Lock()
c.services[name] = v
c.mtx.Unlock()
// apply extends to service
if extends, ok := c.extends[name]; ok {
for _, extend := range extends {
result := extend.Call([]reflect.Value{
reflect.ValueOf(v),
reflect.ValueOf(c),
})
c.mtx.Lock()
c.services[name] = result[0].Interface()
c.mtx.Unlock()
}
}
return v
}
// GetKeys of all services.
func (c *container) GetKeys() []string {
c.mtx.RLock()
defer c.mtx.RUnlock()
keys := make([]string, 0)
for k := range c.values {
keys = append(keys, k)
}
return keys
}
// Fill dst.
func (c *container) Fill(name string, dst interface{}) {
obj := c.Get(name)
if err := fill(obj, dst); err != nil {
log.Panic(err)
}
}
// Extend service.
func (c *container) Extend(name string, f ExtenderFunc) {
c.mtx.Lock()
defer c.mtx.Unlock()
if _, ok := c.services[name]; ok {
log.Panic("Cannot extend initialized service")
}
if _, ok := c.values[name]; !ok {
log.Panicf("Cannot extend %s service", name)
}
c.extends[name] = append(c.extends[name], reflect.ValueOf(f))
}
func fill(src, dest interface{}) (err error) {
defer func() {
if r := recover(); r != nil {
d := reflect.TypeOf(dest)
s := reflect.TypeOf(src)
err = fmt.Errorf("the fill destination should be a pointer to a `%s`, but you used a `%s`", s, d)
}
}()
reflect.ValueOf(dest).Elem().Set(reflect.ValueOf(src))
return err
}