-
Notifications
You must be signed in to change notification settings - Fork 75
/
policyconfig.go
127 lines (109 loc) · 2.93 KB
/
policyconfig.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package parse
import (
"go/ast"
"go/parser"
"go/token"
"path/filepath"
"strconv"
"strings"
)
import (
"github.com/pkg/errors"
)
import (
core_model "github.com/apache/dubbo-kubernetes/pkg/core/resources/model"
)
type PolicyConfig struct {
Package string
Name string
NameLower string
Plural string
SkipRegistration bool
SingularDisplayName string
PluralDisplayName string
Path string
AlternativeNames []string
GoModule string
}
func Policy(path string) (PolicyConfig, error) {
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, path, nil, parser.ParseComments)
if err != nil {
return PolicyConfig{}, err
}
policyName := strings.Split(filepath.Base(path), ".")[0]
var mainStruct *ast.TypeSpec
var mainComment *ast.CommentGroup
var packageName string
ast.Inspect(f, func(n ast.Node) bool {
if file, ok := n.(*ast.File); ok {
packageName = file.Name.String()
return true
}
if gd, ok := n.(*ast.GenDecl); ok && gd.Tok == token.TYPE {
for _, spec := range gd.Specs {
if strings.ToLower(spec.(*ast.TypeSpec).Name.String()) == policyName {
mainStruct = spec.(*ast.TypeSpec)
mainComment = gd.Doc
return false
}
}
return false
}
return false
})
markers, err := parseMarkers(mainComment)
if err != nil {
return PolicyConfig{}, err
}
return newPolicyConfig(packageName, mainStruct.Name.String(), markers)
}
func parseMarkers(cg *ast.CommentGroup) (map[string]string, error) {
result := map[string]string{}
for _, comment := range cg.List {
if !strings.HasPrefix(comment.Text, "// +") {
continue
}
trimmed := strings.TrimPrefix(comment.Text, "// +")
mrkr := strings.Split(trimmed, "=")
if len(mrkr) != 2 {
return nil, errors.Errorf("marker %s has wrong format", trimmed)
}
result[mrkr[0]] = mrkr[1]
}
return result, nil
}
func parseBool(markers map[string]string, key string) (bool, bool) {
if v, ok := markers[key]; ok {
vbool, err := strconv.ParseBool(v)
if err != nil {
return false, false
}
return vbool, true
}
return false, false
}
func newPolicyConfig(pkg, name string, markers map[string]string) (PolicyConfig, error) {
res := PolicyConfig{
Package: pkg,
Name: name,
NameLower: strings.ToLower(name),
SingularDisplayName: core_model.DisplayName(name),
PluralDisplayName: core_model.PluralType(core_model.DisplayName(name)),
AlternativeNames: []string{strings.ToLower(name)},
}
if v, ok := parseBool(markers, "dubbo:policy:skip_registration"); ok {
res.SkipRegistration = v
}
if v, ok := markers["dubbo:policy:singular_display_name"]; ok {
res.SingularDisplayName = v
res.PluralDisplayName = core_model.PluralType(v)
}
if v, ok := markers["dubbo:policy:plural"]; ok {
res.Plural = v
} else {
res.Plural = core_model.PluralType(res.Name)
}
res.Path = strings.ToLower(res.Plural)
return res, nil
}