-
Notifications
You must be signed in to change notification settings - Fork 4
/
container.go
129 lines (110 loc) · 3.45 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
// Package alice provides an additive dependency injection container.
package alice
import (
"fmt"
"reflect"
)
// CreateContainer creates a new instance of container with specified modules. It panics if any of the module is
// invalid. This is the only way to create a container. Most applications call it only once during bootstrap.
func CreateContainer(modules ...Module) Container {
c := &container{
modules: modules,
}
c.populate()
return c
}
// Container defines the interface of an instance container. It initializes instances based on dependencies,
// and provides APIs to retrieve instances by type or name.
type Container interface {
// Instance returns an instance by type. It panics when no instance is found,
// or multiple instances are found for the same type.
Instance(t reflect.Type) interface{}
// InstanceByName returns an instance by name. It panics when no instance is found.
InstanceByName(name string) interface{}
}
// container is an implementation of Container interface. It is not thread-safe.
type container struct {
modules []Module
instanceByName map[string]interface{}
instanceByType map[reflect.Type][]interface{}
}
func (c *container) Instance(t reflect.Type) interface{} {
return c.findInstanceByType(t)
}
func (c *container) InstanceByName(name string) interface{} {
return c.findInstanceByName(name)
}
func (c *container) populate() {
rms := c.reflectModules(c.modules)
g, err := createGraph(rms...)
if err != nil {
panic(err)
}
orderedRms, err := g.instantiationOrder()
if err != nil {
panic(err)
}
c.instanceByName = make(map[string]interface{})
c.instanceByType = make(map[reflect.Type][]interface{})
for _, rm := range orderedRms {
c.instantiateModule(rm)
}
}
func (c *container) instantiateModule(rm *reflectedModule) {
for _, dep := range rm.namedDepends {
instance := c.findInstanceByName(dep.name)
dep.field.Set(reflect.ValueOf(instance))
}
for _, dep := range rm.typedDepends {
instance := c.findInstanceByType(dep.tp)
dep.field.Set(reflect.ValueOf(instance))
}
for _, instanceMethod := range rm.instances {
instance := instanceMethod.method.Call(nil)[0].Interface()
c.instanceByName[instanceMethod.name] = instance
typedInstances, _ := c.instanceByType[instanceMethod.tp]
typedInstances = append(typedInstances, instance)
c.instanceByType[instanceMethod.tp] = typedInstances
}
}
func (c *container) findInstanceByType(t reflect.Type) interface{} {
instances, ok := c.instanceByType[t]
if !ok {
instances = c.findAssignableInstances(t)
}
if len(instances) == 0 {
panic(fmt.Sprintf("instance type %s is not defined", t.Name()))
}
if len(instances) > 1 {
panic(fmt.Sprintf("instance type %s has more than one instances defined", t.Name()))
}
return instances[0]
}
func (c *container) findInstanceByName(name string) interface{} {
instance, ok := c.instanceByName[name]
if !ok {
panic(fmt.Sprintf("instance name %s is not defined", name))
}
return instance
}
func (c *container) findAssignableInstances(t reflect.Type) []interface{} {
var instances []interface{}
for _, instance := range c.instanceByName {
instanceType := reflect.TypeOf(instance)
if instanceType.AssignableTo(t) {
instances = append(instances, instance)
}
}
return instances
}
func (c *container) reflectModules(modules []Module) []*reflectedModule {
var rms []*reflectedModule
for _, m := range c.modules {
rm, err := reflectModule(m)
if err != nil {
panic(err)
}
rms = append(rms, rm)
}
return rms
}