refactoring
This commit is contained in:
parent
0d94b71049
commit
a3974dc218
15
registry.go
15
registry.go
|
@ -6,7 +6,6 @@ import (
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"git.loafle.net/loafer/annotation-go/annotations"
|
|
||||||
luReflect "git.loafle.net/loafer/util-go/reflect"
|
luReflect "git.loafle.net/loafer/util-go/reflect"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -25,7 +24,7 @@ type Regist interface {
|
||||||
// Register is method
|
// Register is method
|
||||||
Register(t reflect.Type) error
|
Register(t reflect.Type) error
|
||||||
// Parse is method
|
// Parse is method
|
||||||
Parse(tag reflect.StructTag) (map[reflect.Type]annotations.Annotation, error)
|
Parse(tag reflect.StructTag) (map[reflect.Type]Annotation, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Registry is struct
|
// Registry is struct
|
||||||
|
@ -71,12 +70,12 @@ func (r *Registry) Register(t reflect.Type) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse is method
|
// Parse is method
|
||||||
func Parse(tag reflect.StructTag) (map[reflect.Type]annotations.Annotation, error) {
|
func Parse(tag reflect.StructTag) (map[reflect.Type]Annotation, error) {
|
||||||
return registry.Parse(tag)
|
return registry.Parse(tag)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse is method
|
// Parse is method
|
||||||
func (r *Registry) Parse(tag reflect.StructTag) (map[reflect.Type]annotations.Annotation, error) {
|
func (r *Registry) Parse(tag reflect.StructTag) (map[reflect.Type]Annotation, error) {
|
||||||
s := strings.Trim(tag.Get(AnnotationTag), " ")
|
s := strings.Trim(tag.Get(AnnotationTag), " ")
|
||||||
if "" == s {
|
if "" == s {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
|
@ -91,7 +90,7 @@ func (r *Registry) Parse(tag reflect.StructTag) (map[reflect.Type]annotations.An
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
rKVs := make(map[reflect.Type]annotations.Annotation, 0)
|
rKVs := make(map[reflect.Type]Annotation, 0)
|
||||||
for name, attributes := range am {
|
for name, attributes := range am {
|
||||||
t, annotation, err := r.buildAnnotation(name, attributes)
|
t, annotation, err := r.buildAnnotation(name, attributes)
|
||||||
if nil != err {
|
if nil != err {
|
||||||
|
@ -113,7 +112,7 @@ func (r *Registry) getTypeAnnotationField(t reflect.Type) *reflect.StructField {
|
||||||
f := rt.Field(i)
|
f := rt.Field(i)
|
||||||
|
|
||||||
if f.Anonymous {
|
if f.Anonymous {
|
||||||
if f.Type == annotations.TypeAnnotationType {
|
if f.Type == TypeAnnotationType {
|
||||||
return &f
|
return &f
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -154,14 +153,14 @@ func (r *Registry) splitAnnotation(s string) (map[string]string, error) {
|
||||||
return ss, nil
|
return ss, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Registry) buildAnnotation(name string, attributes string) (reflect.Type, annotations.Annotation, error) {
|
func (r *Registry) buildAnnotation(name string, attributes string) (reflect.Type, Annotation, error) {
|
||||||
def, ok := r.definitions[name]
|
def, ok := r.definitions[name]
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, nil, fmt.Errorf("There is no annotation[%s]", name)
|
return nil, nil, fmt.Errorf("There is no annotation[%s]", name)
|
||||||
}
|
}
|
||||||
|
|
||||||
v := reflect.New(def.rt)
|
v := reflect.New(def.rt)
|
||||||
i := v.Interface().(annotations.Annotation)
|
i := v.Interface().(Annotation)
|
||||||
|
|
||||||
if "" != attributes {
|
if "" != attributes {
|
||||||
_json := fmt.Sprintf("{%s}", attributes)
|
_json := fmt.Sprintf("{%s}", attributes)
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package annotation
|
package annotation
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"git.loafle.net/loafer/annotation-go/annotations"
|
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
@ -70,7 +69,7 @@ func TestParse(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
args args
|
args args
|
||||||
want map[reflect.Type]annotations.Annotation
|
want map[reflect.Type]Annotation
|
||||||
wantErr bool
|
wantErr bool
|
||||||
}{
|
}{
|
||||||
// TODO: Add test cases.
|
// TODO: Add test cases.
|
||||||
|
@ -100,7 +99,7 @@ func TestRegistry_Parse(t *testing.T) {
|
||||||
name string
|
name string
|
||||||
fields fields
|
fields fields
|
||||||
args args
|
args args
|
||||||
want map[reflect.Type]annotations.Annotation
|
want map[reflect.Type]Annotation
|
||||||
wantErr bool
|
wantErr bool
|
||||||
}{
|
}{
|
||||||
// TODO: Add test cases.
|
// TODO: Add test cases.
|
||||||
|
@ -222,7 +221,7 @@ func TestRegistry_buildAnnotation(t *testing.T) {
|
||||||
fields fields
|
fields fields
|
||||||
args args
|
args args
|
||||||
want reflect.Type
|
want reflect.Type
|
||||||
want1 annotations.Annotation
|
want1 Annotation
|
||||||
wantErr bool
|
wantErr bool
|
||||||
}{
|
}{
|
||||||
// TODO: Add test cases.
|
// TODO: Add test cases.
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package annotations
|
package annotation
|
||||||
|
|
||||||
import "reflect"
|
import "reflect"
|
||||||
|
|
Loading…
Reference in New Issue
Block a user