di/registry/registry.go

176 lines
4.1 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-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-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-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-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-06 10:28:15 +00:00
v := reflect.New(rt)
i := v.Interface()
for f, fd := range td.Fields {
}
2017-12-05 10:02:41 +00:00
return nil, 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-03 13:54:09 +00:00
return nil, nil
}
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-05 13:28:42 +00:00
nf := rt.NumField()
if 0 < nf {
fields := make(map[string]*FieldDefinition)
for i := 0; i < nf; i++ {
f := rt.Field(i)
as, err := cda.ParseAnnotation(f.Tag)
if nil != err {
return nil, err
}
if nil != as && 0 < len(as) {
fRT, fPkgName, fTName := cdur.GetTypeInfo(f.Type)
fd := &FieldDefinition{
PkgName: fPkgName,
TypeName: fTName,
Type: f.Type,
RealType: fRT,
Annotations: as,
}
fields[f.Name] = fd
}
}
if 0 < len(fields) {
2017-12-06 06:07:44 +00:00
td.Fields = fields
2017-12-05 13:28:42 +00:00
}
}
2017-12-06 06:07:44 +00:00
return td, nil
2017-12-03 13:54:09 +00:00
}