This commit is contained in:
crusader 2018-04-05 17:49:15 +09:00
parent c40b3be3cc
commit ee2ede6bd9
2 changed files with 31 additions and 8 deletions

View File

@ -1,5 +1,9 @@
package annotation package annotation
import "reflect"
var TypeAnnotationType = reflect.TypeOf((*TypeAnnotation)(nil)).Elem()
type TypeAnnotation interface { type TypeAnnotation interface {
Annotation Annotation
} }

View File

@ -266,9 +266,7 @@ func parseFields(t reflect.Type, td *TypeDefinition) {
f := rt.Field(i) f := rt.Field(i)
if f.Anonymous { if f.Anonymous {
if parseAnonymousField(f, td) { parseAnonymousField(f, td)
continue
}
parseFields(f.Type, td) parseFields(f.Type, td)
} else { } else {
as, err := cda.ParseAnnotation(f.Tag) as, err := cda.ParseAnnotation(f.Tag)
@ -292,22 +290,43 @@ func parseFields(t reflect.Type, td *TypeDefinition) {
} }
} }
func parseAnonymousField(f reflect.StructField, td *TypeDefinition) bool { func parseAnonymousField(f reflect.StructField, td *TypeDefinition) {
ct := reflect.TypeOf((*cda.TypeAnnotation)(nil)).Elem() parseTypeAnnotation(f, td)
}
if f.Type != ct { func parseTypeAnnotation(f reflect.StructField, td *TypeDefinition) {
return false if !haveEmbeddingOf(cda.TypeAnnotationType, f.Type) {
return
} }
as, err := cda.ParseAnnotation(f.Tag) as, err := cda.ParseAnnotation(f.Tag)
if nil != err { if nil != err {
return false return
} }
if nil != as && 0 < len(as) { if nil != as && 0 < len(as) {
td.TypeAnnotations = as td.TypeAnnotations = as
}
}
func haveEmbeddingOf(t reflect.Type, target reflect.Type) bool {
if t == target {
return true return true
} }
rt, _, _ := cur.GetTypeInfo(target)
if reflect.Struct != rt.Kind() {
return false
}
for i := 0; i < rt.NumField(); i++ {
f := rt.Field(i)
if f.Anonymous {
if haveEmbeddingOf(t, f.Type) {
return true
}
}
}
return false return false
} }