package registry import ( "fmt" "log" "reflect" "runtime" "strings" cda "git.loafle.net/commons_go/di/annotation" cdia "git.loafle.net/commons_go/di/injection/annotation" cdur "git.loafle.net/commons_go/di/util/reflect" "git.loafle.net/commons_go/logging" ) func init() { registry = newRegistry() } var registry ComponentRegistry type ComponentRegistry interface { RegisterType(t reflect.Type) RegisterResource(name string, resource interface{}) error GetInstance(t reflect.Type) interface{} GetInstances(ts []reflect.Type) []interface{} GetInstanceByName(name string) interface{} GetInstancesByAnnotationName(n string) []interface{} } func newRegistry() ComponentRegistry { r := &defaultComponentRegistry{} r.definitionByType = make(map[reflect.Type]*TypeDefinition, 0) r.definitionByName = make(map[string]*TypeDefinition, 0) r.instanceByType = make(map[reflect.Type]interface{}, 0) r.resourceByName = make(map[string]interface{}, 0) return r } type defaultComponentRegistry struct { definitionByType map[reflect.Type]*TypeDefinition definitionByName map[string]*TypeDefinition instanceByType map[reflect.Type]interface{} resourceByName map[string]interface{} } func RegisterType(t reflect.Type) { pc, _, _, ok := runtime.Caller(1) details := runtime.FuncForPC(pc) if ok && details != nil { log.Printf("called from %s\n", details.Name()) } registry.RegisterType(t) } func (cr *defaultComponentRegistry) RegisterType(t reflect.Type) { if nil == t { logging.Logger().Panic("DI: t[reflect.Type] is nil") } if !cdur.IsTypeKind(t, reflect.Struct, true) { logging.Logger().Panic(fmt.Sprintf("DI: t[reflect.Type] must be specified but is %v", t)) } td, err := cr.buildDefinition(t) if nil != err { logging.Logger().Panic(fmt.Sprintf("DI: %v", err)) } 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 // _ca := ca.(*cdia.ComponentAnnotation) ca := td.GetAnnotation(cdia.ComponentTag).(*cdia.Component) name := "" if nil != ca && "" != strings.Trim(ca.Name, " ") { name = ca.Name } else { name = td.TypeName } 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)) } cr.definitionByName[name] = td } 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 } // GetInstance returns instance of type // t must be pointer of struct func GetInstance(t reflect.Type) interface{} { return registry.GetInstance(t) } func (cr *defaultComponentRegistry) GetInstance(t reflect.Type) interface{} { if nil == t { logging.Logger().Panic(fmt.Sprintf("DI: t[reflect.Type] is nil")) } if reflect.Ptr != t.Kind() { logging.Logger().Panic(fmt.Sprintf("DI: t[reflect.Type] must be pointer of struct")) } if reflect.Struct != t.Elem().Kind() { logging.Logger().Panic(fmt.Sprintf("DI: t[reflect.Type] must be pointer of struct")) } var err error rt, _, _ := cdur.GetTypeInfo(t) td, ok := cr.definitionByType[rt] if !ok { td, err = cr.buildDefinition(t) if nil != err { logging.Logger().Panic(fmt.Sprintf("DI: %v", err)) } } comV, ok := cr.instanceByType[td.RealType] if ok { return comV } v := reflect.New(rt) rv := v.Elem() var annotation cda.Annotation var fV interface{} for _, fd := range td.Fields { f := rv.FieldByName(fd.FieldName) if !f.IsValid() { logging.Logger().Panic(fmt.Sprintf("DI: Field[%s] is not valid", fd.FieldName)) } if !f.CanSet() { logging.Logger().Panic(fmt.Sprintf("DI: Field[%s] can not set", fd.FieldName)) } annotation = fd.GetAnnotation(cdia.InjectTag) if nil != annotation { fV = cr.GetInstance(fd.Type) } annotation = fd.GetAnnotation(cdia.ResourceTag) if nil != annotation { n := annotation.(*cdia.Resource).Name if "" == n { n = fd.FieldName } fV = cr.GetInstanceByName(n) } if nil != err { logging.Logger().Panic(fmt.Sprintf("DI: %v", err)) } f.Set(reflect.ValueOf(fV)) } cr.instanceByType[td.RealType] = v.Interface() return v.Interface() } func GetInstanceByName(name string) interface{} { return registry.GetInstanceByName(name) } func (cr *defaultComponentRegistry) GetInstanceByName(name string) interface{} { v, ok := cr.resourceByName[name] if ok { return v } logging.Logger().Panic(fmt.Sprintf("DI: Resource[%s] is not exist", name)) return nil } // GetInstances returns instance of annotated // n must be name of registered annotation func GetInstances(ts []reflect.Type) []interface{} { return registry.GetInstances(ts) } func (cr *defaultComponentRegistry) GetInstances(ts []reflect.Type) []interface{} { instances := make([]interface{}, 0) for _, t := range ts { if i := cr.GetInstance(t); nil != i { instances = append(instances, i) } } return instances } // GetInstancesByAnnotationName returns instance of annotated // n must be name of registered annotation func GetInstancesByAnnotationName(n string) []interface{} { return registry.GetInstancesByAnnotationName(n) } func (cr *defaultComponentRegistry) GetInstancesByAnnotationName(n string) []interface{} { instances := make([]interface{}, 0) for _, td := range cr.definitionByType { if nil != td.GetAnnotation(n) { instances = append(instances, cr.GetInstance(td.Type)) } } return instances } func (cr *defaultComponentRegistry) buildDefinition(t reflect.Type) (*TypeDefinition, error) { if nil == t { return nil, fmt.Errorf("t[reflect.Type] is nil") } rt, pkgName, tName := cdur.GetTypeInfo(t) td := &TypeDefinition{} td.FullName = FullName(pkgName, tName) td.PkgName = pkgName td.TypeName = tName td.Type = t td.RealType = rt td.Fields = make([]*FieldDefinition, 0) parseFields(rt, td) return td, nil } func parseFields(t reflect.Type, td *TypeDefinition) { // fields := make([]*FieldDefinition, 0) rt, _, _ := cdur.GetTypeInfo(t) if reflect.Struct != rt.Kind() { return } for i := 0; i < rt.NumField(); i++ { f := rt.Field(i) if f.Anonymous { if parseAnonymousField(f, td) { continue } parseFields(f.Type, td) } else { as, err := cda.ParseAnnotation(f.Tag) if nil != err { return } if nil != as && 0 < len(as) { fRT, fPkgName, fTName := cdur.GetTypeInfo(f.Type) fd := &FieldDefinition{ FieldName: f.Name, PkgName: fPkgName, TypeName: fTName, Type: f.Type, RealType: fRT, Annotations: as, } td.Fields = append(td.Fields, fd) } } } } 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 } return false }