di/registry/registry.go

266 lines
6.3 KiB
Go
Raw Normal View History

2017-12-03 10:55:10 +00:00
package registry
2017-12-03 13:54:09 +00:00
import (
"fmt"
2017-12-05 13:28:42 +00:00
"log"
2017-12-03 13:54:09 +00:00
"reflect"
2017-12-06 06:07:44 +00:00
"runtime"
"strings"
2017-12-03 13:54:09 +00:00
2017-12-05 10:02:41 +00:00
cda "git.loafle.net/commons_go/di/annotation"
2018-03-15 13:19:44 +00:00
cdia "git.loafle.net/commons_go/di/injection/annotation"
2017-12-03 13:54:09 +00:00
cdur "git.loafle.net/commons_go/di/util/reflect"
2017-12-06 00:48:03 +00:00
"git.loafle.net/commons_go/logging"
2017-12-03 13:54:09 +00:00
)
2017-12-03 10:55:10 +00:00
2017-12-05 13:28:42 +00:00
func init() {
registry = newRegistry()
}
var registry ComponentRegistry
2017-12-03 10:55:10 +00:00
type ComponentRegistry interface {
2018-03-15 13:19:44 +00:00
RegisterType(t reflect.Type)
2017-12-06 06:30:49 +00:00
RegisterResource(name string, resource interface{}) error
2017-12-03 10:55:10 +00:00
2018-03-15 07:32:04 +00:00
GetInstance(t reflect.Type) interface{}
GetInstanceByName(name string) interface{}
2017-12-03 10:55:10 +00:00
}
2017-12-03 13:54:09 +00:00
2017-12-05 13:28:42 +00:00
func newRegistry() ComponentRegistry {
r := &defaultComponentRegistry{}
2017-12-06 00:48:03 +00:00
r.definitionByType = make(map[reflect.Type]*TypeDefinition, 0)
2017-12-05 13:28:42 +00:00
r.definitionByName = make(map[string]*TypeDefinition, 0)
2017-12-08 10:32:59 +00:00
r.instanceByType = make(map[reflect.Type]interface{}, 0)
2017-12-06 06:30:49 +00:00
r.resourceByName = make(map[string]interface{}, 0)
2017-12-05 13:28:42 +00:00
return r
}
2017-12-03 13:54:09 +00:00
type defaultComponentRegistry struct {
2017-12-06 00:48:03 +00:00
definitionByType map[reflect.Type]*TypeDefinition
2017-12-05 13:28:42 +00:00
definitionByName map[string]*TypeDefinition
2017-12-08 10:32:59 +00:00
instanceByType map[reflect.Type]interface{}
2017-12-06 06:30:49 +00:00
resourceByName map[string]interface{}
2017-12-03 13:54:09 +00:00
}
2018-03-15 13:19:44 +00:00
func RegisterType(t reflect.Type) {
2017-12-06 06:07:44 +00:00
pc, _, _, ok := runtime.Caller(1)
details := runtime.FuncForPC(pc)
if ok && details != nil {
log.Printf("called from %s\n", details.Name())
}
2018-03-15 13:19:44 +00:00
registry.RegisterType(t)
2017-12-05 13:28:42 +00:00
}
2018-03-15 13:19:44 +00:00
func (cr *defaultComponentRegistry) RegisterType(t reflect.Type) {
2017-12-03 13:54:09 +00:00
if nil == t {
2017-12-06 00:48:03 +00:00
logging.Logger().Panic("DI: t[reflect.Type] is nil")
2017-12-03 13:54:09 +00:00
}
if !cdur.IsTypeKind(t, reflect.Struct, true) {
2017-12-06 00:48:03 +00:00
logging.Logger().Panic(fmt.Sprintf("DI: t[reflect.Type] must be specified but is %v", t))
2017-12-03 13:54:09 +00:00
}
2017-12-05 13:28:42 +00:00
td, err := cr.buildDefinition(t)
if nil != err {
2017-12-06 00:48:03 +00:00
logging.Logger().Panic(fmt.Sprintf("DI: %v", err))
2017-12-05 13:28:42 +00:00
}
2017-12-06 00:48:03 +00:00
if _, ok := cr.definitionByType[td.RealType]; ok {
logging.Logger().Panic(fmt.Sprintf("DI: The type[%s] of Component is exist already", td.FullName))
}
cr.definitionByType[td.RealType] = td
2017-12-05 13:28:42 +00:00
2018-03-15 13:19:44 +00:00
// _ca := ca.(*cdia.ComponentAnnotation)
ca := td.GetAnnotation(cdia.ComponentTag).(*cdia.Component)
2017-12-06 06:07:44 +00:00
name := ""
if nil != ca && "" != strings.Trim(ca.Name, " ") {
name = ca.Name
} else {
name = td.TypeName
2017-12-06 00:48:03 +00:00
}
2017-12-03 13:54:09 +00:00
2017-12-06 06:07:44 +00:00
if eTD, ok := cr.definitionByName[name]; ok {
logging.Logger().Panic(fmt.Sprintf("DI: The name[%s] of Component is exist already type[%s]", name, eTD.FullName))
2017-12-06 00:48:03 +00:00
}
2017-12-06 06:07:44 +00:00
cr.definitionByName[name] = td
2017-12-03 13:54:09 +00:00
}
2017-12-06 06:30:49 +00:00
func RegisterResource(name string, resource interface{}) error {
return registry.RegisterResource(name, resource)
}
func (cr *defaultComponentRegistry) RegisterResource(name string, resource interface{}) error {
if _, ok := cr.resourceByName[name]; ok {
return fmt.Errorf("DI: Resource[%s] is already exist", name)
}
cr.resourceByName[name] = resource
return nil
}
2017-12-08 10:32:59 +00:00
// GetInstance returns instance of type
// t must be pointer of struct
2018-03-15 07:32:04 +00:00
func GetInstance(t reflect.Type) interface{} {
2017-12-05 13:28:42 +00:00
return registry.GetInstance(t)
}
2018-03-15 07:32:04 +00:00
func (cr *defaultComponentRegistry) GetInstance(t reflect.Type) interface{} {
2017-12-05 10:02:41 +00:00
if nil == t {
2018-03-15 07:32:04 +00:00
logging.Logger().Panic(fmt.Sprintf("DI: t[reflect.Type] is nil"))
2017-12-05 10:02:41 +00:00
}
2017-12-06 00:48:03 +00:00
2017-12-08 10:32:59 +00:00
if reflect.Ptr != t.Kind() {
2018-03-15 07:32:04 +00:00
logging.Logger().Panic(fmt.Sprintf("DI: t[reflect.Type] must be pointer of struct"))
2017-12-08 10:32:59 +00:00
}
if reflect.Struct != t.Elem().Kind() {
2018-03-15 07:32:04 +00:00
logging.Logger().Panic(fmt.Sprintf("DI: t[reflect.Type] must be pointer of struct"))
2017-12-08 10:32:59 +00:00
}
2017-12-05 13:28:42 +00:00
var err error
2017-12-06 00:48:03 +00:00
rt, _, _ := cdur.GetTypeInfo(t)
td, ok := cr.definitionByType[rt]
2017-12-05 10:02:41 +00:00
if !ok {
2017-12-06 00:48:03 +00:00
td, err = cr.buildDefinition(t)
2017-12-05 13:28:42 +00:00
if nil != err {
2018-03-15 07:32:04 +00:00
logging.Logger().Panic(fmt.Sprintf("DI: %v", err))
2017-12-05 13:28:42 +00:00
}
2017-12-05 10:02:41 +00:00
}
2017-12-06 00:48:03 +00:00
2017-12-08 10:32:59 +00:00
comV, ok := cr.instanceByType[td.RealType]
if ok {
2018-03-15 07:32:04 +00:00
return comV
2017-12-08 10:32:59 +00:00
}
2017-12-06 10:28:15 +00:00
v := reflect.New(rt)
2017-12-08 10:32:59 +00:00
rv := v.Elem()
var annotation cda.Annotation
var fV interface{}
2017-12-06 10:28:15 +00:00
2017-12-08 10:32:59 +00:00
for _, fd := range td.Fields {
f := rv.FieldByName(fd.FieldName)
2017-12-06 10:28:15 +00:00
2017-12-08 10:32:59 +00:00
if !f.IsValid() {
2018-03-15 07:32:04 +00:00
logging.Logger().Panic(fmt.Sprintf("DI: Field[%s] is not valid", fd.FieldName))
2017-12-08 10:32:59 +00:00
}
if !f.CanSet() {
2018-03-15 07:32:04 +00:00
logging.Logger().Panic(fmt.Sprintf("DI: Field[%s] can not set", fd.FieldName))
2017-12-08 10:32:59 +00:00
}
2018-03-15 13:19:44 +00:00
annotation = fd.GetAnnotation(cdia.InjectTag)
2017-12-08 10:32:59 +00:00
if nil != annotation {
2018-03-15 07:32:04 +00:00
fV = cr.GetInstance(fd.Type)
2017-12-08 10:32:59 +00:00
}
2018-03-15 13:19:44 +00:00
annotation = fd.GetAnnotation(cdia.ResourceTag)
2017-12-08 10:32:59 +00:00
if nil != annotation {
2018-03-15 13:19:44 +00:00
n := annotation.(*cdia.Resource).Name
2017-12-08 10:32:59 +00:00
if "" == n {
n = fd.FieldName
}
2018-03-15 07:32:04 +00:00
fV = cr.GetInstanceByName(n)
2017-12-08 10:32:59 +00:00
}
if nil != err {
2018-03-15 07:32:04 +00:00
logging.Logger().Panic(fmt.Sprintf("DI: %v", err))
2017-12-08 10:32:59 +00:00
}
f.Set(reflect.ValueOf(fV))
2017-12-06 10:28:15 +00:00
}
2017-12-05 10:02:41 +00:00
2017-12-08 10:32:59 +00:00
cr.instanceByType[td.RealType] = v.Interface()
2018-03-15 07:32:04 +00:00
return v.Interface()
2017-12-03 13:54:09 +00:00
}
2018-03-15 07:32:04 +00:00
func GetInstanceByName(name string) interface{} {
2017-12-05 13:28:42 +00:00
return registry.GetInstanceByName(name)
}
2018-03-15 07:32:04 +00:00
func (cr *defaultComponentRegistry) GetInstanceByName(name string) interface{} {
2017-12-08 10:32:59 +00:00
v, ok := cr.resourceByName[name]
if ok {
2018-03-15 07:32:04 +00:00
return v
2017-12-08 10:32:59 +00:00
}
2018-03-15 07:32:04 +00:00
logging.Logger().Panic(fmt.Sprintf("DI: Resource[%s] is not exist", name))
return nil
2017-12-03 13:54:09 +00:00
}
2017-12-05 13:28:42 +00:00
func (cr *defaultComponentRegistry) buildDefinition(t reflect.Type) (*TypeDefinition, error) {
2017-12-05 10:02:41 +00:00
if nil == t {
2017-12-06 00:48:03 +00:00
return nil, fmt.Errorf("t[reflect.Type] is nil")
2017-12-05 10:02:41 +00:00
}
2017-12-05 13:28:42 +00:00
rt, pkgName, tName := cdur.GetTypeInfo(t)
2017-12-06 06:07:44 +00:00
td := &TypeDefinition{}
td.FullName = FullName(pkgName, tName)
td.PkgName = pkgName
td.TypeName = tName
td.Type = t
td.RealType = rt
2018-03-15 13:19:44 +00:00
td.Fields = make([]*FieldDefinition, 0)
2017-12-03 13:54:09 +00:00
2018-03-15 13:19:44 +00:00
parseFields(rt, td)
2017-12-08 10:32:59 +00:00
return td, nil
}
2018-03-15 13:19:44 +00:00
func parseFields(t reflect.Type, td *TypeDefinition) {
// fields := make([]*FieldDefinition, 0)
2017-12-08 10:32:59 +00:00
rt, _, _ := cdur.GetTypeInfo(t)
if reflect.Struct != rt.Kind() {
2018-03-15 13:19:44 +00:00
return
2017-12-08 10:32:59 +00:00
}
for i := 0; i < rt.NumField(); i++ {
f := rt.Field(i)
if f.Anonymous {
2018-03-15 13:19:44 +00:00
if parseAnonymousField(f, td) {
continue
}
parseFields(f.Type, td)
2017-12-08 10:32:59 +00:00
} else {
2017-12-05 13:28:42 +00:00
as, err := cda.ParseAnnotation(f.Tag)
if nil != err {
2018-03-15 13:19:44 +00:00
return
2017-12-05 13:28:42 +00:00
}
if nil != as && 0 < len(as) {
fRT, fPkgName, fTName := cdur.GetTypeInfo(f.Type)
fd := &FieldDefinition{
2017-12-08 10:32:59 +00:00
FieldName: f.Name,
2017-12-05 13:28:42 +00:00
PkgName: fPkgName,
TypeName: fTName,
Type: f.Type,
RealType: fRT,
Annotations: as,
}
2018-03-15 13:19:44 +00:00
td.Fields = append(td.Fields, fd)
2017-12-05 13:28:42 +00:00
}
}
}
2018-03-15 13:19:44 +00:00
}
func parseAnonymousField(f reflect.StructField, td *TypeDefinition) bool {
logging.Logger().Debug(fmt.Sprintf("Anonymous Pkg:%s, Type: %s", f.Type.PkgPath(), f.Type.Name()))
ct := reflect.TypeOf((*cda.TypeAnnotation)(nil)).Elem()
logging.Logger().Debug(fmt.Sprintf("TypeAnnon Pkg:%s, Type: %s", ct.PkgPath(), ct.Name()))
if f.Type != ct {
return false
}
as, err := cda.ParseAnnotation(f.Tag)
if nil != err {
return false
}
if nil != as && 0 < len(as) {
td.TypeAnnotations = as
return true
}
2017-12-05 13:28:42 +00:00
2018-03-15 13:19:44 +00:00
return false
2017-12-03 13:54:09 +00:00
}