-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontainer.go
126 lines (102 loc) · 2.74 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
package gocontainer
import (
"fmt"
"reflect"
"sync"
)
// Container interface
type Container interface {
Register(id string, object interface{})
Deregister(id string)
Has(id string) bool
Get(id string) (interface{}, bool)
MustGet(id string) interface{}
Invoke(id string, fn interface{})
MustInvoke(id string, fn interface{})
MustInvokeMany(ids ...string) func(fn interface{})
}
type container struct {
sync.RWMutex
registry map[string]interface{}
}
// New creates new container
func New() Container {
return &container{
registry: make(map[string]interface{}),
}
}
// Register service by id
func (c *container) Register(id string, object interface{}) {
if c.registry == nil {
c.registry = make(map[string]interface{})
}
c.Lock()
c.registry[id] = object
c.Unlock()
}
// Deregister service be id
func (c *container) Deregister(id string) {
c.Lock()
delete(c.registry, id)
c.Unlock()
}
// Has checks if container has an object
func (c *container) Has(id string) bool {
_, ok := c.Get(id)
return ok
}
// Get a service by id
func (c *container) Get(id string) (interface{}, bool) {
c.RLock()
o, ok := c.registry[id]
c.RUnlock()
return o, ok
}
// MustGet calls Get underneath
// will panic if object not found within container
func (c *container) MustGet(id string) interface{} {
o, ok := c.Get(id)
if !ok {
panic(fmt.Sprintf("Object <%s> nof found within a container", id))
}
return o
}
// Invoke gets a service safely typed by passing it to a closure
// will panic if callback is not a function
func (c *container) Invoke(id string, fn interface{}) {
if reflect.TypeOf(fn).Kind() != reflect.Func {
panic(fmt.Sprintf("%s is not a reflect.Func", reflect.TypeOf(fn)))
}
o, ok := c.Get(id)
callback := reflect.ValueOf(fn)
args := []reflect.Value{reflect.ValueOf(o), reflect.ValueOf(ok)}
callback.Call(args)
}
// MustInvoke calls MustGet underneath
// will panic if object not found within container
func (c *container) MustInvoke(id string, fn interface{}) {
if reflect.TypeOf(fn).Kind() != reflect.Func {
panic(fmt.Sprintf("%s is not a reflect.Func", reflect.TypeOf(fn)))
}
o := c.MustGet(id)
callback := reflect.ValueOf(fn)
args := []reflect.Value{reflect.ValueOf(o)}
callback.Call(args)
}
// MustInvokeMany calls MustInvoke underneath
// returns many services from container
// will panic if object not found within container
func (c *container) MustInvokeMany(ids ...string) func(fn interface{}) {
var args []reflect.Value
for _, id := range ids {
o := c.MustGet(id)
args = append(args, reflect.ValueOf(o))
}
return func(fn interface{}) {
if reflect.TypeOf(fn).Kind() != reflect.Func {
panic(fmt.Sprintf("%s is not a reflect.Func", reflect.TypeOf(fn)))
}
callback := reflect.ValueOf(fn)
callback.Call(args)
}
}