di/registry/registry.go

314 lines
7.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"
2018-03-21 12:31:37 +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"
2018-03-20 02:43:00 +00:00
cur "git.loafle.net/commons_go/util/reflect"
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-21 12:33:53 +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-21 09:28:21 +00:00
GetInstance(t reflect.Type) (interface{}, error)
GetInstances(ts []reflect.Type) ([]interface{}, error)
GetInstanceByName(name string) (interface{}, error)
GetInstancesByAnnotationName(n string) ([]interface{}, error)
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-21 12:33:53 +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 {
2018-03-21 12:31:37 +00:00
log.Printf("called from %s", details.Name())
2017-12-06 06:07:44 +00:00
}
2018-03-21 12:33:53 +00:00
registry.RegisterType(t)
2017-12-05 13:28:42 +00:00
}
2018-03-21 12:33:53 +00:00
func (cr *defaultComponentRegistry) RegisterType(t reflect.Type) {
2017-12-03 13:54:09 +00:00
if nil == t {
2018-03-21 12:33:53 +00:00
log.Panicf("DI: t[reflect.Type] is nil")
2017-12-03 13:54:09 +00:00
}
2018-03-20 02:43:00 +00:00
if !cur.IsTypeKind(t, reflect.Struct, true) {
2018-03-21 12:33:53 +00:00
log.Panicf("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 {
2018-03-21 12:33:53 +00:00
log.Panicf("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 {
2018-03-21 12:33:53 +00:00
log.Panicf("DI: The type[%s] of Component is exist already", td.FullName)
2017-12-06 00:48:03 +00:00
}
cr.definitionByType[td.RealType] = td
2017-12-05 13:28:42 +00:00
2018-03-15 14:32:45 +00:00
name := td.TypeName
if a := td.GetAnnotationByType(reflect.TypeOf((*cdia.Component)(nil)), true); nil != a {
ca := a.(*cdia.Component)
if "" != strings.Trim(ca.Name, " ") {
name = ca.Name
}
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 {
2018-03-21 12:33:53 +00:00
log.Panicf("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-21 09:28:21 +00:00
func GetInstance(t reflect.Type) (interface{}, error) {
2017-12-05 13:28:42 +00:00
return registry.GetInstance(t)
}
2018-03-21 09:28:21 +00:00
func (cr *defaultComponentRegistry) GetInstance(t reflect.Type) (interface{}, error) {
2017-12-05 10:02:41 +00:00
if nil == t {
2018-03-21 09:28:21 +00:00
return nil, fmt.Errorf("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-21 09:28:21 +00:00
return nil, fmt.Errorf("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-21 09:28:21 +00:00
return nil, fmt.Errorf("DI: t[reflect.Type] must be pointer of struct")
2017-12-08 10:32:59 +00:00
}
2018-03-21 12:54:05 +00:00
var (
td *TypeDefinition
comV interface{}
ok bool
err error
)
2017-12-06 00:48:03 +00:00
2018-03-20 02:43:00 +00:00
rt, _, _ := cur.GetTypeInfo(t)
2018-03-21 12:54:05 +00:00
if td, ok = cr.definitionByType[rt]; !ok {
if td, err = cr.buildDefinition(t); nil != err {
2018-03-21 09:28:21 +00:00
return nil, fmt.Errorf("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
2018-03-21 12:54:05 +00:00
if comV, ok = cr.instanceByType[td.RealType]; ok {
2018-03-21 09:28:21 +00:00
return comV, nil
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()
2018-03-21 12:54:05 +00:00
var (
annotation cda.Annotation
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-21 09:28:21 +00:00
return nil, fmt.Errorf("DI: Field[%s] is not valid", fd.FieldName)
2017-12-08 10:32:59 +00:00
}
if !f.CanSet() {
2018-03-21 09:28:21 +00:00
return nil, fmt.Errorf("DI: Field[%s] can not set", fd.FieldName)
2017-12-08 10:32:59 +00:00
}
2018-03-16 03:16:23 +00:00
if annotation = fd.GetAnnotationByType(reflect.TypeOf((*cdia.Inject)(nil)), false); nil != annotation {
2018-03-21 12:54:05 +00:00
if fV, err = cr.GetInstance(fd.Type); nil != err {
2018-03-21 09:28:21 +00:00
return nil, err
}
2017-12-08 10:32:59 +00:00
}
2018-03-16 03:16:23 +00:00
if annotation = fd.GetAnnotationByType(reflect.TypeOf((*cdia.Resource)(nil)), false); 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-21 12:54:05 +00:00
if fV, err = cr.GetInstanceByName(n); nil != err {
2018-03-21 09:28:21 +00:00
return nil, err
}
2017-12-08 10:32:59 +00:00
}
if nil != err {
2018-03-21 09:28:21 +00:00
return nil, fmt.Errorf("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-21 09:28:21 +00:00
return v.Interface(), nil
2017-12-03 13:54:09 +00:00
}
2018-03-21 09:28:21 +00:00
func GetInstanceByName(name string) (interface{}, error) {
2017-12-05 13:28:42 +00:00
return registry.GetInstanceByName(name)
}
2018-03-21 09:28:21 +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 {
2018-03-21 09:28:21 +00:00
return v, nil
2017-12-08 10:32:59 +00:00
}
2018-03-21 09:28:21 +00:00
return nil, fmt.Errorf("DI: Resource[%s] is not exist", name)
2017-12-03 13:54:09 +00:00
}
2018-03-15 14:08:51 +00:00
// GetInstances returns instance of annotated
// n must be name of registered annotation
2018-03-21 09:28:21 +00:00
func GetInstances(ts []reflect.Type) ([]interface{}, error) {
2018-03-15 14:08:51 +00:00
return registry.GetInstances(ts)
}
2018-03-21 09:28:21 +00:00
func (cr *defaultComponentRegistry) GetInstances(ts []reflect.Type) ([]interface{}, error) {
var (
i interface{}
err error
)
2018-03-15 14:08:51 +00:00
instances := make([]interface{}, 0)
for _, t := range ts {
2018-03-21 12:54:05 +00:00
if i, err = cr.GetInstance(t); nil != err {
2018-03-21 09:28:21 +00:00
return nil, err
2018-03-15 14:08:51 +00:00
}
2018-03-21 09:28:21 +00:00
instances = append(instances, i)
2018-03-15 14:08:51 +00:00
}
2018-03-21 09:28:21 +00:00
return instances, nil
2018-03-15 14:08:51 +00:00
}
2018-03-15 13:49:25 +00:00
// GetInstancesByAnnotationName returns instance of annotated
// n must be name of registered annotation
2018-03-21 09:28:21 +00:00
func GetInstancesByAnnotationName(n string) ([]interface{}, error) {
2018-03-15 13:49:25 +00:00
return registry.GetInstancesByAnnotationName(n)
}
2018-03-21 09:28:21 +00:00
func (cr *defaultComponentRegistry) GetInstancesByAnnotationName(n string) ([]interface{}, error) {
var (
i interface{}
err error
)
2018-03-15 13:49:25 +00:00
instances := make([]interface{}, 0)
for _, td := range cr.definitionByType {
if nil != td.GetAnnotation(n) {
2018-03-21 12:54:05 +00:00
if i, err = cr.GetInstance(td.Type); nil != err {
2018-03-21 09:28:21 +00:00
return nil, err
}
instances = append(instances, i)
2018-03-15 13:49:25 +00:00
}
}
2018-03-21 09:28:21 +00:00
return instances, nil
2018-03-15 13:49:25 +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
}
2018-03-20 02:43:00 +00:00
rt, pkgName, tName := cur.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)
2018-03-20 02:43:00 +00:00
rt, _, _ := cur.GetTypeInfo(t)
2017-12-08 10:32:59 +00:00
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) {
2018-03-20 02:43:00 +00:00
fRT, fPkgName, fTName := cur.GetTypeInfo(f.Type)
2017-12-05 13:28:42 +00:00
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 {
ct := reflect.TypeOf((*cda.TypeAnnotation)(nil)).Elem()
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
}