di-go/annotation/meta.go

76 lines
1.5 KiB
Go
Raw Normal View History

2018-04-03 09:02:31 +00:00
package annotation
import (
"reflect"
"strings"
)
type AnnotationFieldMeta struct {
fieldName string
options anotationMetaOptions
}
type anotationMetaOptions string
func (o anotationMetaOptions) Contains(optionName string) bool {
if len(o) == 0 {
return false
}
s := string(o)
for s != "" {
var next string
i := strings.Index(s, AnnotationAttributeSpliter)
if i >= 0 {
s, next = s[:i], s[i+1:]
}
if s == optionName {
return true
}
s = next
}
return false
}
2019-11-11 14:41:01 +00:00
// func getMetaFields(t reflect.Type) map[string]*AnnotationFieldMeta {
// fields := make(map[string]*AnnotationFieldMeta, 0)
// rt, _, _ := cur.GetTypeInfo(t)
// if reflect.Struct != rt.Kind() {
// return fields
// }
2018-04-03 09:02:31 +00:00
2019-11-11 14:41:01 +00:00
// for i := 0; i < rt.NumField(); i++ {
// f := rt.Field(i)
2018-04-03 09:02:31 +00:00
2019-11-11 14:41:01 +00:00
// if f.Anonymous {
// pFields := getMetaFields(f.Type)
// for k, v := range pFields {
// fields[k] = v
// }
// } else {
// name, metaOptions := parseAnnotationMeta(f.Tag)
// if "" == name {
// continue
// }
// fields[name] = &AnnotationFieldMeta{
// fieldName: f.Name,
// options: metaOptions,
// }
// }
// }
2018-04-03 09:02:31 +00:00
2019-11-11 14:41:01 +00:00
// return fields
// }
2018-04-03 09:02:31 +00:00
func parseAnnotationMeta(tag reflect.StructTag) (string, anotationMetaOptions) {
s := strings.Trim(tag.Get(AnnotationMetaTag), " ")
if "" == s {
return "", ""
}
if idx := strings.Index(s, AnnotationAttributeSpliter); idx != -1 {
return s[:idx], anotationMetaOptions(s[idx+1:])
}
return s, anotationMetaOptions("")
}