From ee2ede6bd9ee485bce7891585c2a4473c1732add Mon Sep 17 00:00:00 2001 From: crusader Date: Thu, 5 Apr 2018 17:49:15 +0900 Subject: [PATCH] ing --- annotation/type.go | 4 ++++ registry/registry.go | 35 +++++++++++++++++++++++++++-------- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/annotation/type.go b/annotation/type.go index 28b3416..22ce1c4 100644 --- a/annotation/type.go +++ b/annotation/type.go @@ -1,5 +1,9 @@ package annotation +import "reflect" + +var TypeAnnotationType = reflect.TypeOf((*TypeAnnotation)(nil)).Elem() + type TypeAnnotation interface { Annotation } diff --git a/registry/registry.go b/registry/registry.go index f1be966..8cf8eda 100644 --- a/registry/registry.go +++ b/registry/registry.go @@ -266,9 +266,7 @@ func parseFields(t reflect.Type, td *TypeDefinition) { f := rt.Field(i) if f.Anonymous { - if parseAnonymousField(f, td) { - continue - } + parseAnonymousField(f, td) parseFields(f.Type, td) } else { 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 { - ct := reflect.TypeOf((*cda.TypeAnnotation)(nil)).Elem() +func parseAnonymousField(f reflect.StructField, td *TypeDefinition) { + parseTypeAnnotation(f, td) +} - if f.Type != ct { - return false +func parseTypeAnnotation(f reflect.StructField, td *TypeDefinition) { + if !haveEmbeddingOf(cda.TypeAnnotationType, f.Type) { + return } as, err := cda.ParseAnnotation(f.Tag) if nil != err { - return false + return } if nil != as && 0 < len(as) { td.TypeAnnotations = as + } +} + +func haveEmbeddingOf(t reflect.Type, target reflect.Type) bool { + if t == target { 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 }