-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathinject.go
664 lines (587 loc) · 14.9 KB
/
inject.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
/**
by liyibo 2016
1.if singleton, every struct ptr inject object can only have one
instance in memory, we set twice in named to
ensure that(first by name, then by struct ptr type)
2.TODO every struct inject object(not a ptr) should be handled like:
create new struct on every inject
`inject:""` by type,must be a pointer to a struct
`inject:"devService"`
3.singleton(default false)
`singleton:"true"`
`singleton:"false"`
4.cannil(default false)
`cannil:"true"`
`cannil:"false"`
**/
package inji
import (
"bytes"
"encoding/json"
"fmt"
"os"
"reflect"
"strings"
"sync"
"time"
"github.com/facebookgo/structtag"
"github.com/teou/implmap"
"github.com/teou/ordered_map"
)
type Logger interface {
IsDebugEnabled() bool
Debug(format interface{}, v ...interface{})
Info(format interface{}, v ...interface{})
Error(format interface{}, v ...interface{}) error
}
type Object struct {
Name string
reflectType reflect.Type
Value interface{}
closed bool
}
func (o Object) String() string {
if o.reflectType.Kind() == reflect.Ptr {
return fmt.Sprintf(`{"name":"%s","type":"%v","value":"%p"}`, o.Name, o.reflectType, o.Value)
} else {
return fmt.Sprintf(`{"name":"%s","type":"%v"}`, o.Name, o.reflectType)
}
}
type Graph struct {
l sync.RWMutex
Logger Logger
named *ordered_map.OrderedMap
}
func NewGraph() *Graph {
g := &Graph{
named: ordered_map.NewOrderedMap(),
}
return g
}
func getTypeName(t reflect.Type) string {
isptr := false
if t.Kind() == reflect.Ptr {
t = t.Elem()
isptr = true
}
pkg := t.PkgPath()
name := ""
if pkg != "" {
name = pkg + "." + t.Name()
} else {
name = t.Name()
}
if isptr {
name = "*" + name
}
return name
}
func (g *Graph) FindByType(t reflect.Type) (*Object, bool) {
g.l.RLock()
defer g.l.RUnlock()
return g.findByType(t)
}
func (g *Graph) findByType(t reflect.Type) (*Object, bool) {
n := getTypeName(t)
return g.find(n)
}
func (g *Graph) Len() int {
g.l.RLock()
defer g.l.RUnlock()
return g._len()
}
func (g *Graph) _len() int {
return g.named.Len()
}
func (g *Graph) Find(name string) (*Object, bool) {
g.l.RLock()
defer g.l.RUnlock()
return g.find(name)
}
func (g *Graph) find(name string) (*Object, bool) {
f, ok := g.named.Get(name)
if !ok {
return nil, false
}
ret, ok := f.(*Object)
if !ok {
//g.named.Delete(name)
panic(fmt.Sprintf("%s in graph is not a *Object, should not happen!", name))
return nil, false
} else {
return ret, true
}
}
func (g *Graph) del(name string) {
g.named.Delete(name)
}
func (g *Graph) set(name string, o *Object) {
g.named.Set(name, o)
}
func (g *Graph) setboth(name string, o *Object) {
g.named.Set(name, o)
if isStructPtr(o.reflectType) {
tn := getTypeName(o.reflectType)
g.named.Set(tn, o)
}
}
func (g *Graph) RegWithoutInjection(name string, value interface{}) interface{} {
return g.RegisterOrFailNoFill(name, value)
}
func (g *Graph) RegisterOrFailNoFill(name string, value interface{}) interface{} {
v, err := g.RegisterNoFill(name, value)
if err != nil {
if g.Logger != nil {
g.Logger.Error(err)
}
panic(fmt.Sprintf("reg fail,name=%v,err=%v", name, err.Error()))
}
return v
}
func (g *Graph) RegisterOrFailSingleNoFill(name string, value interface{}) interface{} {
v, err := g.RegisterSingleNoFill(name, value)
if err != nil {
if g.Logger != nil {
g.Logger.Error(err)
}
panic(fmt.Sprintf("reg fail,name=%v,err=%v", name, err.Error()))
}
return v
}
func (g *Graph) RegisterOrFail(name string, value interface{}) interface{} {
v, err := g.Register(name, value)
if err != nil {
if g.Logger != nil {
g.Logger.Error(err)
}
panic(fmt.Sprintf("reg fail,name=%v,err=%v", name, err.Error()))
}
return v
}
func (g *Graph) RegisterOrFailSingle(name string, value interface{}) interface{} {
v, err := g.RegisterSingle(name, value)
if err != nil {
if g.Logger != nil {
g.Logger.Error(err)
}
panic(fmt.Sprintf("reg fail,name=%v,err=%v", name, err.Error()))
}
return v
}
func (g *Graph) RegisterNoFill(name string, value interface{}) (interface{}, error) {
g.l.Lock()
defer g.l.Unlock()
return g.register(name, value, false, true)
}
func (g *Graph) RegisterSingleNoFill(name string, value interface{}) (interface{}, error) {
g.l.Lock()
defer g.l.Unlock()
return g.register(name, value, true, true)
}
func (g *Graph) Register(name string, value interface{}) (interface{}, error) {
g.l.Lock()
defer g.l.Unlock()
return g.register(name, value, false, false)
}
func (g *Graph) RegisterSingle(name string, value interface{}) (interface{}, error) {
g.l.Lock()
defer g.l.Unlock()
return g.register(name, value, true, false)
}
func (g *Graph) register(name string, value interface{}, singleton bool, noFill bool) (interface{}, error) {
reflectType := reflect.TypeOf(value)
if isStructPtr(reflectType) {
if name == "" {
name = getTypeName(reflectType)
}
} else {
if name == "" {
return nil, fmt.Errorf("name can not be empty,name=%s,type=%v", name, reflectType)
}
}
//already registered
found, ok := g.find(name)
if ok {
return nil, fmt.Errorf("already registered,name=%s,type=%v,found=%v", name, reflectType, found)
}
o := &Object{
Name: name,
reflectType: reflectType,
}
if isStructPtr(o.reflectType) {
t := reflectType.Elem()
var v reflect.Value
created := false
if isNil(value) {
created = true
v = reflect.New(t)
} else {
v = reflect.ValueOf(value)
}
for i := 0; i < t.NumField(); i++ {
if !created && noFill {
continue
}
f := t.Field(i)
vfe := v.Elem()
vf := vfe.Field(i)
ok, tag, err := structtag.Extract("inject", string(f.Tag))
if err != nil {
return nil, fmt.Errorf("extract tag fail,f=%s,err=%v", f.Name, err)
}
if !ok {
continue
}
if vf.CanInterface() {
if !isZeroOfUnderlyingType(vf.Interface()) {
continue
}
}
if f.Anonymous || !vf.CanSet() {
return nil, fmt.Errorf("inject tag must on a public field!field=%s,type=%s", f.Name, t.Name())
// continue // useless code
}
_, singletonStr, _ := structtag.Extract("singleton", string(f.Tag))
singletonTag := false
if singletonStr == "true" {
singletonTag = true
}
_, canNilStr, _ := structtag.Extract("cannil", string(f.Tag))
_, nilableStr, _ := structtag.Extract("nilable", string(f.Tag))
canNil := false
if canNilStr == "true" || nilableStr == "true" {
canNil = true
}
var found *Object
if tag != "" {
//due to default singleton of struct ptr injections
//we should first find by name,then find by type
found, ok = g.find(tag)
if singletonTag && !ok && isStructPtr(f.Type) {
found, ok = g.findByType(f.Type)
}
} else {
found, ok = g.findByType(f.Type)
}
if !ok || found == nil {
if canNil {
continue
}
if isStructPtr(f.Type) {
_, err := g.register(tag, reflect.NewAt(f.Type.Elem(), nil).Interface(), singletonTag, noFill)
if err != nil {
return nil, err
}
} else {
var implFound reflect.Type
impls := implmap.Get(tag)
for _, impl := range impls {
if impl == nil {
continue
}
if impl.AssignableTo(f.Type) {
implFound = impl
break
}
}
if implFound != nil {
_, err := g.register(tag, reflect.NewAt(implFound.Elem(), nil).Interface(), singletonTag, noFill)
if err != nil {
return nil, err
}
} else {
return nil, fmt.Errorf("dependency field=%s,tag=%s not found in object %s:%v", f.Name, tag, name, reflectType)
}
}
if tag != "" {
found, ok = g.find(tag)
if !ok && singleton {
found, ok = g.findByType(f.Type)
}
} else {
found, ok = g.findByType(f.Type)
}
}
if !ok || found == nil {
return nil, fmt.Errorf("dependency %s not found in object %s:%v", f.Name, name, reflectType)
}
reflectFoundValue := reflect.ValueOf(found.Value)
if !found.reflectType.AssignableTo(f.Type) {
switch reflectFoundValue.Kind() {
case reflect.Int:
fallthrough
case reflect.Int8:
fallthrough
case reflect.Int16:
fallthrough
case reflect.Int32:
fallthrough
case reflect.Int64:
iv := reflectFoundValue.Int()
switch f.Type.Kind() {
case reflect.Int:
fallthrough
case reflect.Int8:
fallthrough
case reflect.Int16:
fallthrough
case reflect.Int32:
fallthrough
case reflect.Int64:
vf.SetInt(iv)
default:
return nil, fmt.Errorf("dependency name=%s,type=%v not valid in object %s:%v", f.Name, f.Type, name, reflectType)
}
case reflect.Float32:
fallthrough
case reflect.Float64:
fv := reflectFoundValue.Float()
switch f.Type.Kind() {
case reflect.Float32:
fallthrough
case reflect.Float64:
vf.SetFloat(fv)
default:
return nil, fmt.Errorf("dependency name=%s,type=%v not valid in object %s:%v", f.Name, f.Type, name, reflectType)
}
default:
return nil, fmt.Errorf("dependency name=%s,type=%v not valid in object %s:%v", f.Name, f.Type, name, reflectType)
}
} else {
vf.Set(reflectFoundValue)
}
}
o.Value = v.Interface()
} else {
//TODO
//if inejection type is a struct(not a pointer),
//we should create a new struct every time
//when a inject tag is found,and no *Object is created and
//the created struct should NOT be set into
//graph.named(or there will be a memory leak)!
//same as a bean's prototype scope in spring.
//otherwise all inject dependency will behave like
//spring's singleton bean scope
if canNil(value) && isNil(value) {
return nil, fmt.Errorf("register nil on name=%s, val=%v", name, value)
}
o.Value = value
}
//depedency resolved, init the object
canStart, ok := o.Value.(Startable)
if ok {
st := time.Now()
err := canStart.Start()
cost := time.Now().Sub(st)
if cost > 5*time.Second {
errMsg := fmt.Sprintf("obj start took too long,name=%v,time=%v,err=%v", name, cost, err)
fmt.Fprint(os.Stderr, errMsg+"\n")
if g.Logger != nil {
g.Logger.Error(errMsg)
}
}
if err != nil {
return nil, fmt.Errorf("Start object fail,name=%v,err=%v", name, err)
}
}
//set to graph
if isStructPtr(reflectType) && singleton {
g.setboth(name, o)
} else {
g.set(name, o)
}
if g.Logger != nil && g.Logger.IsDebugEnabled() {
toLogJson, toLogErr := json.Marshal(o.Value)
g.Logger.Debug("registered!name=%s,t=%v,v=%v,jsonerr=%v", name, reflectType, string(toLogJson), toLogErr)
}
return o.Value, nil
}
func (g *Graph) SPrint() string {
g.l.RLock()
defer g.l.RUnlock()
return g.sPrint()
}
func (g *Graph) sPrint() string {
ret := "["
iter := g.named.IterFunc()
count := g._len()
i := 0
for kv, ok := iter(); ok; kv, ok = iter() {
str := fmt.Sprintf(`{"key":"%s","object":%s}`, fmt.Sprintf("%s", kv.Key), fmt.Sprintf("%s", kv.Value))
ret = ret + str
i++
if i < count {
ret = ret + ","
}
}
ret = ret + "]"
return ret
}
func (g *Graph) SPrintTree() string {
g.l.RLock()
defer g.l.RUnlock()
buf := bytes.NewBufferString("dependence tree:\n")
len := g.named.Len()
i := 0
iter := g.named.IterFunc()
for kv, ok := iter(); ok; kv, ok = iter() {
head := "├── "
if i == 0 {
head = "┌── "
} else if i == len-1 {
head = "└── "
}
i++
g.sPrintTree(head, kv.Value.(*Object), buf)
}
return buf.String()
}
func (g *Graph) sPrintTree(path string, o *Object, buf *bytes.Buffer) error {
value := ""
if o.reflectType.Kind() == reflect.Ptr {
value = fmt.Sprintf("%p", o.Value)
} else {
value = fmt.Sprintf("%v", o.Value)
}
show := fmt.Sprintf("%s%s(%v=%v)\n", path, o.Name, o.reflectType, value)
buf.WriteString(show)
if isStructPtr(o.reflectType) {
childPath := path
childPath = strings.Replace(childPath, "└", " ", -1)
childPath = strings.Replace(childPath, "├", "│", -1)
childPath = strings.Replace(childPath, "─", " ", -1)
childPath = strings.Replace(childPath, "┌", "│", -1)
t := o.reflectType.Elem()
// load tags of injected child
var tags []string
for i := 0; i < t.NumField(); i++ {
structFiled := t.Field(i)
ok, tag, err := structtag.Extract("inject", string(structFiled.Tag))
if err != nil {
return fmt.Errorf("extract tag fail,f=%s,err=%v", structFiled.Name, err)
}
if !ok {
continue
}
if len(tag) == 0 {
tag = getTypeName(structFiled.Type)
}
_, ok = g.find(tag)
if ok {
tags = append(tags, tag)
}
}
for i, tag := range tags {
corner := ""
if i == len(tags)-1 {
corner = childPath + " └── "
} else {
corner = childPath + " ├── "
}
childObject, _ := g.find(tag)
g.sPrintTree(corner, childObject, buf)
}
}
return nil
}
//beaware of the close order when use g.Close!
//every *Object will be Closed on reverse order
//of the Register
//there should be no defer xx.Close betwen g.Register
//function calls in main.exe
func (g *Graph) Close() {
g.l.Lock()
defer g.l.Unlock()
if g.Logger != nil {
g.Logger.Info("close objects %v", g.sPrint())
}
var keys []string
iter := g.named.RevIterFunc()
for kv, ok := iter(); ok; kv, ok = iter() {
k, ok := kv.Key.(string)
if !ok {
continue
}
keys = append(keys, k)
o, ok := kv.Value.(*Object)
if !ok {
continue
}
if o.closed {
continue
}
if isStructPtr(o.reflectType) {
keys = append(keys, getTypeName(o.reflectType))
}
if o.Value == nil {
continue
}
c, ok := o.Value.(Closeable)
if ok {
c.Close()
if g.Logger != nil {
g.Logger.Debug("closed!object=%s", o)
}
o.closed = true
}
}
for _, k := range keys {
g.del(k)
}
if g.Logger != nil {
g.Logger.Info("inject graph closed all")
}
}
func isStructPtr(t reflect.Type) bool {
return t.Kind() == reflect.Ptr && t.Elem().Kind() == reflect.Struct
}
func canNil(v interface{}) bool {
k := reflect.ValueOf(v).Kind()
return k == reflect.Ptr || k == reflect.Interface
}
func isNil(v interface{}) bool {
return reflect.ValueOf(v).IsNil()
}
func isZeroOfUnderlyingType(x interface{}) bool {
if x == nil {
return true
}
rv := reflect.ValueOf(x)
k := rv.Kind()
if k == reflect.Struct {
return reflect.DeepEqual(reflect.New(reflect.TypeOf(x)).Elem().Interface(), x)
}
if k == reflect.Func {
return rv.IsNil()
}
if (k == reflect.Ptr || k == reflect.Interface || k == reflect.Chan || k == reflect.Map || k == reflect.Slice) && rv.IsNil() {
return true
}
switch k {
case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice, reflect.String:
if rv.Len() <= 0 {
return true
} else {
return false
}
}
return x == reflect.Zero(reflect.TypeOf(x)).Interface()
}
func ReflectRegFields(v interface{}) map[string]interface{} {
ret := make(map[string]interface{})
vfe := reflect.ValueOf(v).Elem()
t := vfe.Type()
if t.Kind() != reflect.Struct {
panic(fmt.Sprintf("value must be a struct pointer %v %v", v, t))
}
for i := 0; i < t.NumField(); i++ {
f := t.Field(i)
vf := vfe.Field(i)
name := f.Name
name = strings.ToLower(string(name[0])) + name[1:]
vi := vf.Interface()
Reg(name, vi)
ret[name] = vi
}
return ret
}