2018-08-23 08:06:25 +00:00
|
|
|
package di
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
oa "git.loafle.net/overflow/annotation-go"
|
|
|
|
"git.loafle.net/overflow/di-go/annotation"
|
|
|
|
our "git.loafle.net/overflow/util-go/reflect"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Registry interface {
|
|
|
|
RegisterType(t reflect.Type)
|
2018-09-04 06:12:06 +00:00
|
|
|
RegisterSingleton(singleton interface{}) error
|
|
|
|
RegisterSingletonByName(name string, singleton interface{}) error
|
2018-08-23 08:06:25 +00:00
|
|
|
|
|
|
|
GetInstance(t reflect.Type) (interface{}, error)
|
|
|
|
GetInstances(ts []reflect.Type) ([]interface{}, error)
|
|
|
|
GetInstanceByName(name string) (interface{}, error)
|
|
|
|
GetInstancesByAnnotationType(t reflect.Type) ([]interface{}, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(parent Registry) Registry {
|
|
|
|
r := &InstanceRegistry{
|
|
|
|
parent: parent,
|
|
|
|
definitionByType: make(map[reflect.Type]*TypeDefinition, 0),
|
|
|
|
definitionByName: make(map[string]*TypeDefinition, 0),
|
|
|
|
instanceByType: make(map[reflect.Type]interface{}, 0),
|
|
|
|
instanceByName: make(map[string]interface{}, 0),
|
|
|
|
}
|
|
|
|
if nil == r.parent {
|
|
|
|
r.parent = AppRegistry
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
var AppRegistry = &InstanceRegistry{
|
|
|
|
parent: nil,
|
|
|
|
definitionByType: make(map[reflect.Type]*TypeDefinition, 0),
|
|
|
|
definitionByName: make(map[string]*TypeDefinition, 0),
|
|
|
|
instanceByType: make(map[reflect.Type]interface{}, 0),
|
|
|
|
instanceByName: make(map[string]interface{}, 0),
|
|
|
|
}
|
|
|
|
|
|
|
|
type InstanceRegistry struct {
|
|
|
|
parent Registry
|
|
|
|
definitionByType map[reflect.Type]*TypeDefinition
|
|
|
|
definitionByName map[string]*TypeDefinition
|
|
|
|
instanceByType map[reflect.Type]interface{}
|
|
|
|
instanceByName map[string]interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func RegisterType(t reflect.Type) {
|
|
|
|
AppRegistry.RegisterType(t)
|
|
|
|
}
|
|
|
|
func (r *InstanceRegistry) RegisterType(t reflect.Type) {
|
|
|
|
if nil == t {
|
|
|
|
log.Panicf("t[reflect.Type] is nil")
|
|
|
|
}
|
|
|
|
if !our.IsTypeKind(t, reflect.Struct, true) {
|
2018-09-04 06:12:06 +00:00
|
|
|
log.Panicf("t[reflect.Type] must be Struct but is %v", t)
|
2018-08-23 08:06:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
td, err := r.buildDefinition(t)
|
|
|
|
if nil != err {
|
|
|
|
log.Panicf("DI: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := r.definitionByType[td.Type]; ok {
|
|
|
|
log.Panicf("The type[%s] of Component is exist already", td.FullName)
|
|
|
|
}
|
|
|
|
r.definitionByType[td.Type] = td
|
|
|
|
|
|
|
|
name := td.TypeName
|
|
|
|
|
|
|
|
a, err := oa.GetTypeAnnotation(td.Type, annotation.InjectableAnnotationType)
|
|
|
|
if nil != err {
|
|
|
|
log.Panicf("%v", err)
|
|
|
|
}
|
|
|
|
if nil != a {
|
|
|
|
ia := a.(*annotation.InjectableAnnotation)
|
|
|
|
if "" != strings.Trim(ia.Name, " ") {
|
|
|
|
name = ia.Name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if eTD, ok := r.definitionByName[name]; ok {
|
|
|
|
log.Panicf("The name[%s] of Component is exist already type[%s]", name, eTD.FullName)
|
|
|
|
}
|
|
|
|
r.definitionByName[name] = td
|
|
|
|
}
|
|
|
|
|
2018-09-04 06:12:06 +00:00
|
|
|
func RegisterSingleton(singleton interface{}) error {
|
|
|
|
return AppRegistry.RegisterSingleton(singleton)
|
2018-08-23 08:06:25 +00:00
|
|
|
}
|
2018-09-04 06:12:06 +00:00
|
|
|
func (r *InstanceRegistry) RegisterSingleton(singleton interface{}) error {
|
|
|
|
t := reflect.TypeOf(singleton)
|
|
|
|
|
|
|
|
if nil == t {
|
|
|
|
log.Panicf("t[reflect.Type] is nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
rt, _, tName := our.GetTypeInfo(t)
|
|
|
|
if !our.IsTypeKind(rt, reflect.Struct, true) {
|
|
|
|
log.Panicf("t[reflect.Type] must be Struct but is %v", t)
|
|
|
|
}
|
|
|
|
|
|
|
|
r.instanceByType[rt] = singleton
|
|
|
|
|
|
|
|
r.RegisterSingletonByName(tName, singleton)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func RegisterSingletonByName(name string, singleton interface{}) error {
|
|
|
|
return AppRegistry.RegisterSingletonByName(name, singleton)
|
|
|
|
}
|
|
|
|
func (r *InstanceRegistry) RegisterSingletonByName(name string, singleton interface{}) error {
|
|
|
|
t := reflect.TypeOf(singleton)
|
|
|
|
|
|
|
|
if nil == t {
|
|
|
|
log.Panicf("t[reflect.Type] is nil")
|
|
|
|
}
|
|
|
|
|
2018-08-23 08:06:25 +00:00
|
|
|
if _, ok := r.instanceByName[name]; ok {
|
2018-09-04 06:12:06 +00:00
|
|
|
return fmt.Errorf("name[%s] of Singleton is already exist", name)
|
2018-08-23 08:06:25 +00:00
|
|
|
}
|
2018-09-04 06:12:06 +00:00
|
|
|
r.instanceByName[name] = singleton
|
2018-08-23 08:06:25 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *InstanceRegistry) buildDefinition(t reflect.Type) (*TypeDefinition, error) {
|
|
|
|
if nil == t {
|
|
|
|
return nil, fmt.Errorf("t[reflect.Type] is nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
rt, pkgName, tName := our.GetTypeInfo(t)
|
|
|
|
td := &TypeDefinition{}
|
|
|
|
td.FullName = FullName(pkgName, tName)
|
|
|
|
td.PkgName = pkgName
|
|
|
|
td.TypeName = tName
|
|
|
|
td.Type = t
|
|
|
|
td.RealType = rt
|
|
|
|
|
|
|
|
return td, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetInstance returns instance of type
|
|
|
|
// t must be pointer of struct
|
|
|
|
func GetInstance(t reflect.Type) (interface{}, error) {
|
|
|
|
return AppRegistry.GetInstance(t)
|
|
|
|
}
|
|
|
|
func (r *InstanceRegistry) GetInstance(t reflect.Type) (instance interface{}, err error) {
|
|
|
|
if nil == t {
|
|
|
|
return nil, fmt.Errorf("t[reflect.Type] is nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if reflect.Ptr != t.Kind() {
|
|
|
|
return nil, fmt.Errorf("t[reflect.Type] must be pointer of struct")
|
|
|
|
}
|
|
|
|
|
|
|
|
if reflect.Struct != t.Elem().Kind() {
|
|
|
|
return nil, fmt.Errorf("t[reflect.Type] must be pointer of struct")
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2018-09-04 06:12:06 +00:00
|
|
|
td *TypeDefinition
|
|
|
|
injectableV interface{}
|
|
|
|
ok bool
|
2018-09-04 06:44:42 +00:00
|
|
|
name string
|
2018-08-23 08:06:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
rt, _, _ := our.GetTypeInfo(t)
|
|
|
|
if td, ok = r.definitionByType[t]; !ok {
|
|
|
|
if td, err = r.buildDefinition(t); nil != err {
|
|
|
|
return nil, fmt.Errorf("DI: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-04 06:12:06 +00:00
|
|
|
if injectableV, ok = r.instanceByType[td.RealType]; ok {
|
|
|
|
return injectableV, nil
|
2018-08-23 08:06:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
v := reflect.New(rt)
|
|
|
|
rv := v.Elem()
|
|
|
|
|
|
|
|
instance = v.Interface()
|
|
|
|
r.instanceByType[td.RealType] = instance
|
2018-09-04 06:12:06 +00:00
|
|
|
|
|
|
|
if a, err := oa.GetTypeAnnotation(td.Type, annotation.InjectableAnnotationType); nil == err && nil != a {
|
|
|
|
_a := a.(*annotation.InjectableAnnotation)
|
|
|
|
if "" != _a.Name {
|
2018-09-04 06:44:42 +00:00
|
|
|
name = _a.Name
|
|
|
|
r.instanceByName[name] = instance
|
2018-09-04 06:12:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-23 08:06:25 +00:00
|
|
|
err = nil
|
|
|
|
defer func() {
|
|
|
|
if nil != err {
|
|
|
|
instance = nil
|
2018-09-04 06:44:42 +00:00
|
|
|
if _, ok := r.instanceByType[td.RealType]; ok {
|
|
|
|
delete(r.instanceByType, td.RealType)
|
|
|
|
}
|
|
|
|
if _, ok := r.instanceByName[name]; ok {
|
|
|
|
delete(r.instanceByName, name)
|
|
|
|
}
|
2018-08-23 08:06:25 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
var (
|
2018-08-30 06:27:45 +00:00
|
|
|
a oa.Annotation
|
2018-08-23 08:06:25 +00:00
|
|
|
fV interface{}
|
|
|
|
)
|
|
|
|
|
|
|
|
ass, err := oa.GetAllFieldAnnotations(td.Type)
|
|
|
|
if nil != err {
|
|
|
|
return nil, fmt.Errorf("%v", err)
|
|
|
|
}
|
|
|
|
if nil != ass {
|
2018-09-04 06:12:06 +00:00
|
|
|
LOOP:
|
2018-08-23 08:06:25 +00:00
|
|
|
for n, as := range ass {
|
|
|
|
f := rv.FieldByName(n)
|
|
|
|
|
|
|
|
if !f.IsValid() {
|
|
|
|
err = fmt.Errorf("Field[%s] is not valid", n)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !f.CanSet() {
|
|
|
|
err = fmt.Errorf("Field[%s] can not set", n)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-30 06:27:45 +00:00
|
|
|
a, ok = as[annotation.InjectAnnotationType]
|
2018-08-23 08:06:25 +00:00
|
|
|
if ok {
|
2018-09-04 06:12:06 +00:00
|
|
|
_a := a.(*annotation.InjectAnnotation)
|
|
|
|
if "" == _a.Name {
|
|
|
|
if fV, err = r.GetInstance(f.Type()); nil == err {
|
|
|
|
log.Printf("%s of %s injected by type[%s]", n, td.RealType.Name(), reflect.TypeOf(fV))
|
|
|
|
f.Set(reflect.ValueOf(fV))
|
|
|
|
continue LOOP
|
|
|
|
} else {
|
|
|
|
err = fmt.Errorf("cannot find instance for %s[Type:%s]", n, f.Type())
|
|
|
|
}
|
|
|
|
|
|
|
|
if fV, err = r.GetInstanceByName(n); nil == err {
|
|
|
|
log.Printf("%s of %s injected by name[%s]", n, td.RealType.Name(), n)
|
|
|
|
f.Set(reflect.ValueOf(fV))
|
|
|
|
continue LOOP
|
|
|
|
} else {
|
|
|
|
err = fmt.Errorf("cannot find instance for %s[Name:%s]", n, n)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if fV, err = r.GetInstanceByName(_a.Name); nil == err {
|
|
|
|
log.Printf("%s of %s injected by name[%s]", n, td.RealType.Name(), _a.Name)
|
|
|
|
f.Set(reflect.ValueOf(fV))
|
|
|
|
continue LOOP
|
|
|
|
} else {
|
|
|
|
err = fmt.Errorf("cannot find instance for %s[Name:%s]", n, _a.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if fV, err = r.GetInstance(f.Type()); nil == err {
|
|
|
|
log.Printf("%s of %s injected by type[%s]", n, td.RealType.Name(), reflect.TypeOf(fV))
|
|
|
|
f.Set(reflect.ValueOf(fV))
|
|
|
|
continue LOOP
|
|
|
|
} else {
|
|
|
|
err = fmt.Errorf("cannot find instance for %s[Type:%s]", n, f.Type())
|
|
|
|
}
|
2018-08-23 08:06:25 +00:00
|
|
|
}
|
|
|
|
|
2018-09-04 06:12:06 +00:00
|
|
|
if nil != err {
|
|
|
|
if _a.Required {
|
|
|
|
return
|
|
|
|
}
|
2018-08-30 06:27:45 +00:00
|
|
|
}
|
2018-08-23 08:06:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-04 06:44:42 +00:00
|
|
|
if as, err := oa.GetMethodAnnotationsByType(td.Type, annotation.PostConstructAnnotationType); nil == err && nil != as {
|
|
|
|
for k := range as {
|
|
|
|
ins := make([]reflect.Value, 0)
|
|
|
|
reflect.ValueOf(instance).MethodByName(k).Call(ins)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-23 08:06:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetInstanceByName(name string) (interface{}, error) {
|
|
|
|
return AppRegistry.GetInstanceByName(name)
|
|
|
|
}
|
|
|
|
func (r *InstanceRegistry) GetInstanceByName(name string) (interface{}, error) {
|
2018-09-04 06:12:06 +00:00
|
|
|
if td, ok := r.definitionByName[name]; ok {
|
|
|
|
v, err := r.GetInstance(td.Type)
|
|
|
|
if nil == err {
|
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-23 08:06:25 +00:00
|
|
|
v, ok := r.instanceByName[name]
|
|
|
|
if ok {
|
|
|
|
return v, nil
|
|
|
|
}
|
2018-09-04 06:12:06 +00:00
|
|
|
return nil, fmt.Errorf("Instance[%s] is not exist", name)
|
2018-08-23 08:06:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetInstances returns instance of annotated
|
|
|
|
// n must be name of registered annotation
|
|
|
|
func GetInstances(ts []reflect.Type) ([]interface{}, error) {
|
|
|
|
return AppRegistry.GetInstances(ts)
|
|
|
|
}
|
|
|
|
func (r *InstanceRegistry) GetInstances(ts []reflect.Type) ([]interface{}, error) {
|
|
|
|
var (
|
|
|
|
i interface{}
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
instances := make([]interface{}, 0)
|
|
|
|
for _, t := range ts {
|
|
|
|
if i, err = r.GetInstance(t); nil != err {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
instances = append(instances, i)
|
|
|
|
}
|
|
|
|
|
|
|
|
return instances, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetInstancesByAnnotationType(at reflect.Type) ([]interface{}, error) {
|
|
|
|
return AppRegistry.GetInstancesByAnnotationType(at)
|
|
|
|
}
|
|
|
|
func (r *InstanceRegistry) GetInstancesByAnnotationType(at reflect.Type) ([]interface{}, error) {
|
|
|
|
var (
|
|
|
|
a oa.Annotation
|
|
|
|
i interface{}
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
instances := make([]interface{}, 0)
|
|
|
|
|
|
|
|
for _, td := range r.definitionByType {
|
|
|
|
a, err = oa.GetTypeAnnotation(td.Type, at)
|
|
|
|
if nil != err {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if nil != a {
|
|
|
|
i, err = r.GetInstance(td.Type)
|
|
|
|
if nil != err {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
instances = append(instances, i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return instances, nil
|
|
|
|
}
|