/
injector.go
165 lines (147 loc) · 3.56 KB
/
injector.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
package gioc
import (
"reflect"
"sync"
)
type injector struct {
parent Injector
providers map[string]provider
sync.Mutex
}
func NewInjector() Injector {
i := &injector{}
i.providers = make(map[string]provider, 64)
i.Map(new(Injector), "").ToValue(i)
return i
}
func (this *injector) Map(ptr interface{}, name string) Mapping {
typ := reflect.TypeOf(ptr)
return this.MapByType(typ, name)
}
func (this *injector) CreateChild() Injector {
i := NewInjector()
i.SetParent(this)
return i
}
func (this *injector) UnMap(ptr interface{}, name string) {
typ := reflect.TypeOf(ptr)
this.UnMap(typ, name)
}
func (this *injector) InjectInto(p interface{}) {
val := reflect.ValueOf(p)
this.injectInto(val)
}
func (this *injector) GetInstance(ptr interface{}, name string) interface{} {
typ := reflect.TypeOf(ptr)
for typ.Kind() == reflect.Ptr {
typ = typ.Elem()
}
return this.GetInstanceByType(typ, name).Interface()
}
func (this*injector) GetParent() Injector {
return this.parent
}
func (this*injector) SetParent(i Injector) {
this.parent = i
}
func (this*injector) HasMapping(ptr interface{}, name string, deeply bool) (b bool) {
return this.HasMappingOfType(reflect.TypeOf(ptr), name, deeply)
}
func (this *injector)InstantiationUnMapped(typ reflect.Type)reflect.Value{
v:=reflect.New(typ)
this.InjectInto(v)
return v
}
/*************************************************/
/**private methods*/
func (this *injector) createMapping(typ reflect.Type, name, key string) Mapping {
if _, ok := this.providers[key]; ok {
panic("this mapping is already exists!")
}
mp := newMapping(this, typ, name, key)
return mp
}
func (this *injector) getProvider(typ reflect.Type, name string) provider {
i := this
for i != nil {
if p, ok := i.providers[generateUid(typ, name)]; ok {
return p
}
if this.parent == nil {
i = nil
}else {
i = this.parent.(*injector)
}
}
return nil
}
func (this *injector) GetInstanceByType(typ reflect.Type, name string) reflect.Value {
p := this.getProvider(typ, name)
if p != nil {
return p.apply(this)
}
panic("can't find mapping!")
}
func (this *injector) injectInto(val reflect.Value) {
td := getTypeDescribe(val.Type())
for val.Kind() == reflect.Ptr {
val = val.Elem()
}
for _, v := range td.fields {
vField := val.FieldByIndex(v.Index)
if vField.CanSet() {
vField.Set(this.GetInstanceByType(v.Type, v.Tag.Get("inject")))
}
}
if td.hasInitMethod{
td.initMethod.Func.Call([]reflect.Value{val.Addr()})
}
}
func (this *injector) UnMapByType(typ reflect.Type, name string) {
if this.HasMappingOfType(typ, name, false) {
this.Lock()
delete(this.providers, generateUid(typ, name))
this.Unlock()
}
}
func (this*injector) HasMappingOfType(typ reflect.Type, name string, deeply bool) (b bool) {
i := this
for typ.Kind() == reflect.Ptr {
typ = typ.Elem()
}
b = false
for i != nil {
_, b = i.providers[generateUid(typ, name)]
if deeply {
break
}
if this.parent == nil {
i = nil
}else {
i = this.parent.(*injector)
}
}
return
}
func (this *injector) MapByType(typ reflect.Type, name string) Mapping {
for typ.Kind() == reflect.Ptr {
typ = typ.Elem()
}
mappingKey := generateUid(typ, name)
return this.createMapping(typ, name, mappingKey)
}
func (this *injector) addProvider(uid string, p provider) {
this.Lock()
if _, ok := this.providers[uid]; !ok {
this.providers[uid] = p
}
this.Unlock()
}
/****************************************************/
func generateUid(typ reflect.Type, name string) string {
if len(name) > 0 {
return typ.String() + ":" + name
}else {
return typ.String() + ":-"
}
}