2019-11-12 14:58:17 +00:00
|
|
|
package annotation
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
|
2019-11-13 13:49:55 +00:00
|
|
|
"git.loafle.net/loafer/annotation-go/annotations"
|
2019-11-12 14:58:17 +00:00
|
|
|
luReflect "git.loafle.net/loafer/util-go/reflect"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
registry = newRegistry()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Definition is struct
|
|
|
|
type Definition struct {
|
|
|
|
t reflect.Type
|
|
|
|
rt reflect.Type
|
|
|
|
}
|
|
|
|
|
|
|
|
// Regist is interface
|
|
|
|
type Regist interface {
|
|
|
|
// Register is method
|
2019-11-13 13:39:48 +00:00
|
|
|
Register(t reflect.Type) error
|
2019-11-12 14:58:17 +00:00
|
|
|
// Parse is method
|
2019-11-13 13:49:55 +00:00
|
|
|
Parse(tag reflect.StructTag) (map[reflect.Type]annotations.Annotation, error)
|
2019-11-12 14:58:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Registry is struct
|
|
|
|
type Registry struct {
|
|
|
|
definitions map[string]*Definition
|
|
|
|
}
|
|
|
|
|
|
|
|
var registry Regist
|
|
|
|
|
|
|
|
func newRegistry() Regist {
|
|
|
|
r := &Registry{}
|
|
|
|
r.definitions = make(map[string]*Definition, 0)
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register is method
|
2019-11-13 13:39:48 +00:00
|
|
|
func Register(t reflect.Type) error {
|
|
|
|
return registry.Register(t)
|
2019-11-12 14:58:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Register is method
|
2019-11-13 13:39:48 +00:00
|
|
|
func (r *Registry) Register(t reflect.Type) error {
|
2019-11-12 14:58:17 +00:00
|
|
|
rt, _, _ := luReflect.GetTypeInfo(t)
|
|
|
|
f := r.getTypeAnnotationField(t)
|
|
|
|
if nil == f {
|
2019-11-13 13:39:48 +00:00
|
|
|
return fmt.Errorf("Annotation: This type[%s] is not Annotation. use TypeAnnotation", rt.Name())
|
2019-11-12 14:58:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
name := r.parseAnnotationMeta(f.Tag)
|
|
|
|
|
|
|
|
if _, ok := r.definitions[name]; ok {
|
2019-11-13 13:39:48 +00:00
|
|
|
return fmt.Errorf("Annotation: name[%s] of annotation exist already", name)
|
2019-11-12 14:58:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
def := &Definition{
|
|
|
|
t: t,
|
|
|
|
rt: rt,
|
|
|
|
}
|
|
|
|
|
|
|
|
r.definitions[name] = def
|
2019-11-13 13:39:48 +00:00
|
|
|
return nil
|
2019-11-12 14:58:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parse is method
|
2019-11-13 13:49:55 +00:00
|
|
|
func Parse(tag reflect.StructTag) (map[reflect.Type]annotations.Annotation, error) {
|
2019-11-12 14:58:17 +00:00
|
|
|
return registry.Parse(tag)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse is method
|
2019-11-13 13:49:55 +00:00
|
|
|
func (r *Registry) Parse(tag reflect.StructTag) (map[reflect.Type]annotations.Annotation, error) {
|
2019-11-12 14:58:17 +00:00
|
|
|
s := strings.Trim(tag.Get(AnnotationTag), " ")
|
|
|
|
if "" == s {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2019-11-13 13:49:55 +00:00
|
|
|
am, err := r.splitAnnotation(s)
|
2019-11-12 14:58:17 +00:00
|
|
|
if nil != err {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-11-13 13:49:55 +00:00
|
|
|
if nil == am || 0 == len(am) {
|
2019-11-12 14:58:17 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2019-11-13 13:49:55 +00:00
|
|
|
rKVs := make(map[reflect.Type]annotations.Annotation, 0)
|
|
|
|
for name, attributes := range am {
|
2019-11-12 14:58:17 +00:00
|
|
|
t, annotation, err := r.buildAnnotation(name, attributes)
|
|
|
|
if nil != err {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rKVs[t] = annotation
|
|
|
|
}
|
|
|
|
|
|
|
|
return rKVs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Registry) getTypeAnnotationField(t reflect.Type) *reflect.StructField {
|
|
|
|
rt, _, _ := luReflect.GetTypeInfo(t)
|
|
|
|
if reflect.Struct != rt.Kind() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < rt.NumField(); i++ {
|
|
|
|
f := rt.Field(i)
|
|
|
|
|
|
|
|
if f.Anonymous {
|
2019-11-13 13:49:55 +00:00
|
|
|
if f.Type == annotations.TypeAnnotationType {
|
2019-11-12 14:58:17 +00:00
|
|
|
return &f
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Registry) parseAnnotationMeta(tag reflect.StructTag) string {
|
|
|
|
return strings.Trim(tag.Get(AnnotationMetaTag), " ")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Registry) splitAnnotation(s string) (map[string]string, error) {
|
|
|
|
ss := make(map[string]string, 0)
|
|
|
|
ts := s
|
|
|
|
for {
|
|
|
|
as := strings.Index(ts, AnnotationChar)
|
|
|
|
if -1 == as {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
aas := strings.Index(ts, AnnotationStartChar)
|
|
|
|
aae := strings.Index(ts, AnnotationEndChar)
|
|
|
|
|
|
|
|
aName := ts[as:aas]
|
|
|
|
aAttributes := ts[aas+1 : aae]
|
|
|
|
|
|
|
|
if 0 < len(aAttributes) {
|
|
|
|
ss[aName] = aAttributes
|
|
|
|
} else {
|
|
|
|
ss[aName] = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(ts) <= (aae + 1) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
ts = ts[aae+1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
return ss, nil
|
|
|
|
}
|
|
|
|
|
2019-11-13 13:49:55 +00:00
|
|
|
func (r *Registry) buildAnnotation(name string, attributes string) (reflect.Type, annotations.Annotation, error) {
|
2019-11-12 14:58:17 +00:00
|
|
|
def, ok := r.definitions[name]
|
|
|
|
if !ok {
|
|
|
|
return nil, nil, fmt.Errorf("There is no annotation[%s]", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
v := reflect.New(def.rt)
|
2019-11-13 13:49:55 +00:00
|
|
|
i := v.Interface().(annotations.Annotation)
|
2019-11-12 14:58:17 +00:00
|
|
|
|
|
|
|
if "" != attributes {
|
|
|
|
_json := fmt.Sprintf("{%s}", attributes)
|
|
|
|
if err := json.Unmarshal([]byte(_json), i); nil != err {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return def.t, i, nil
|
|
|
|
}
|