-
Notifications
You must be signed in to change notification settings - Fork 5
/
container.go
139 lines (116 loc) · 4.19 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
// Copyright (c) 2021 BitMaelum Authors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package container
import "sync"
/*
* This is a very basic container system. We use this to easily locate services (ie: service locator) inside our code.
* With this container, we can also easily change a service to another instance, like a mocking service.
* From the code point of view, we still can use container.Get("service"), while we have set this to a mocked service.
*
* There is no functionality for dependencies etc, but we have shared/unshared services.
*/
// ServiceFunc is the function that needs to be resolved in the definition
type ServiceFunc func() (interface{}, error)
// ServiceType defines what kind of service it is (singleton, or new instance on each call)
type ServiceType int
// Service types
const (
ServiceTypeShared ServiceType = iota // Service is shared. Each call returns the same instance
ServiceTypeNonShared // Service is not shared. Each call returns a new instance
)
// ServiceDefinition is a single service definition
type ServiceDefinition struct {
Func ServiceFunc
Type ServiceType
}
// Container is the interface each container needs to implement
type Container interface {
SetShared(key string, f ServiceFunc)
SetNonShared(key string, f ServiceFunc)
Get(key string) interface{}
Has(key string) bool
}
// Type is the main container structure holding all service
type Type struct {
mu sync.Mutex
definitions map[string]*ServiceDefinition
resolved map[string]interface{}
}
// NewContainer will create a new container
func NewContainer() Container {
return &Type{
definitions: make(map[string]*ServiceDefinition),
resolved: make(map[string]interface{}),
}
}
// SetNonShared will set the function for the given service. It is not shared, meaning you will get a new instance on each call to get
func (c *Type) SetNonShared(key string, f ServiceFunc) {
c.mu.Lock()
defer c.mu.Unlock()
c.definitions[key] = &ServiceDefinition{
Func: f,
Type: ServiceTypeNonShared,
}
// Delete existing resolved object if any
delete(c.resolved, key)
}
// SetShared will set the function for the given service. It will return a shared instance on each call to get
func (c *Type) SetShared(key string, f ServiceFunc) {
c.mu.Lock()
defer c.mu.Unlock()
c.definitions[key] = &ServiceDefinition{
Func: f,
Type: ServiceTypeShared,
}
// Delete existing resolved object if any
delete(c.resolved, key)
}
// Has will return true when the definition exists
func (c *Type) Has(key string) bool {
_, ok := c.definitions[key]
return ok
}
// Get will retrieve the function for the given service, or nil when not found
func (c *Type) Get(key string) interface{} {
s, ok := c.definitions[key]
if !ok {
return nil
}
// Multi means we don't use a shared instance but instead instantiate a new object each time called
if s.Type == ServiceTypeNonShared {
obj, err := s.Func()
if err != nil {
return nil
}
return obj
}
// Already resolved, return
if c.resolved[key] != nil {
return c.resolved[key]
}
// Create instance, save it and return
obj, err := s.Func()
if err != nil {
return nil
}
c.mu.Lock()
c.resolved[key] = obj
c.mu.Unlock()
return obj
}