/
interceptor.go
68 lines (59 loc) · 1.73 KB
/
interceptor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package astutils
import (
"go/ast"
"github.com/go-park/sandwich/pkg/aspect"
"github.com/go-park/sandwich/pkg/tools/collections"
)
type (
ProxyInterceptor func([]Annotation, aspect.Proxy, *ast.StructType) []aspect.ProxyOption
FieldInterceptor func([]Annotation, aspect.Field, *ast.Field) []aspect.FieldOption
)
func init() {
proxyInterceptors = append(proxyInterceptors, proxyParamProcess)
}
var (
proxyInterceptors []ProxyInterceptor
fieldInterceptors []FieldInterceptor
)
func RegisterProxyInterceptors(opts ...ProxyInterceptor) {
proxyInterceptors = append(proxyInterceptors, opts...)
}
func RegisterFieldInterceptors(opts ...FieldInterceptor) {
fieldInterceptors = append(fieldInterceptors, opts...)
}
func proxyParamProcess(ann []Annotation, pro aspect.Proxy, t *ast.StructType) (result []aspect.ProxyOption) {
params := GetCommentParam(pro.Docs(), CommentProxy)
// proxy object
abstract := params[CommentKeyAbstract]
if v, ok := params[CommentKeyDefault]; ok {
abstract = v
}
// factory method suffix
suffix := DefaultProxySuffix
if v, ok := params[CommentKeySuffix]; ok {
suffix = v
}
// factory method option
option := params[CommentKeyOption]
// is singleton
var singleton bool
s := params[CommentKeySingleton]
if s == "true" {
singleton = true
}
result = append(result,
aspect.WithProxyAbstract(abstract),
aspect.WithProxySuffix(suffix),
aspect.WithProxyOption(option),
aspect.WithProxyMode(singleton),
)
var pos []aspect.Pointcut
if collections.Contains(ann, CommentPointcut) {
params := GetCommentParam(pro.Docs(), CommentPointcut)
for _, v := range params {
pos = append(pos, aspect.NewPointcut(aspect.WithPointcutName(v)))
}
}
result = append(result, aspect.WithProxyPointcuts(pos...))
return
}