annotation-go/registry.go

478 lines
12 KiB
Go
Raw Permalink Normal View History

2018-08-22 09:36:13 +00:00
package annotation
import (
"encoding/json"
"fmt"
"reflect"
"strings"
2018-08-23 06:13:22 +00:00
our "git.loafle.net/overflow/util-go/reflect"
2018-09-04 03:19:41 +00:00
yaml "gopkg.in/yaml.v2"
2018-08-22 09:36:13 +00:00
)
type Registry interface {
Register(t reflect.Type) error
2018-08-23 06:13:22 +00:00
GetTypeAnnotation(t reflect.Type, at reflect.Type) (Annotation, error)
GetTypeAnnotations(t reflect.Type) (map[reflect.Type]Annotation, error)
GetFieldAnnotation(t reflect.Type, name string, at reflect.Type) (Annotation, error)
GetFieldAnnotations(t reflect.Type, name string) (map[reflect.Type]Annotation, error)
2018-09-04 06:32:53 +00:00
GetFieldAnnotationsByType(t reflect.Type, at reflect.Type) (map[string]Annotation, error)
2018-08-23 06:13:22 +00:00
GetAllFieldAnnotations(t reflect.Type) (map[string]map[reflect.Type]Annotation, error)
GetMethodAnnotation(t reflect.Type, name string, at reflect.Type) (Annotation, error)
GetMethodAnnotations(t reflect.Type, name string) (map[reflect.Type]Annotation, error)
2018-09-04 06:32:53 +00:00
GetMethodAnnotationsByType(t reflect.Type, at reflect.Type) (map[string]Annotation, error)
2018-08-23 06:13:22 +00:00
GetAllMethodAnnotations(t reflect.Type) (map[string]map[reflect.Type]Annotation, error)
2018-08-22 09:36:13 +00:00
}
func New(parent Registry) Registry {
r := &AnnotationRegistry{
2018-08-23 06:13:22 +00:00
parent: parent,
definitions: make(map[string]*Definition, 0),
typeDefinitions: make(map[reflect.Type]*TypeDefinition, 0),
2018-08-22 09:36:13 +00:00
}
if nil == r.parent {
r.parent = SystemRegistry
}
return r
}
2018-08-22 10:15:12 +00:00
var SystemRegistry = &AnnotationRegistry{
2018-08-23 06:13:22 +00:00
parent: nil,
definitions: make(map[string]*Definition, 0),
typeDefinitions: make(map[reflect.Type]*TypeDefinition, 0),
2018-08-22 10:15:12 +00:00
}
2018-08-22 09:36:13 +00:00
type AnnotationRegistry struct {
2018-08-23 06:13:22 +00:00
parent Registry
definitions map[string]*Definition
typeDefinitions map[reflect.Type]*TypeDefinition
2018-08-22 09:36:13 +00:00
}
func Register(t reflect.Type) error {
return SystemRegistry.Register(t)
}
func (r *AnnotationRegistry) Register(t reflect.Type) error {
2018-08-23 06:13:22 +00:00
rt, _, _ := our.GetTypeInfo(t)
2018-08-22 09:36:13 +00:00
fields := findAnnotatedFields(t, AnnotationType, false)
switch len(fields) {
case 0:
return fmt.Errorf("type[%s] is not Annotation", rt.Name())
case 1:
default:
return fmt.Errorf("type[%s] have only one Annotation", rt.Name())
}
f := fields[AnnotationName]
name := strings.TrimSpace(f.Tag.Get(NameTag))
if "" == name {
return fmt.Errorf("annotation name of type[%s] is not valid", rt.Name())
}
if _, ok := r.definitions[name]; ok {
return fmt.Errorf("name[%s] of annotation exist already", name)
}
r.definitions[name] = &Definition{
t: t,
rt: rt,
}
return nil
}
2018-08-23 06:13:22 +00:00
func findAnnotatedFields(t reflect.Type, ft reflect.Type, deep bool) map[string]*reflect.StructField {
fields := make(map[string]*reflect.StructField, 0)
rt, _, _ := our.GetTypeInfo(t)
if reflect.Struct != rt.Kind() {
return fields
}
LOOP:
for i := 0; i < rt.NumField(); i++ {
f := rt.Field(i)
if f.Anonymous {
if f.Type == ft {
fields[f.Name] = &f
continue LOOP
}
if deep {
_fields := findAnnotatedFields(f.Type, ft, deep)
for _n, _f := range _fields {
fields[_n] = _f
}
}
}
}
return fields
2018-08-22 09:36:13 +00:00
}
2018-08-23 06:13:22 +00:00
func (r *AnnotationRegistry) getAnnotation(f *reflect.StructField) (map[reflect.Type]Annotation, error) {
2018-08-22 09:36:13 +00:00
annotations := make(map[reflect.Type]Annotation, 0)
tag := strings.TrimSpace(f.Tag.Get(AnnotationTag))
if "" == tag {
return annotations, nil
}
if !AnnotationRGX.MatchString(tag) {
return nil, fmt.Errorf("Tag of annotation[%s] is not match", tag)
}
rss := AnnotationRGX.FindAllStringSubmatch(tag, -1)
if nil == rss || 0 == len(rss) {
return annotations, nil
}
for _, rs := range rss {
if 3 != len(rs) {
return nil, fmt.Errorf("Tag of annotation[%s] is not valid", rs[0])
}
name := fmt.Sprintf("@%s", strings.TrimSpace(rs[1]))
2018-08-23 09:28:10 +00:00
body := strings.TrimSpace(rs[2])
2018-08-22 09:36:13 +00:00
def, ok := r.definitions[name]
if !ok {
return nil, fmt.Errorf("annotation[%s] is not exist", name)
}
v := reflect.New(def.rt)
i := v.Interface()
2018-08-23 09:28:10 +00:00
if "" != body {
if !AnnotationBodyRGX.MatchString(body) {
return nil, fmt.Errorf("Body[%s] of annotation[%s] is not valid", body, name)
}
body = AnnotationBodyRGX.ReplaceAllStringFunc(body, func(token string) string {
switch len(token) {
case 0, 1, 2:
return "\"\""
default:
return strings.Replace(fmt.Sprintf("\"%s\"", token[1:len(token)-1]), "\\'", "'", -1)
}
})
body = fmt.Sprintf("{%s}", strings.TrimSpace(body))
err := json.Unmarshal([]byte(body), i)
if nil != err {
return nil, fmt.Errorf("Unmarshal failed %v", err)
}
2018-08-22 09:36:13 +00:00
}
2018-09-04 03:19:41 +00:00
_v := reflect.Indirect(reflect.ValueOf(i))
for index := 0; index < _v.NumField(); index++ {
_f := _v.Field(index)
_fs := def.rt.Field(index)
if !_f.CanAddr() || !_f.CanInterface() {
continue
}
if isBlank := reflect.DeepEqual(_f.Interface(), reflect.Zero(_f.Type()).Interface()); isBlank {
// Set default configuration if blank
if value := _fs.Tag.Get(DefaultTag); value != "" {
if err := yaml.Unmarshal([]byte(value), _f.Addr().Interface()); err != nil {
return nil, fmt.Errorf("Unmarshal failed %v", err)
}
} else if _fs.Tag.Get(RequiredTag) == "true" {
// return error if it is required but blank
return nil, fmt.Errorf("%s is required, but blank", _fs.Name)
}
}
}
2018-08-22 09:36:13 +00:00
annotations[def.t] = i
}
return annotations, nil
}
2018-08-23 06:13:22 +00:00
func (r *AnnotationRegistry) getTypeDefinition(t reflect.Type) (*TypeDefinition, error) {
rt, _, _ := our.GetTypeInfo(t)
2018-08-22 09:36:13 +00:00
if reflect.Struct != rt.Kind() {
2018-08-23 06:13:22 +00:00
return nil, fmt.Errorf("type[%s] is not struct", rt.Name())
}
td, ok := r.typeDefinitions[t]
if ok {
return td, nil
}
td = &TypeDefinition{
t: t,
rt: rt,
2018-08-22 09:36:13 +00:00
}
LOOP:
for i := 0; i < rt.NumField(); i++ {
f := rt.Field(i)
if f.Anonymous {
2018-08-23 06:13:22 +00:00
if f.Type == TypeAnnotationType {
if 0 < len(td.typeAnnotation) {
return nil, fmt.Errorf("TypeAnnotation of type[%s] be defined one more time", rt.Name())
}
as, err := r.getAnnotation(&f)
if nil != err {
return nil, err
}
if 0 == len(as) {
continue LOOP
}
td.typeAnnotation = as
2018-08-22 09:36:13 +00:00
}
2018-08-23 06:13:22 +00:00
} else {
if f.Type == MethodAnnotationType {
as, err := r.getAnnotation(&f)
if nil != err {
return nil, err
}
if 0 == len(as) {
continue LOOP
}
if 0 == len(td.methodAnnotation) {
td.methodAnnotation = make(map[string]map[reflect.Type]Annotation, 0)
2018-08-22 09:36:13 +00:00
}
2018-09-04 01:15:34 +00:00
_name := strings.TrimLeft(f.Name, MethodAnnotationPrefix)
if "" == _name {
return nil, fmt.Errorf("name[%s] of method annotation is not valid", _name)
}
td.methodAnnotation[_name] = as
2018-08-23 06:13:22 +00:00
continue LOOP
} else {
as, err := r.getAnnotation(&f)
if nil != err {
return nil, err
}
if 0 == len(as) {
continue LOOP
}
if 0 == len(td.fieldAnnotation) {
td.fieldAnnotation = make(map[string]map[reflect.Type]Annotation, 0)
}
td.fieldAnnotation[f.Name] = as
continue LOOP
2018-08-22 09:36:13 +00:00
}
}
}
2018-08-23 06:13:22 +00:00
r.typeDefinitions[t] = td
return td, nil
}
func GetTypeAnnotation(t reflect.Type, at reflect.Type) (Annotation, error) {
return SystemRegistry.GetTypeAnnotation(t, at)
}
func (r *AnnotationRegistry) GetTypeAnnotation(t reflect.Type, at reflect.Type) (Annotation, error) {
td, err := r.getTypeDefinition(t)
if nil != err {
return nil, err
}
if 0 == len(td.typeAnnotation) {
2018-08-23 08:01:01 +00:00
return nil, nil
2018-08-23 06:13:22 +00:00
}
a, ok := td.typeAnnotation[at]
if !ok {
2018-08-23 08:01:01 +00:00
return nil, nil
2018-08-23 06:13:22 +00:00
}
return a, nil
}
func GetTypeAnnotations(t reflect.Type) (map[reflect.Type]Annotation, error) {
return SystemRegistry.GetTypeAnnotations(t)
}
func (r *AnnotationRegistry) GetTypeAnnotations(t reflect.Type) (map[reflect.Type]Annotation, error) {
td, err := r.getTypeDefinition(t)
if nil != err {
return nil, err
}
if 0 == len(td.typeAnnotation) {
2018-08-23 08:01:01 +00:00
return nil, nil
2018-08-23 06:13:22 +00:00
}
return td.typeAnnotation, nil
}
func GetFieldAnnotation(t reflect.Type, name string, at reflect.Type) (Annotation, error) {
return SystemRegistry.GetFieldAnnotation(t, name, at)
}
func (r *AnnotationRegistry) GetFieldAnnotation(t reflect.Type, name string, at reflect.Type) (Annotation, error) {
td, err := r.getTypeDefinition(t)
if nil != err {
return nil, err
}
if 0 == len(td.fieldAnnotation) {
2018-08-23 08:01:01 +00:00
return nil, nil
2018-08-23 06:13:22 +00:00
}
as, ok := td.fieldAnnotation[name]
if !ok || 0 == len(as) {
2018-08-23 08:01:01 +00:00
return nil, nil
2018-08-23 06:13:22 +00:00
}
a, ok := as[at]
if !ok {
2018-08-23 08:01:01 +00:00
return nil, nil
2018-08-23 06:13:22 +00:00
}
return a, nil
}
func GetFieldAnnotations(t reflect.Type, name string) (map[reflect.Type]Annotation, error) {
return SystemRegistry.GetFieldAnnotations(t, name)
}
func (r *AnnotationRegistry) GetFieldAnnotations(t reflect.Type, name string) (map[reflect.Type]Annotation, error) {
td, err := r.getTypeDefinition(t)
if nil != err {
return nil, err
}
if 0 == len(td.fieldAnnotation) {
2018-08-23 08:01:01 +00:00
return nil, nil
2018-08-23 06:13:22 +00:00
}
as, ok := td.fieldAnnotation[name]
if !ok || 0 == len(as) {
2018-08-23 08:01:01 +00:00
return nil, nil
2018-08-23 06:13:22 +00:00
}
return as, nil
}
2018-09-04 06:32:53 +00:00
func GetFieldAnnotationsByType(t reflect.Type, at reflect.Type) (map[string]Annotation, error) {
return SystemRegistry.GetFieldAnnotationsByType(t, at)
}
func (r *AnnotationRegistry) GetFieldAnnotationsByType(t reflect.Type, at reflect.Type) (map[string]Annotation, error) {
td, err := r.getTypeDefinition(t)
if nil != err {
return nil, err
}
if 0 == len(td.fieldAnnotation) {
return nil, nil
}
as := make(map[string]Annotation, 0)
for k, v := range td.fieldAnnotation {
if a, ok := v[at]; ok {
as[k] = a
}
}
return as, nil
}
2018-08-23 06:13:22 +00:00
func GetAllFieldAnnotations(t reflect.Type) (map[string]map[reflect.Type]Annotation, error) {
return SystemRegistry.GetAllFieldAnnotations(t)
}
func (r *AnnotationRegistry) GetAllFieldAnnotations(t reflect.Type) (map[string]map[reflect.Type]Annotation, error) {
td, err := r.getTypeDefinition(t)
if nil != err {
return nil, err
}
if 0 == len(td.fieldAnnotation) {
2018-08-23 08:01:01 +00:00
return nil, nil
2018-08-23 06:13:22 +00:00
}
return td.fieldAnnotation, nil
}
func GetMethodAnnotation(t reflect.Type, name string, at reflect.Type) (Annotation, error) {
return SystemRegistry.GetMethodAnnotation(t, name, at)
}
func (r *AnnotationRegistry) GetMethodAnnotation(t reflect.Type, name string, at reflect.Type) (Annotation, error) {
td, err := r.getTypeDefinition(t)
if nil != err {
return nil, err
}
if 0 == len(td.methodAnnotation) {
2018-08-23 08:01:01 +00:00
return nil, nil
2018-08-23 06:13:22 +00:00
}
as, ok := td.methodAnnotation[name]
if !ok || 0 == len(as) {
2018-08-23 08:01:01 +00:00
return nil, nil
2018-08-23 06:13:22 +00:00
}
a, ok := as[at]
if !ok {
2018-08-23 08:01:01 +00:00
return nil, nil
2018-08-23 06:13:22 +00:00
}
return a, nil
}
func GetMethodAnnotations(t reflect.Type, name string) (map[reflect.Type]Annotation, error) {
return SystemRegistry.GetMethodAnnotations(t, name)
}
func (r *AnnotationRegistry) GetMethodAnnotations(t reflect.Type, name string) (map[reflect.Type]Annotation, error) {
td, err := r.getTypeDefinition(t)
if nil != err {
return nil, err
}
if 0 == len(td.methodAnnotation) {
2018-08-23 08:01:01 +00:00
return nil, nil
2018-08-23 06:13:22 +00:00
}
as, ok := td.methodAnnotation[name]
if !ok || 0 == len(as) {
2018-08-23 08:01:01 +00:00
return nil, nil
2018-08-23 06:13:22 +00:00
}
return as, nil
}
2018-09-04 06:32:53 +00:00
func GetMethodAnnotationsByType(t reflect.Type, at reflect.Type) (map[string]Annotation, error) {
return SystemRegistry.GetMethodAnnotationsByType(t, at)
}
func (r *AnnotationRegistry) GetMethodAnnotationsByType(t reflect.Type, at reflect.Type) (map[string]Annotation, error) {
td, err := r.getTypeDefinition(t)
if nil != err {
return nil, err
}
if 0 == len(td.methodAnnotation) {
return nil, nil
}
as := make(map[string]Annotation, 0)
for k, v := range td.methodAnnotation {
if a, ok := v[at]; ok {
as[k] = a
}
}
return as, nil
}
2018-08-23 06:13:22 +00:00
func GetAllMethodAnnotations(t reflect.Type) (map[string]map[reflect.Type]Annotation, error) {
return SystemRegistry.GetAllMethodAnnotations(t)
}
func (r *AnnotationRegistry) GetAllMethodAnnotations(t reflect.Type) (map[string]map[reflect.Type]Annotation, error) {
td, err := r.getTypeDefinition(t)
if nil != err {
return nil, err
}
if 0 == len(td.methodAnnotation) {
2018-08-23 08:01:01 +00:00
return nil, nil
2018-08-23 06:13:22 +00:00
}
return td.methodAnnotation, nil
2018-08-22 09:36:13 +00:00
}