-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.go
195 lines (149 loc) · 3.52 KB
/
service.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package autonats
import (
"fmt"
"go/ast"
"log"
"path/filepath"
"regexp"
"strings"
"time"
)
const DocPrefix = "@nats:"
type Service struct {
InterfaceID string
Name string
Methods []*Method
Imports map[string]string
Basedir string
PackageName string
FileName string
}
func (svc *Service) combineImports(with []*ast.ImportSpec) {
imports := make(map[string]bool)
for _, m := range svc.Methods {
for k := range m.imports {
imports[k] = true
}
}
for k := range imports {
var name, path string
for _, i := range with {
if i.Name != nil {
if i.Name.Name == k {
name = i.Name.Name
path = i.Path.Value
break
}
} else {
valSplit := strings.Split(strings.ReplaceAll(i.Path.Value, "\"", ""), "/")
if valSplit[len(valSplit)-1] == k {
path = i.Path.Value
break
}
}
}
if path == "" {
panic("empty path!")
}
path = strings.ReplaceAll(path, "\"", "")
svc.Imports[path] = name
}
}
type ServiceConfig struct {
Name string
Timeout time.Duration
}
func ServiceConfigFromDoc(doc *ast.CommentGroup) ServiceConfig {
text := doc.Text()
rgx := regexp.MustCompile(fmt.Sprintf(`(?im)%s([a-z0-9-_]+)\s([a-z0-9-_]+)`, DocPrefix))
matches := rgx.FindAllSubmatch([]byte(text), -1)
args := make(map[string][]string)
for _, match := range matches {
if len(match) < 3 {
// line is too short to be ours
continue
}
ar := make([]string, len(match)-2)
key := string(match[1])
args[key] = ar
for ai, a := range match[2:] {
ar[ai] = string(a)
}
}
return ServiceConfig{
Name: args["server"][0],
Timeout: time.Second * 3,
}
}
func ServicesFromFile(pkgName, fileName string, file *ast.File) []*Service {
services := make([]*Service, 0)
ast.Inspect(file, func(node ast.Node) bool {
if node == nil {
return false
}
decl, ok, val := findServiceDecl(node)
if !ok {
return val
}
typeSpec, ok := decl.Specs[0].(*ast.TypeSpec)
if !ok {
log.Println("invalid spec type")
return false
}
iface, ok := findServiceIface(typeSpec)
if !ok {
return false
}
svcConfig := ServiceConfigFromDoc(decl.Doc)
methods := make([]*Method, iface.Methods.NumFields())
for i, m := range iface.Methods.List {
methods[i] = MethodFromField(m)
}
service := Service{
InterfaceID: typeSpec.Name.Name,
Name: svcConfig.Name,
Methods: methods,
Imports: make(map[string]string),
Basedir: filepath.Dir(fileName),
FileName: file.Name.Name,
PackageName: pkgName,
}
service.combineImports(file.Imports)
services = append(services, &service)
return true
})
return services
}
func findServiceDecl(node ast.Node) (decl *ast.GenDecl, ok, value bool) {
decl, ok = node.(*ast.GenDecl)
if !ok {
return nil, false, true
}
if decl.Doc == nil || !strings.Contains(decl.Doc.Text(), DocPrefix) {
return nil, false, false
}
if len(decl.Specs) != 1 {
log.Println("invalid number of specs")
return nil, false, false
}
return decl, true, false
}
func findServiceIface(typeSpec *ast.TypeSpec) (*ast.InterfaceType, bool) {
iface, ok := typeSpec.Type.(*ast.InterfaceType)
if !ok {
log.Println("couldn't find an interface")
return nil, false
}
if iface.Methods.NumFields() == 0 {
log.Println("interface has no methods")
return nil, false
}
return iface, true
}
func ServicesFromPkg(v *ast.Package) []*Service {
services := make([]*Service, 0)
for fk, fv := range v.Files {
services = append(services, ServicesFromFile(v.Name, fk, fv)...)
}
return services
}