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-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-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-05 10:02:41 +00:00
|
|
|
RegisterType(t reflect.Type, ca *cda.ComponentAnnotation) 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{}
|
|
|
|
r.definitionsByType = make(map[reflect.Type]*TypeDefinition, 0)
|
|
|
|
r.definitionByName = make(map[string]*TypeDefinition, 0)
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2017-12-03 13:54:09 +00:00
|
|
|
type defaultComponentRegistry struct {
|
2017-12-05 13:28:42 +00:00
|
|
|
definitionsByType map[reflect.Type]*TypeDefinition
|
2017-12-05 10:02:41 +00:00
|
|
|
|
2017-12-05 13:28:42 +00:00
|
|
|
definitionByName map[string]*TypeDefinition
|
2017-12-03 13:54:09 +00:00
|
|
|
}
|
|
|
|
|
2017-12-05 13:28:42 +00:00
|
|
|
func RegisterType(t reflect.Type, ca *cda.ComponentAnnotation) error {
|
|
|
|
return registry.RegisterType(t, ca)
|
|
|
|
}
|
2017-12-05 10:02:41 +00:00
|
|
|
func (cr *defaultComponentRegistry) RegisterType(t reflect.Type, ca *cda.ComponentAnnotation) error {
|
2017-12-03 13:54:09 +00:00
|
|
|
if nil == t {
|
|
|
|
return fmt.Errorf("DI: t[reflect.Type] is nil")
|
|
|
|
}
|
|
|
|
if !cdur.IsTypeKind(t, reflect.Struct, true) {
|
|
|
|
return fmt.Errorf("DI: t[reflect.Type] must be specified but is %v", t)
|
|
|
|
}
|
|
|
|
|
2017-12-05 13:28:42 +00:00
|
|
|
td, err := cr.buildDefinition(t)
|
|
|
|
if nil != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cr.definitionsByType[td.RealType] = td
|
|
|
|
|
|
|
|
if nil != ca {
|
|
|
|
if nil != ca.Names && 0 < len(ca.Names) {
|
|
|
|
for _, n := range ca.Names {
|
|
|
|
cr.definitionByName[n] = td
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-12-03 13:54:09 +00:00
|
|
|
|
|
|
|
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-05 13:28:42 +00:00
|
|
|
var err error
|
2017-12-05 10:02:41 +00:00
|
|
|
cd, ok := cr.definitionsByType[t]
|
|
|
|
if !ok {
|
2017-12-05 13:28:42 +00:00
|
|
|
cd, err = cr.buildDefinition(t)
|
|
|
|
if nil != err {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-12-05 10:02:41 +00:00
|
|
|
}
|
2017-12-05 13:28:42 +00:00
|
|
|
log.Printf("%v", cd)
|
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 {
|
|
|
|
return nil, fmt.Errorf("DI: t[reflect.Type] is nil")
|
|
|
|
}
|
|
|
|
|
2017-12-05 13:28:42 +00:00
|
|
|
rt, pkgName, tName := cdur.GetTypeInfo(t)
|
|
|
|
cd := &TypeDefinition{}
|
2017-12-05 10:02:41 +00:00
|
|
|
cd.PkgName = pkgName
|
|
|
|
cd.TypeName = tName
|
|
|
|
cd.Type = t
|
|
|
|
cd.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) {
|
|
|
|
cd.Fields = fields
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return cd, nil
|
2017-12-03 13:54:09 +00:00
|
|
|
}
|