This repository has been archived by the owner on Jun 5, 2019. It is now read-only.
/
container.go
227 lines (185 loc) · 5.52 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package bussard
import (
"fmt"
"reflect"
"strconv"
)
type (
// ServiceContainer is a wrapper around services indexed by a unique
// name. Services can be retrieved by name, or injected into a struct
// by reading tagged fields.
ServiceContainer interface {
// Get retrieves the service registered to the given key. It is an
// error for a service not to be registered to this key.
Get(key string) (interface{}, error)
// MustGet calls Get and panics on error.
MustGet(service string) interface{}
// Set registers a service with the given key. It is an error for
// a service to already be registered to this key.
Set(key string, service interface{}) error
// MustSet calls Set and panics on error.
MustSet(service string, value interface{})
// Inject will attempt to populate the given type with values from
// the service container based on the value's struct tags. An error
// may occur if a service has not been registered, a service has a
// different type than expected, or struct tags are malformed.
Inject(obj interface{}) error
}
// PostInject is a marker interface for injectable objects which should
// perform some action after injection of services.
PostInject interface {
PostInject() error
}
serviceContainer struct {
services map[string]interface{}
}
)
const (
serviceTag = "service"
optionalTag = "optional"
)
// NewServiceContainer creates an empty service container.
func NewServiceContainer() ServiceContainer {
return &serviceContainer{
services: map[string]interface{}{},
}
}
// Get retrieves the service registered to the given key. It is an
// error for a service not to be registered to this key.
func (c *serviceContainer) Get(key string) (interface{}, error) {
service, ok := c.services[key]
if !ok {
return nil, fmt.Errorf("no service registered to key `%s`", key)
}
return service, nil
}
// MustGet calls Get and panics on error.
func (c *serviceContainer) MustGet(service string) interface{} {
value, err := c.Get(service)
if err != nil {
panic(err.Error())
}
return value
}
// Set registers a service with the given key. It is an error for
// a service to already be registered to this key.
func (c *serviceContainer) Set(key string, service interface{}) error {
if _, ok := c.services[key]; ok {
return fmt.Errorf("duplicate service key `%s`", key)
}
c.services[key] = service
return nil
}
// MustSet calls Set and panics on error.
func (c *serviceContainer) MustSet(service string, value interface{}) {
if err := c.Set(service, value); err != nil {
panic(err.Error())
}
}
// Inject will attempt to populate the given type with values from
// the service container based on the value's struct tags. An error
// may occur if a service has not been registered, a service has a
// different type than expected, or struct tags are malformed.
func (c *serviceContainer) Inject(obj interface{}) error {
_, err := c.inject(obj, nil, nil)
return err
}
func (c *serviceContainer) inject(obj interface{}, root *reflect.Value, baseIndexPath []int) (bool, error) {
var (
ov = reflect.ValueOf(obj)
oi = reflect.Indirect(ov)
ot = oi.Type()
)
if root == nil {
root = &oi
}
if oi.Kind() != reflect.Struct {
return false, nil
}
hasTag := false
for i := 0; i < ot.NumField(); i++ {
indexPath := make([]int, len(baseIndexPath))
copy(indexPath, baseIndexPath)
indexPath = append(indexPath, i)
var (
fieldType = ot.Field(i)
fieldValue = (*root).FieldByIndex(indexPath)
serviceTag = fieldType.Tag.Get(serviceTag)
optionalTag = fieldType.Tag.Get(optionalTag)
)
if fieldType.Anonymous {
wasZeroValue := false
if !reflect.Indirect(fieldValue).IsValid() {
initializedValue := reflect.New(fieldType.Type.Elem())
fieldValue.Set(initializedValue)
fieldValue = initializedValue
wasZeroValue = true
}
anonymousFieldHasTag, err := c.inject(fieldValue.Interface(), root, indexPath)
if err != nil {
return false, err
}
if anonymousFieldHasTag {
hasTag = true
} else if wasZeroValue {
zeroValue := reflect.Zero(fieldType.Type)
fieldValue = (*root).FieldByIndex(indexPath)
fieldValue.Set(zeroValue)
}
continue
}
if serviceTag == "" {
continue
}
hasTag = true
if err := loadServiceField(c, fieldType, fieldValue, serviceTag, optionalTag); err != nil {
return false, err
}
}
if pi, ok := obj.(PostInject); ok {
if err := pi.PostInject(); err != nil {
return false, err
}
}
return hasTag, nil
}
func loadServiceField(container *serviceContainer, fieldType reflect.StructField, fieldValue reflect.Value, serviceTag, optionalTag string) error {
if !fieldValue.IsValid() {
return fmt.Errorf("field '%s' is invalid", fieldType.Name)
}
if !fieldValue.CanSet() {
return fmt.Errorf("field '%s' can not be set - it may be unexported", fieldType.Name)
}
value, err := container.Get(serviceTag)
if err != nil {
if optionalTag != "" {
val, err := strconv.ParseBool(optionalTag)
if err != nil {
return fmt.Errorf("field '%s' has an invalid optional tag", fieldType.Name)
}
if val {
return nil
}
}
return err
}
var (
targetType = fieldValue.Type()
targetValue = reflect.ValueOf(value)
)
if !targetValue.IsValid() || !targetValue.Type().ConvertibleTo(targetType) {
return fmt.Errorf(
"field '%s' cannot be assigned a value of type %s",
fieldType.Name,
getTypeName(value),
)
}
fieldValue.Set(targetValue.Convert(targetType))
return nil
}
func getTypeName(v interface{}) string {
if v == nil {
return "nil"
}
return reflect.TypeOf(v).String()
}