FieldAnnotation is added

This commit is contained in:
병준 박 2019-12-02 21:58:00 +09:00
parent d08fa9dbe7
commit d7ef4e8d24
5 changed files with 92 additions and 8 deletions

View File

@ -16,6 +16,7 @@ type TypeDefinition struct {
RealType reflect.Type RealType reflect.Type
TypeAnnotations map[reflect.Type]annotation.Annotation TypeAnnotations map[reflect.Type]annotation.Annotation
FieldAnnotations map[string]map[reflect.Type]annotation.Annotation
MethodAnnotations map[string]map[reflect.Type]annotation.Annotation MethodAnnotations map[string]map[reflect.Type]annotation.Annotation
Fields []*FieldDefinition Fields []*FieldDefinition
} }
@ -44,6 +45,36 @@ func (td *TypeDefinition) GetTypeAnnotationByType(at reflect.Type, includeEmbedd
return nil return nil
} }
func (td *TypeDefinition) GetFieldAnnotationByType(at reflect.Type, methodName string) annotation.Annotation {
if nil == td.FieldAnnotations {
return nil
}
fs, ok := td.FieldAnnotations[methodName]
if !ok {
return nil
}
return fs[at]
}
func (td *TypeDefinition) GetFieldAnnotationsByType(at reflect.Type) map[string]annotation.Annotation {
if nil == td.FieldAnnotations {
return nil
}
fas := make(map[string]annotation.Annotation)
for k, v := range td.FieldAnnotations {
a, ok := v[at]
if !ok {
continue
}
fas[k] = a
}
return fas
}
func (td *TypeDefinition) GetMethodAnnotationByType(at reflect.Type, methodName string) annotation.Annotation { func (td *TypeDefinition) GetMethodAnnotationByType(at reflect.Type, methodName string) annotation.Annotation {
if nil == td.MethodAnnotations { if nil == td.MethodAnnotations {
return nil return nil

2
go.mod
View File

@ -3,6 +3,6 @@ module git.loafle.net/loafer/di-go
go 1.13 go 1.13
require ( require (
git.loafle.net/loafer/annotation-go v0.0.0-20191113135342-a3974dc21898 git.loafle.net/loafer/annotation-go v0.0.0-20191202124147-cb9404f54278
git.loafle.net/loafer/util-go v0.0.0-20191113132317-6eeae49d258d git.loafle.net/loafer/util-go v0.0.0-20191113132317-6eeae49d258d
) )

4
go.sum
View File

@ -1,5 +1,5 @@
git.loafle.net/loafer/annotation-go v0.0.0-20191113135342-a3974dc21898 h1:3moCEHhGfzHTQVBde+IkvB42oDGUdpCDkBWjN5ZnoL8= git.loafle.net/loafer/annotation-go v0.0.0-20191202124147-cb9404f54278 h1:ncGi8FZ/raFuzqR9689YyLQqM4Tq639c8jL1p4rjtfY=
git.loafle.net/loafer/annotation-go v0.0.0-20191113135342-a3974dc21898/go.mod h1:1yow6wwbB3nWq6Asgt3BAPfXJTjZeqgMYF+VVPlj9Xk= git.loafle.net/loafer/annotation-go v0.0.0-20191202124147-cb9404f54278/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-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 h1:ESDbDHHzH2Ysq+thQrO/OQtyDkVhzNzshjn0SJIqa0g=
git.loafle.net/loafer/util-go v0.0.0-20191113132317-6eeae49d258d/go.mod h1:HGVw9FNJIc/UFDIzxmoIj5K2+D9Eadal5jjHOq0NFOU= git.loafle.net/loafer/util-go v0.0.0-20191113132317-6eeae49d258d/go.mod h1:HGVw9FNJIc/UFDIzxmoIj5K2+D9Eadal5jjHOq0NFOU=

View File

@ -26,6 +26,8 @@ type Regist interface {
GetInstancesByAnnotationType(t reflect.Type) ([]interface{}, error) GetInstancesByAnnotationType(t reflect.Type) ([]interface{}, error)
GetTypeAnnotation(instanceType reflect.Type, annotationType reflect.Type) annotation.Annotation GetTypeAnnotation(instanceType reflect.Type, annotationType reflect.Type) annotation.Annotation
GetFieldAnnotation(instanceType reflect.Type, annotationType reflect.Type, methodName string) annotation.Annotation
GetFieldAnnotations(instanceType reflect.Type, annotationType reflect.Type) map[string]annotation.Annotation
GetMethodAnnotation(instanceType reflect.Type, annotationType reflect.Type, methodName string) annotation.Annotation GetMethodAnnotation(instanceType reflect.Type, annotationType reflect.Type, methodName string) annotation.Annotation
GetMethodAnnotations(instanceType reflect.Type, annotationType reflect.Type) map[string]annotation.Annotation GetMethodAnnotations(instanceType reflect.Type, annotationType reflect.Type) map[string]annotation.Annotation
} }
@ -274,6 +276,30 @@ func (r *Registry) GetTypeAnnotation(instanceType reflect.Type, annotationType r
return def.GetTypeAnnotationByType(annotationType, false) return def.GetTypeAnnotationByType(annotationType, false)
} }
func GetFieldAnnotation(instanceType reflect.Type, annotationType reflect.Type, methodName string) annotation.Annotation {
return registry.GetFieldAnnotation(instanceType, annotationType, methodName)
}
func (r *Registry) GetFieldAnnotation(instanceType reflect.Type, annotationType reflect.Type, methodName string) annotation.Annotation {
def, ok := r.definitionByType[instanceType]
if !ok {
return nil
}
return def.GetFieldAnnotationByType(annotationType, methodName)
}
func GetFieldAnnotations(instanceType reflect.Type, annotationType reflect.Type) map[string]annotation.Annotation {
return registry.GetFieldAnnotations(instanceType, annotationType)
}
func (r *Registry) GetFieldAnnotations(instanceType reflect.Type, annotationType reflect.Type) map[string]annotation.Annotation {
def, ok := r.definitionByType[instanceType]
if !ok {
return nil
}
return def.GetFieldAnnotationsByType(annotationType)
}
func GetMethodAnnotation(instanceType reflect.Type, annotationType reflect.Type, methodName string) annotation.Annotation { func GetMethodAnnotation(instanceType reflect.Type, annotationType reflect.Type, methodName string) annotation.Annotation {
return registry.GetMethodAnnotation(instanceType, annotationType, methodName) return registry.GetMethodAnnotation(instanceType, annotationType, methodName)
} }
@ -333,9 +359,14 @@ func parseFields(t reflect.Type, td *TypeDefinition) {
} }
parseFields(f.Type, td) parseFields(f.Type, td)
} else { } else {
if parseMethodAnnotation(&f, td) { if annotation.FieldAnnotationType == f.Type && parseFieldAnnotation(&f, td) {
continue continue
} }
if annotation.MethodAnnotationType == f.Type && parseMethodAnnotation(&f, td) {
continue
}
as, err := annotation.Parse(f.Tag) as, err := annotation.Parse(f.Tag)
if nil != err { if nil != err {
return return
@ -384,6 +415,26 @@ func parseTypeAnnotation(f *reflect.StructField, td *TypeDefinition) bool {
return false return false
} }
func parseFieldAnnotation(f *reflect.StructField, td *TypeDefinition) bool {
if annotation.FieldAnnotationType != f.Type {
return false
}
as, err := annotation.Parse(f.Tag)
if nil != err {
return false
}
if nil != as && 0 < len(as) {
if nil == td.FieldAnnotations {
td.FieldAnnotations = make(map[string]map[reflect.Type]annotation.Annotation, 0)
}
td.FieldAnnotations[f.Name[1:]] = as
return true
}
return false
}
func parseMethodAnnotation(f *reflect.StructField, td *TypeDefinition) bool { func parseMethodAnnotation(f *reflect.StructField, td *TypeDefinition) bool {
if annotation.MethodAnnotationType != f.Type { if annotation.MethodAnnotationType != f.Type {
return false return false

View File

@ -24,6 +24,7 @@ var ComponentAnnotationType = reflect.TypeOf((*ComponentAnnotation)(nil))
type ComponentAnnotation struct { type ComponentAnnotation struct {
annotation.TypeAnnotation `@annotation:"@Component"` annotation.TypeAnnotation `@annotation:"@Component"`
Name string `json:"name"` Name string `json:"name"`
InitMethod string `json:"initMethod"` // func (receiver interface{}, cr ComponentRegistry) error InitMethod string `json:"initMethod"` // func (receiver interface{}, cr ComponentRegistry) error
DestroyMethod string `json:"destroyMethod"` // func (receiver interface{}, cr ComponentRegistry) error DestroyMethod string `json:"destroyMethod"` // func (receiver interface{}, cr ComponentRegistry) error
@ -41,5 +42,6 @@ var ResourceAnnotationType = reflect.TypeOf((*ResourceAnnotation)(nil))
type ResourceAnnotation struct { type ResourceAnnotation struct {
annotation.TypeAnnotation `@annotation:"@Resource"` annotation.TypeAnnotation `@annotation:"@Resource"`
Name string `json:"name"` Name string `json:"name"`
} }