-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontainer.go
206 lines (182 loc) · 5.8 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
package inject
import (
"container/list"
"fmt"
"reflect"
"sync"
"github.com/binacsgo/graph"
)
// BeforeVisitor before visitor
type BeforeVisitor interface {
BeforeInject()
}
// AfterVisitor after visitor
type AfterVisitor interface {
AfterInject() error
}
// Properties todo
type Properties interface {
GetPropertiesValue(key string) string
}
// ObjFactory todo
type ObjFactory interface {
CreateObj() interface{}
}
// Container container of objects
type Container struct {
nameObjMap map[string]*ObjInfo
orderObjMap map[int64]*ObjInfo
definationMap map[reflect.Type]*ObjDefination // not used yet
fieldMatchStrategyMap FieldMatchStrategyMap
registList *list.List
graph *graph.Graph
order int64
conns int64
mutex sync.Mutex
}
// NewContainer return a container that store all the objects
func NewContainer() *Container {
return &Container{
nameObjMap: make(map[string]*ObjInfo, 16),
orderObjMap: make(map[int64]*ObjInfo, 16),
definationMap: make(map[reflect.Type]*ObjDefination, 16), // not used yet
fieldMatchStrategyMap: strategyMap,
registList: list.New(),
}
}
// Regist regist to container
func (ic *Container) Regist(injectName string, obj interface{}) {
if len(injectName) <= 0 || obj == nil {
panic(fmt.Errorf("Invalid param: obj=[%v]", obj))
}
if err := canRegisterCheck(obj); err != nil {
panic(fmt.Errorf("Can not pass the canRegisterCheck: err=[%v]", err))
}
if err := ic.regist(injectName, obj); err != nil {
panic(fmt.Errorf("Regist failed for [%v]: err=[%v]", injectName, err))
}
}
// DoInject inject to container
func (ic *Container) DoInject() {
if err := ic.inject(); err != nil {
panic(fmt.Errorf("DoInject failed: err=[%v]", err))
}
}
// Report report the container
func (ic *Container) Report() string {
return ""
}
func (ic *Container) regist(injectName string, obj interface{}) error {
ic.mutex.Lock()
defer ic.mutex.Unlock()
// 1. Check for duplicate registrations
if _, ok := ic.nameObjMap[injectName]; ok {
return fmt.Errorf("Already exists in nameObjMap")
}
// 2. Build the ObjInfo
newObj, err := newObjInfo(injectName, ic.order, obj)
if err != nil {
return fmt.Errorf("Can not pass the newObjInfo: err=[%v]", err)
}
// 3. Update the container
{
ic.registList.PushBack(newObj)
ic.nameObjMap[injectName] = newObj
ic.orderObjMap[ic.order] = newObj
// [0, order)
ic.order += 1
ic.conns += int64(len(newObj.objDefination.injectList))
}
return nil
}
func (ic *Container) inject() error {
ic.mutex.Lock()
defer ic.mutex.Unlock()
ic.graph = graph.NewGraph(ic.order, ic.conns)
for elem := ic.registList.Front(); elem != nil; elem = elem.Next() {
objInfo := elem.Value.(*ObjInfo)
objInfoName := objInfo.injectName
for i := range objInfo.objDefination.injectList {
depObjInfoName := objInfo.objDefination.injectList[i].tag.injectName
ic.graph.AddEdge(ic.nameObjMap[objInfoName].order, ic.nameObjMap[depObjInfoName].order, 1)
}
}
topo, ok := graph.Topology(ic.graph)
if !ok {
return fmt.Errorf("Find circle in graph")
}
pedding := make([]*ObjInfo, 0)
for i := len(topo) - 1; i >= 0; i-- {
order := topo[i]
objInfo := ic.orderObjMap[order]
ic.invokeBeforeInject(objInfo)
if err := ic.injectFields(objInfo); err != nil {
return fmt.Errorf("Inject objInfo.injectName[%v] got err=[%v]", objInfo.injectName, err)
}
pedding = append(pedding, objInfo)
}
return ic.invokeAfterInject(pedding)
}
func (ic *Container) invokeBeforeInject(obj *ObjInfo) {
if visitor, ok := obj.instance.(BeforeVisitor); ok {
visitor.BeforeInject()
}
}
func (ic *Container) invokeAfterInject(objList []*ObjInfo) error {
for _, obj := range objList {
if visitor, ok := obj.instance.(AfterVisitor); ok {
err := visitor.AfterInject()
if err != nil {
return err
}
}
obj.injectComplete = true
}
return nil
}
func (ic *Container) injectFields(objInfo *ObjInfo) error {
if objInfo.instance == nil {
return nil
}
injectListLen := len(objInfo.objDefination.injectList)
for i := 0; i < injectListLen; i++ {
injectObj, err := ic.findObjByTFieldInfo(objInfo.objDefination.injectList[i])
if err != nil {
return err
}
if injectObj != nil {
fmt.Printf("Object [%v] Inject field[%v] -> [%v]\n", objInfo.injectName, objInfo.objDefination.injectList[i].fieldName, injectObj.injectName)
doInject(objInfo.objDefination.injectList[i], injectObj)
} else {
if objInfo.objDefination.injectList[i].tag.required {
return fmt.Errorf("Object [%v] Inject fail field[%v] -> not found", objInfo.injectName, objInfo.objDefination.injectList[i].fieldName)
}
fmt.Printf("Object [%v] Inject fail field[%v] -> not found (not required)\n", objInfo.injectName, objInfo.objDefination.injectList[i].fieldName)
}
}
return nil
}
func (ic *Container) findObjByTFieldInfo(fieldinfo *InjectFieldInfo) (*ObjInfo, error) {
depObj, err := ic.fieldMatchStrategyMap.getStrategy(fieldinfo.tag.strategy).findObjByTFieldInfo(ic.nameObjMap, fieldinfo)
if err != nil {
return nil, err
}
if depObj == nil {
// DO NOT return nil, nil
return nil, fmt.Errorf("depObj [%v]=nil, this should not happen if we perform injection by topology", fieldinfo.tag.injectName)
}
if !canInjectCheck(fieldinfo.reflectType, depObj.objDefination.reflectType) {
return nil, fmt.Errorf("canInjectCheck fail [%v] can not inject to [%v]", depObj.objDefination.reflectType, fieldinfo.reflectType)
}
return depObj, nil
}
func doInject(fieldinfo *InjectFieldInfo, objInfo *ObjInfo) {
fmt.Printf("doInject: fieldName=[%v] CanSet=[%v] Kind=[%v] AssignableTo=[%v]\n",
fieldinfo.fieldName,
fieldinfo.reflectValue.CanSet(),
fieldinfo.reflectValue.Kind(),
objInfo.objDefination.reflectType.AssignableTo(fieldinfo.reflectType),
)
fieldinfo.reflectValue.Set(objInfo.objDefination.reflectValue)
fieldinfo.objInfo = objInfo
}