-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinspect.go
245 lines (208 loc) · 6.29 KB
/
inspect.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package inspect
import (
"bytes"
"fmt"
"go/ast"
"go/parser"
"go/printer"
"go/token"
"os"
"path/filepath"
"strings"
)
// FilterIgnoreTests is a filter function for parser.ParseDir
// that ignores all test files.
var FilterIgnoreTests = func(info os.FileInfo) bool {
return !strings.HasSuffix(info.Name(), "_test.go")
}
// A FuncOption is used to decide whether exported and/or
// unexported functions get parsed.
type FuncOption int
const (
FuncUnexported FuncOption = 1 << iota
FuncExported
FuncBoth = FuncExported | FuncUnexported
)
// A Package describes a package.
//
// A Package contains a package name, a slice of the package's imports,
// and also a slice of all of the Function's that the package contains.
type Package struct {
Name string `json:"-"`
Imports []string `json:",omitempty"`
Funcs []*Function `json:",omitempty"`
Interfaces []*Interface `json:",omitempty"`
}
type Interface struct {
Name string `json:"Name"`
Methods []string `json:",omitempty"`
Interfaces []string `json:",omitempty"`
}
// A Function describes a function.
//
// A Function contains a function name, function signature
// and also the function's documentation.
type Function struct {
Name string `json:"Name"`
Signature string `json:"Sig"`
Documentation string `json:"Doc,omitempty"`
}
// IsExported is a wrapper around ast.IsExported that returns a true or false
// value based on whether the current function is exported or not.
func (f *Function) IsExported() bool {
return ast.IsExported(f.Name)
}
// ParsePackagesFromDir parses all packages in a directory.
//
// If ignoreTests is true, all test files will be ignored.
//
// If there are directories contained within dir, ParsePackagesFromDir
// attempts to traverse into those directories as well.
//
// If an error occurs whilst traversing the nested directories,
// ParsePackagesFromDir will return a map containing any correctly
// parsed packages and the error that occurred.
func ParsePackagesFromDir(dir string, ignoreTests bool, funcOption FuncOption) (map[string]*Package, error) {
fset := token.NewFileSet()
pkgs := make(map[string]*Package)
var filter func(os.FileInfo) bool
if ignoreTests {
filter = FilterIgnoreTests
}
return pkgs, filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() || strings.HasPrefix(path, filepath.Join(dir, "cmd")) {
return nil
}
parsed, err := parser.ParseDir(fset, path, filter, parser.ParseComments)
if err != nil {
return err
}
for _, pkg := range parsed {
p := ParsePackage(fset, pkg, funcOption)
if _, exists := pkgs[pkg.Name]; exists {
pkgs[pkg.Name].Funcs = append(pkgs[pkg.Name].Funcs, p.Funcs...)
} else {
pkgs[pkg.Name] = p
}
}
return nil
})
}
// ParsePackage returns a *Package generated from an *ast.Package.
func ParsePackage(fset *token.FileSet, pkg *ast.Package, funcOption FuncOption) *Package {
// Merge all of the package's files into a single file, and filter
// out any import or function duplicates along the way.
mergedFile := ast.MergePackageFiles(pkg,
ast.FilterFuncDuplicates+ast.FilterImportDuplicates,
)
// Return a new Package with it's fields appropriately set.
return &Package{
Name: pkg.Name,
Funcs: ParseFileFuncs(fset, mergedFile, funcOption),
Imports: ParseFileImports(mergedFile),
Interfaces: ParseFileInterfaces(fset, mergedFile),
}
}
// ParseFileFuncs returns a []*Function generated from an *ast.File.
func ParseFileFuncs(fset *token.FileSet, file *ast.File, funcOption FuncOption) []*Function {
funcs := []*Function{}
// If funcOption isn't set then parse both exported and unexported functions.
if funcOption&FuncUnexported == 0 && funcOption&FuncExported == 0 {
funcOption = FuncBoth
}
bb := new(bytes.Buffer)
ast.Inspect(file, func(n ast.Node) bool {
bb.Reset()
if fnc, ok := n.(*ast.FuncDecl); ok {
var f *Function
if funcOption&FuncUnexported != 0 && !fnc.Name.IsExported() {
f = ParseFunction(fset, fnc, bb)
}
if funcOption&FuncExported != 0 && fnc.Name.IsExported() {
f = ParseFunction(fset, fnc, bb)
}
if f != nil {
funcs = append(funcs, f)
}
}
return true
})
return funcs
}
// ParseFunction returns a []*Function generated from an *ast.FuncDecl.
func ParseFunction(fset *token.FileSet, fnc *ast.FuncDecl, bb *bytes.Buffer) *Function {
f := &Function{Name: fnc.Name.Name}
fnc.Body = nil
if err := printer.Fprint(bb, fset, fnc); err != nil {
return nil
}
var startPos int
if fnc.Doc.Text() != "" {
startPos = int(fnc.Type.Pos() - fnc.Doc.Pos())
}
f.Signature = bb.String()[startPos:]
f.Documentation = strings.TrimSpace(fnc.Doc.Text())
return f
}
// ParseFileImports generates a list of imports from an *ast.File object.
func ParseFileImports(file *ast.File) []string {
imports := []string{}
// Append the file's imports to the imports string slice.
for _, i := range file.Imports {
imports = append(imports, strings.Trim(i.Path.Value, "\""))
}
return imports
}
// ParseFileInterfaces generates a []*Interface from an *ast.File object.
func ParseFileInterfaces(fset *token.FileSet, file *ast.File) []*Interface {
ifaces := []*Interface{}
var bb bytes.Buffer
ast.Inspect(file, func(n ast.Node) bool {
decl, ok := n.(*ast.GenDecl)
if !ok {
return true
}
for _, spec := range decl.Specs {
ts, ok := spec.(*ast.TypeSpec)
if !ok {
continue
}
ifaceType, ok := ts.Type.(*ast.InterfaceType)
if !ok {
continue
}
iface := &Interface{Name: ts.Name.Name, Methods: []string{}}
list := ifaceType.Methods.List
for _, names := range list {
ident, ok := names.Type.(*ast.Ident)
if ok {
iface.Interfaces = append(iface.Interfaces, ident.Name)
}
sel, ok := names.Type.(*ast.SelectorExpr)
if ok {
printer.Fprint(&bb, fset, sel)
iface.Interfaces = append(iface.Interfaces, bb.String())
bb.Reset()
}
if len(names.Names) == 0 {
continue
}
fnc, ok := names.Type.(*ast.FuncType)
if ok {
printer.Fprint(&bb, fset, fnc)
sig := strings.Replace(
bb.String(), "func(", fmt.Sprintf("func %s(", names.Names[0].Name), 1,
)
iface.Methods = append(iface.Methods, sig)
bb.Reset()
}
}
ifaces = append(ifaces, iface)
}
return true
})
return ifaces
}