diff --git a/annotations/inject.go b/annotations/inject.go deleted file mode 100644 index 946e8dd..0000000 --- a/annotations/inject.go +++ /dev/null @@ -1,21 +0,0 @@ -package annotations - -// @Inject(name? string) - -import ( - "reflect" - - "git.loafle.net/loafer/annotation-go" -) - -var InjectAnnotationType = reflect.TypeOf((*InjectAnnotation)(nil)) - -func init() { - annotation.Register(InjectAnnotationType) -} - -type InjectAnnotation struct { - annotation.TypeAnnotation `@annotation:"@Inject"` - - Name string `json:"name"` -} diff --git a/annotations/resource.go b/annotations/resource.go deleted file mode 100644 index 10f27da..0000000 --- a/annotations/resource.go +++ /dev/null @@ -1,18 +0,0 @@ -package annotations - -import ( - "reflect" - - "git.loafle.net/loafer/annotation-go" -) - -var ResourceAnnotationType = reflect.TypeOf((*ResourceAnnotation)(nil)) - -func init() { - annotation.Register(ResourceAnnotationType) -} - -type ResourceAnnotation struct { - annotation.TypeAnnotation `@annotation:"@Resource"` - Name string `json:"name"` -} diff --git a/go.mod b/go.mod index 51bf90d..492f87e 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,6 @@ module git.loafle.net/loafer/di-go go 1.13 require ( - git.loafle.net/loafer/annotation-go v0.0.0-20191112145817-e44b732fea76 + git.loafle.net/loafer/annotation-go v0.0.0-20191113135342-a3974dc21898 git.loafle.net/loafer/util-go v0.0.0-20191113132317-6eeae49d258d ) diff --git a/go.sum b/go.sum index 434af39..a471600 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -git.loafle.net/loafer/annotation-go v0.0.0-20191112145817-e44b732fea76 h1:fUggcxR3GQLUeDVt2vuLJ7NW20sdr+Cnk/Q0uOioBLI= -git.loafle.net/loafer/annotation-go v0.0.0-20191112145817-e44b732fea76/go.mod h1:1yow6wwbB3nWq6Asgt3BAPfXJTjZeqgMYF+VVPlj9Xk= +git.loafle.net/loafer/annotation-go v0.0.0-20191113135342-a3974dc21898 h1:3moCEHhGfzHTQVBde+IkvB42oDGUdpCDkBWjN5ZnoL8= +git.loafle.net/loafer/annotation-go v0.0.0-20191113135342-a3974dc21898/go.mod h1:1yow6wwbB3nWq6Asgt3BAPfXJTjZeqgMYF+VVPlj9Xk= git.loafle.net/loafer/util-go v0.0.0-20191112142134-9a567d18b779/go.mod h1:HGVw9FNJIc/UFDIzxmoIj5K2+D9Eadal5jjHOq0NFOU= git.loafle.net/loafer/util-go v0.0.0-20191113132317-6eeae49d258d h1:ESDbDHHzH2Ysq+thQrO/OQtyDkVhzNzshjn0SJIqa0g= git.loafle.net/loafer/util-go v0.0.0-20191113132317-6eeae49d258d/go.mod h1:HGVw9FNJIc/UFDIzxmoIj5K2+D9Eadal5jjHOq0NFOU= diff --git a/registry.go b/registry.go index 1a85e1a..0524817 100644 --- a/registry.go +++ b/registry.go @@ -8,7 +8,6 @@ import ( "strings" "git.loafle.net/loafer/annotation-go" - "git.loafle.net/loafer/di-go/annotations" luReflect "git.loafle.net/loafer/util-go/reflect" ) @@ -82,8 +81,8 @@ func (r *Registry) RegisterType(t reflect.Type) { name := td.TypeName - if a := td.GetTypeAnnotationByType(annotations.ComponentAnnotationType, true); nil != a { - ca := a.(*annotations.ComponentAnnotation) + if a := td.GetTypeAnnotationByType(ComponentAnnotationType, true); nil != a { + ca := a.(*ComponentAnnotation) if "" != strings.Trim(ca.Name, " ") { name = ca.Name } @@ -172,14 +171,14 @@ func (r *Registry) GetInstance(t reflect.Type) (instance interface{}, err error) return } - if annotation = fd.GetAnnotationByType(annotations.InjectAnnotationType, false); nil != annotation { + if annotation = fd.GetAnnotationByType(InjectAnnotationType, false); nil != annotation { if fV, err = r.GetInstance(fd.Type); nil != err { return } } - if annotation = fd.GetAnnotationByType(annotations.ResourceAnnotationType, false); nil != annotation { - n := annotation.(*annotations.ResourceAnnotation).Name + if annotation = fd.GetAnnotationByType(ResourceAnnotationType, false); nil != annotation { + n := annotation.(*ResourceAnnotation).Name if "" == n { n = fd.FieldName } diff --git a/annotations/component.go b/types.go similarity index 54% rename from annotations/component.go rename to types.go index 6c508dc..fa69df5 100644 --- a/annotations/component.go +++ b/types.go @@ -1,4 +1,4 @@ -package annotations +package di // @Inject(name? string) @@ -8,15 +8,32 @@ import ( "git.loafle.net/loafer/annotation-go" ) -var ComponentAnnotationType = reflect.TypeOf((*ComponentAnnotation)(nil)) - func init() { annotation.Register(ComponentAnnotationType) + annotation.Register(InjectAnnotationType) + annotation.Register(ResourceAnnotationType) } +var ComponentAnnotationType = reflect.TypeOf((*ComponentAnnotation)(nil)) + type ComponentAnnotation struct { annotation.TypeAnnotation `@annotation:"@Component"` Name string `json:"name"` InitMethod string `json:"initMethod"` // func (receiver interface{}, cr ComponentRegistry) error DestroyMethod string `json:"destroyMethod"` // func (receiver interface{}, cr ComponentRegistry) error } + +var InjectAnnotationType = reflect.TypeOf((*InjectAnnotation)(nil)) + +type InjectAnnotation struct { + annotation.TypeAnnotation `@annotation:"@Inject"` + + Name string `json:"name"` +} + +var ResourceAnnotationType = reflect.TypeOf((*ResourceAnnotation)(nil)) + +type ResourceAnnotation struct { + annotation.TypeAnnotation `@annotation:"@Resource"` + Name string `json:"name"` +}