81 lines
1.6 KiB
Go
81 lines
1.6 KiB
Go
|
package annotation
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"reflect"
|
||
|
"strings"
|
||
|
|
||
|
"git.loafle.net/commons_go/di"
|
||
|
)
|
||
|
|
||
|
const (
|
||
|
AnnotationStartChar = "("
|
||
|
AnnotationEndChar = ")"
|
||
|
AnnotationAttributeSpliter = ","
|
||
|
AnnotationKeyValueSpliter = "="
|
||
|
)
|
||
|
|
||
|
type Annotation interface {
|
||
|
}
|
||
|
|
||
|
// @Inject(name? string)
|
||
|
// @Resource(name? string)
|
||
|
func ParseAnnotation(tag reflect.StructTag) (map[string]Annotation, error) {
|
||
|
a := strings.Trim(tag.Get(di.AnnotationTag), " ")
|
||
|
if "" == a {
|
||
|
return nil, nil
|
||
|
}
|
||
|
|
||
|
rKVs := make(map[string]Annotation)
|
||
|
|
||
|
inject, err := ParseInject(a)
|
||
|
if nil != err {
|
||
|
return nil, err
|
||
|
}
|
||
|
rKVs[InjectTag] = inject
|
||
|
|
||
|
resource, err := ParseResource(a)
|
||
|
if nil != err {
|
||
|
return nil, err
|
||
|
}
|
||
|
rKVs[ResourceTag] = resource
|
||
|
|
||
|
return rKVs, nil
|
||
|
}
|
||
|
|
||
|
func ParseAttribute(a string, startIndex int) (map[string]string, error) {
|
||
|
if startIndex >= len(a) {
|
||
|
return nil, nil
|
||
|
}
|
||
|
|
||
|
if AnnotationStartChar != string([]rune(a)[startIndex]) {
|
||
|
return nil, nil
|
||
|
}
|
||
|
|
||
|
endIndex := strings.Index(a[startIndex+1:], AnnotationEndChar)
|
||
|
if -1 == endIndex {
|
||
|
return nil, fmt.Errorf("DI: Syntax error - annotation(%s) has '%s', but '%s' is not exist", a, AnnotationStartChar, AnnotationEndChar)
|
||
|
}
|
||
|
endIndex = endIndex + startIndex + 1
|
||
|
|
||
|
body := a[startIndex+1 : endIndex]
|
||
|
kvs := strings.Split(body, AnnotationAttributeSpliter)
|
||
|
if 0 == len(kvs) {
|
||
|
return nil, nil
|
||
|
}
|
||
|
|
||
|
rKVs := make(map[string]string)
|
||
|
for i := 0; i < len(kvs); i++ {
|
||
|
s := strings.Trim(kvs[i], " ")
|
||
|
if "" == s {
|
||
|
continue
|
||
|
}
|
||
|
kv := strings.Split(s, AnnotationKeyValueSpliter)
|
||
|
k := strings.Trim(kv[0], " ")
|
||
|
v := strings.Trim(kv[1], " ")
|
||
|
rKVs[k] = v
|
||
|
}
|
||
|
|
||
|
return rKVs, nil
|
||
|
}
|