-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgodocx.go
232 lines (208 loc) · 5.84 KB
/
godocx.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
// Package godocx is the library for extracting Go documentation.
package godocx
import (
"errors"
"go/build"
"go/doc"
"go/parser"
"go/token"
"io/fs"
"regexp"
"time"
)
// Value is the documentation for a var or const declaration.
type Value struct {
Name string `json:"name"`
Doc string `json:"doc"`
Annotations []string `json:"annotations"`
}
// Type is the documentation for a type declaration.
type Type struct {
Name string `json:"name"`
Doc string `json:"doc"`
Annotations []string `json:"annotations"`
Consts []*Value `json:"consts"`
Vars []*Value `json:"vars"`
Funcs []*Func `json:"funcs"`
}
// Func is the documentation for a func declaration.
type Func struct {
Name string `json:"name"`
Doc string `json:"doc"`
Recv string `json:"recv"`
Orig string `json:"orig"`
Level int `json:"level"`
Annotations []string `json:"annotations"`
}
// A Note represents a marked comment starting with "MARKER(uid): note body".
// Any note with a marker of 2 or more upper case [A-Z] letters and a uid of
// at least one character is recognized. The ":" following the uid is optional.
type Note struct {
UID string `json:"uid"`
Body string `json:"body"`
}
// Package is the documentation for an entire package.
type Package struct {
Name string `json:"name"`
ImportPath string `json:"importPath"`
Notes map[string][]*Note `json:"notes"`
Consts []*Value `json:"consts"`
Vars []*Value `json:"vars"`
Funcs []*Func `json:"funcs"`
Types []*Type `json:"types"`
}
// DocEnvelope puts packages together.
// Last output of godocx should be this type.
type DocEnvelope struct {
Timestamp time.Time `json:"timestamp"`
Packages map[string]*Package `json:"packages"`
}
// New creates a new DocEnvelope from the given directory paths.
func New(dirPaths []string) (*DocEnvelope, error) {
pkgs := make(map[string]*Package, len(dirPaths))
for _, dirPath := range dirPaths {
pkg, err := newPackage(dirPath)
if err != nil {
return nil, err
}
pkgs[pkg.Name] = pkg
}
return &DocEnvelope{
Timestamp: time.Now(),
Packages: pkgs,
}, nil
}
// newPackage creates a new Package from the given package name and import path.
func newPackage(dirPath string) (*Package, error) {
// To search target files, use build.ImportDir.
buildPkg, err := build.ImportDir(dirPath, build.ImportComment)
if err != nil {
return nil, err
}
// include tells parser.ParseDir which files to include.
// That means the file must be in the build package's GoFiles or CgoFiles
// list only (no tag-ignored files, tests, swig or other non-Go files).
include := func(info fs.FileInfo) bool {
for _, name := range buildPkg.GoFiles {
if name == info.Name() {
return true
}
}
for _, name := range buildPkg.CgoFiles {
if name == info.Name() {
return true
}
}
return false
}
// analyze a package with ast.
fset := token.NewFileSet()
pkgMap, firstErr := parser.ParseDir(fset, dirPath, include, parser.ParseComments)
if firstErr != nil {
return nil, firstErr
}
// check the number of packages.
// ParseDir is given a func `include`, so the number of packages should be 1.
if len(pkgMap) == 0 {
return nil, errors.New("godocx: no packages found")
} else if len(pkgMap) > 1 {
return nil, errors.New("godocx: multiple packages found")
}
// analyze a package with doc.
d := doc.New(pkgMap[buildPkg.Name], ".", doc.AllDecls|doc.AllMethods)
// convert doc.Package to Package.
consts := make([]*Value, 0, len(d.Consts))
for _, v := range d.Consts {
consts = append(consts, &Value{
Name: v.Names[0],
Doc: v.Doc,
Annotations: extractAnnotations(v.Doc),
})
}
vars := make([]*Value, 0, len(d.Vars))
for _, v := range d.Vars {
vars = append(vars, &Value{
Name: v.Names[0],
Doc: v.Doc,
Annotations: extractAnnotations(v.Doc),
})
}
Funcs := make([]*Func, 0, len(d.Funcs))
for _, v := range d.Funcs {
Funcs = append(Funcs, &Func{
Name: v.Name,
Doc: v.Doc,
Recv: v.Recv,
Orig: v.Orig,
Level: v.Level,
Annotations: extractAnnotations(v.Doc),
})
}
types := make([]*Type, 0, len(d.Types))
for _, v := range d.Types {
cs := make([]*Value, 0, len(v.Consts))
for _, v := range v.Consts {
cs = append(cs, &Value{
Name: v.Names[0],
Doc: v.Doc,
Annotations: extractAnnotations(v.Doc),
})
}
vs := make([]*Value, 0, len(v.Vars))
for _, v := range v.Vars {
vs = append(vs, &Value{
Name: v.Names[0],
Doc: v.Doc,
Annotations: extractAnnotations(v.Doc),
})
}
fs := make([]*Func, 0, len(v.Funcs))
for _, v := range v.Funcs {
fs = append(fs, &Func{
Name: v.Name,
Doc: v.Doc,
Recv: v.Recv,
Orig: v.Orig,
Level: v.Level,
Annotations: extractAnnotations(v.Doc),
})
}
types = append(types, &Type{
Name: v.Name,
Doc: v.Doc,
Annotations: extractAnnotations(v.Doc),
Consts: cs,
Vars: vs,
Funcs: fs,
})
}
notes := make(map[string][]*Note, len(d.Notes))
for k, v := range d.Notes {
n := make([]*Note, 0, len(v))
for _, vv := range v {
n = append(n, &Note{
UID: vv.UID,
Body: vv.Body,
})
}
notes[k] = n
}
return &Package{
Name: d.Name,
ImportPath: d.ImportPath,
Notes: notes,
Consts: consts,
Vars: vars,
Funcs: Funcs,
Types: types,
}, nil
}
var annotationRegExp = regexp.MustCompile(`@(\w+)`)
// extractAnnotations extracts annotations from the comment.
func extractAnnotations(comment string) []string {
annotations := annotationRegExp.FindAllString(comment, -1)
if annotations == nil {
return []string{}
}
return annotations
}