-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
224 lines (208 loc) · 5.42 KB
/
parser.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
package main
import (
"errors"
"fmt"
"go/ast"
"go/build"
"go/parser"
"go/token"
"io/ioutil"
"os"
"strings"
)
type fileParser struct {
vendor string
fset *token.FileSet
typeMap map[string]*ast.TypeSpec
nodes []nodePair
endpoints map[string][]endpoint
}
type nodePair struct {
Node *ast.File
Dir string
}
type param struct {
Name string
Type string
Location string
Description string
}
func newParser(files []filePair, vendor string) (fp fileParser, err error) {
fp.vendor = vendor
fp.fset = token.NewFileSet()
fp.typeMap = make(map[string]*ast.TypeSpec)
fp.endpoints = make(map[string][]endpoint)
visited := make(map[string]bool)
var node *ast.File
for _, file := range files {
node, err = fp.inspetNode(file, "", visited)
if err != nil {
return
}
fp.nodes = append(fp.nodes, nodePair{
Node: node,
Dir: file.Dir,
})
}
return
}
func (fp *fileParser) inspetNode(file filePair, importName string, visited map[string]bool) (node *ast.File, err error) {
node, err = parser.ParseFile(fp.fset, file.FileName, nil, parser.ParseComments)
if err != nil {
return
}
var files []filePair
for _, i := range node.Imports {
name := i.Path.Value[1 : len(i.Path.Value)-1]
if _, ok := visited[name]; !ok {
visited[name] = true
files, err = fp.readPackage(name)
if err != nil {
err = fmt.Errorf("error reading package %s: %v", name, err)
return
}
for _, file := range files {
parts := strings.Split(name, "/")
_, err = fp.inspetNode(file, parts[len(parts)-1], visited)
}
}
}
ast.Inspect(node, func(n ast.Node) bool {
// Find Return Statements
ret, ok := n.(*ast.TypeSpec)
if ok {
if len(importName) > 0 {
fp.typeMap[fmt.Sprintf("%s.%s", importName, ret.Name.Name)] = ret
return true
}
fp.typeMap[ret.Name.Name] = ret
return true
}
return true
})
return
}
func (fp *fileParser) readPackage(packagePath string) (files []filePair, err error) {
fileInfo, dir, err := fp.getFilesFromGOROOT(packagePath)
if err != nil {
fileInfo, dir, err = fp.getFilesFromVendor(packagePath)
if err != nil {
fileInfo, dir, err = fp.getFilesFromGOPATH(packagePath)
if err != nil {
err = fmt.Errorf("error reading package %s: %v", packagePath, err)
return
}
}
}
for _, f := range fileInfo {
if strings.HasSuffix(f.Name(), ".go") {
parts := strings.Split(dir, "/")
files = append(files, filePair{
FileName: fmt.Sprintf("%s/%s", dir, f.Name()),
Dir: parts[len(parts)-1],
})
}
}
return
}
func (fp *fileParser) getFilesFromGOROOT(packagePath string) (fileInfo []os.FileInfo, dir string, err error) {
goroot := os.Getenv("GOROOT")
if goroot == "" {
goroot = build.Default.GOROOT
}
dir = fmt.Sprintf("%s/src/%s", goroot, packagePath)
fileInfo, err = ioutil.ReadDir(dir)
return
}
func (fp *fileParser) getFilesFromGOPATH(packagePath string) (fileInfo []os.FileInfo, dir string, err error) {
gopath := os.Getenv("GOPATH")
if gopath == "" {
gopath = build.Default.GOPATH
}
dir = fmt.Sprintf("%s/src/%s", gopath, packagePath)
fileInfo, err = ioutil.ReadDir(dir)
return
}
func (fp *fileParser) getFilesFromVendor(packagePath string) (fileInfo []os.FileInfo, dir string, err error) {
if len(fp.vendor) < 1 {
err = errors.New("Vendor dir not set")
return
}
dir = fmt.Sprintf("%s/%s", fp.vendor, packagePath)
fileInfo, err = ioutil.ReadDir(dir)
return
}
func (fp *fileParser) parseComments() (err error) {
for _, np := range fp.nodes {
for _, f := range np.Node.Decls {
fn, ok := f.(*ast.FuncDecl)
if !ok {
continue
}
if ep, valid := fp.parseComment(fn.Doc.Text()); valid {
if _, ok := fp.endpoints[np.Dir]; !ok {
fp.endpoints[np.Dir] = make([]endpoint, 0)
}
fp.endpoints[np.Dir] = append(fp.endpoints[np.Dir], ep)
}
}
}
return
}
func (fp *fileParser) parseComment(text string) (ep endpoint, valid bool) {
lines := strings.Split(text, "\n")
for _, l := range lines {
ep.parseLine(l, fp)
}
if ep.Route != "" {
valid = true
}
return
}
func (fp *fileParser) createMarkdownDocumentation() (err error) {
file, err := os.Create("documentation.md")
if err != nil {
return
}
defer file.Close()
for name, dir := range fp.endpoints {
//TODO pretty format this
//TODO add flag to make this optional
file.Write([]byte(fmt.Sprintf("# %s\n", strings.TrimPrefix(name, "./"))))
for _, ep := range dir {
var method string
if len(ep.Method) > 0 {
method = fmt.Sprintf("[%s]", ep.Method)
}
if len(ep.Name) > 0 {
file.Write([]byte(fmt.Sprintf("## %s\n\n", ep.Name)))
file.Write([]byte(fmt.Sprintf("### %s %s\n\n", ep.Route, method)))
} else {
file.Write([]byte(fmt.Sprintf("## %s %s\n\n", ep.Route, method)))
}
if len(ep.Description) > 0 {
file.Write([]byte(fmt.Sprintf("%s\n\n", ep.Description)))
}
if len(ep.Returns) > 0 {
file.Write([]byte(">Returns:\n\n"))
file.Write([]byte(fmt.Sprintf("```Go\n%s\n```\n\n", ep.Returns)))
}
if len(ep.Params) > 0 {
file.Write([]byte("### Params\n\n"))
file.Write([]byte("Name | Type | Location\n"))
file.Write([]byte("---- | ---- | --------\n"))
for _, p := range ep.Params {
file.Write([]byte(fmt.Sprintf("%s | %s | %s\n", p.Name, p.Type, p.Location)))
}
file.Write([]byte("\n"))
}
}
}
return
}
func (fp *fileParser) createDocumentation(opts options) (err error) {
if opts.HTML {
return fp.createHTMLDocumentation(opts.Title)
}
return fp.createMarkdownDocumentation()
}