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