di/registry/registry.go

240 lines
5.6 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"
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 {
2017-12-06 00:48:03 +00:00
RegisterType(t reflect.Type, ca *cda.ComponentAnnotation)
2017-12-06 06:30:49 +00:00
RegisterResource(name string, resource interface{}) error
2017-12-03 10:55:10 +00:00
GetInstance(t reflect.Type) (interface{}, error)
GetInstanceByName(name string) (interface{}, error)
}
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
}
2017-12-06 00:48:03 +00:00
func RegisterType(t reflect.Type, ca *cda.ComponentAnnotation) {
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())
}
2017-12-06 00:48:03 +00:00
registry.RegisterType(t, ca)
2017-12-05 13:28:42 +00:00
}
2017-12-06 00:48:03 +00:00
func (cr *defaultComponentRegistry) RegisterType(t reflect.Type, ca *cda.ComponentAnnotation) {
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
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
2017-12-05 13:28:42 +00:00
func GetInstance(t reflect.Type) (interface{}, error) {
return registry.GetInstance(t)
}
2017-12-05 10:02:41 +00:00
func (cr *defaultComponentRegistry) GetInstance(t reflect.Type) (interface{}, error) {
if nil == t {
return nil, fmt.Errorf("DI: t[reflect.Type] is nil")
}
2017-12-06 00:48:03 +00:00
2017-12-08 10:32:59 +00:00
if reflect.Ptr != t.Kind() {
return nil, fmt.Errorf("DI: t[reflect.Type] must be pointer of struct")
}
if reflect.Struct != t.Elem().Kind() {
return nil, fmt.Errorf("DI: t[reflect.Type] must be pointer of struct")
}
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 {
return nil, err
}
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 {
return comV, nil
}
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() {
return nil, fmt.Errorf("DI: Field[%s] is not valid", fd.FieldName)
}
if !f.CanSet() {
return nil, fmt.Errorf("DI: Field[%s] can not set", fd.FieldName)
}
annotation = fd.GetAnnotation(cda.InjectTag)
if nil != annotation {
fV, err = cr.GetInstance(fd.Type)
}
annotation = fd.GetAnnotation(cda.ResourceTag)
if nil != annotation {
n := annotation.(*cda.Resource).Name
if "" == n {
n = fd.FieldName
}
fV, err = cr.GetInstanceByName(n)
}
if nil != err {
return nil, err
}
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()
return v.Interface(), nil
2017-12-03 13:54:09 +00:00
}
2017-12-05 13:28:42 +00:00
func GetInstanceByName(name string) (interface{}, error) {
return registry.GetInstanceByName(name)
}
2017-12-05 10:02:41 +00:00
func (cr *defaultComponentRegistry) GetInstanceByName(name string) (interface{}, error) {
2017-12-08 10:32:59 +00:00
v, ok := cr.resourceByName[name]
if ok {
return v, nil
}
return nil, fmt.Errorf("DI: Resource[%s] is not exist", name)
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
2017-12-03 13:54:09 +00:00
2017-12-08 10:32:59 +00:00
fields := getFields(rt)
if 0 < len(fields) {
td.Fields = fields
}
return td, nil
}
func getFields(t reflect.Type) []*FieldDefinition {
fields := make([]*FieldDefinition, 0)
rt, _, _ := cdur.GetTypeInfo(t)
if reflect.Struct != rt.Kind() {
return fields
}
for i := 0; i < rt.NumField(); i++ {
f := rt.Field(i)
if f.Anonymous {
fields = append(fields, getFields(f.Type)...)
} else {
2017-12-05 13:28:42 +00:00
as, err := cda.ParseAnnotation(f.Tag)
if nil != err {
2017-12-08 10:32:59 +00:00
return fields
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,
}
2017-12-08 10:32:59 +00:00
fields = append(fields, fd)
2017-12-05 13:28:42 +00:00
}
}
}
2017-12-08 10:32:59 +00:00
return fields
2017-12-03 13:54:09 +00:00
}