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