This repository has been archived by the owner on Mar 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
godocjson.go
330 lines (304 loc) · 9.63 KB
/
godocjson.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package main
import (
"encoding/json"
"flag"
"fmt"
"go/ast"
"go/doc"
"go/parser"
"go/token"
"log"
"os"
"regexp"
"strings"
)
// Func represents a function declaration.
type Func struct {
Doc string `json:"doc"`
Name string `json:"name"`
PackageName string `json:"packageName"`
PackageImportPath string `json:"packageImportPath"`
Type string `json:"type"`
Filename string `json:"filename"`
Line int `json:"line"`
Params []FuncParam `json:"parameters"`
Results []FuncParam `json:"results"`
// methods
// (for functions, these fields have the respective zero value)
Recv string `json:"recv"` // actual receiver "T" or "*T"
Orig string `json:"orig"` // original receiver "T" or "*T"
// Level int // embedding level; 0 means not embedded
}
// Package represents a package declaration.
type Package struct {
Type string `json:"type"`
Doc string `json:"doc"`
Name string `json:"name"`
ImportPath string `json:"importPath"`
Imports []string `json:"imports"`
Filenames []string `json:"filenames"`
Notes map[string][]*Note `json:"notes"`
// DEPRECATED. For backward compatibility Bugs is still populated,
// but all new code should use Notes instead.
Bugs []string `json:"bugs"`
// declarations
Consts []*Value `json:"consts"`
Types []*Type `json:"types"`
Vars []*Value `json:"vars"`
Funcs []*Func `json:"funcs"`
}
// Note represents a note comment.
type Note struct {
Pos token.Pos `json:"pos"`
End token.Pos `json:"end"` // position range of the comment containing the marker
UID string `json:"uid"` // uid found with the marker
Body string `json:"body"` // note body text
}
// Type represents a type declaration.
type Type struct {
PackageName string `json:"packageName"`
PackageImportPath string `json:"packageImportPath"`
Doc string `json:"doc"`
Name string `json:"name"`
Type string `json:"type"`
Filename string `json:"filename"`
Line int `json:"line"`
// Decl *ast.GenDecl
// associated declarations
Consts []*Value `json:"consts"` // sorted list of constants of (mostly) this type
Vars []*Value `json:"vars"` // sorted list of variables of (mostly) this type
Funcs []*Func `json:"funcs"` // sorted list of functions returning this type
Methods []*Func `json:"methods"` // sorted list of methods (including embedded ones) of this type
}
// Value represents a value declaration.
type Value struct {
PackageName string `json:"packageName"`
PackageImportPath string `json:"packageImportPath"`
Doc string `json:"doc"`
Names []string `json:"names"` // var or const names in declaration order
Type string `json:"type"`
Filename string `json:"filename"`
Line int `json:"line"`
// Decl *ast.GenDecl
}
// FuncParam represents a parameter to a function.
type FuncParam struct {
Type string `json:"type"`
Name string `json:"name"`
}
func typeOf(x interface{}) string {
switch x := x.(type) {
case *ast.Ident:
return x.String()
case *ast.ArrayType:
return "[]" + typeOf(x.Elt)
case *ast.Field:
return x.Names[0].Name + " " + typeOf(x.Type)
case *ast.StructType:
fields := make([]string, x.Fields.NumFields())
for i, f := range x.Fields.List {
fields[i] = typeOf(f.Type)
}
return fmt.Sprintf("struct{%s}", strings.Join(fields, ","))
case *ast.InterfaceType:
methods := make([]string, x.Methods.NumFields())
for i, m := range x.Methods.List {
methods[i] = typeOf(m.Type)
}
return fmt.Sprintf("interface{%s}", strings.Join(methods, ","))
case *ast.SelectorExpr:
return typeOf(x.X) + "." + x.Sel.Name
case *ast.Ellipsis:
return "..." + typeOf(x.Elt)
case *ast.StarExpr:
return "*" + typeOf(x.X)
case *ast.FuncType:
params := make([]string, x.Params.NumFields())
for i, p := range x.Params.List {
params[i] = typeOf(p.Type)
}
var results []string
if x.Results != nil {
results = make([]string, x.Results.NumFields())
for i, r := range x.Results.List {
results[i] = typeOf(r.Type)
}
}
return fmt.Sprintf("func(%s)%s", strings.Join(params, ","), strings.Join(results, ","))
case *ast.MapType:
return fmt.Sprintf("map [%s]%s", typeOf(x.Key), typeOf(x.Value))
case *ast.ChanType:
if x.Dir == ast.SEND {
return fmt.Sprintf("chan<- %s", typeOf(x.Value))
} else if x.Dir == ast.RECV {
return fmt.Sprintf("<-chan %s", typeOf(x.Value))
} else {
return fmt.Sprintf("chan %s", typeOf(x.Value))
}
default:
panic(fmt.Sprintf("Unknown type %+v", x))
}
}
func processFuncDecl(d *ast.FuncDecl, fun *Func) {
fun.Params = make([]FuncParam, 0)
for _, f := range d.Type.Params.List {
t := typeOf(f.Type)
for _, name := range f.Names {
fun.Params = append(fun.Params, FuncParam{
Type: t,
Name: name.String(),
})
}
}
fun.Results = make([]FuncParam, 0)
if d.Type.Results != nil {
for _, f := range d.Type.Results.List {
t := typeOf(f.Type)
if len(f.Names) == 0 {
// For case func foo() Type
fun.Results = append(fun.Results, FuncParam{
Type: t,
})
} else {
// For case func foo() (name, name Type)
for _, name := range f.Names {
fun.Results = append(fun.Results, FuncParam{
Type: t,
Name: name.String(),
})
}
}
}
}
}
// CopyFuncs produces a json-annotated array of Func objects from an array of GoDoc Func objects.
func CopyFuncs(f []*doc.Func, packageName string, packageImportPath string, fileSet *token.FileSet) []*Func {
newFuncs := make([]*Func, len(f))
for i, n := range f {
position := fileSet.Position(n.Decl.Pos())
newFuncs[i] = &Func{
Doc: n.Doc,
Name: n.Name,
PackageName: packageName,
PackageImportPath: packageImportPath,
Type: "func",
Orig: n.Orig,
Recv: n.Recv,
Filename: position.Filename,
Line: position.Line,
}
processFuncDecl(n.Decl, newFuncs[i])
}
return newFuncs
}
// CopyValues produces a json-annotated array of Value objects from an array of GoDoc Value objects.
func CopyValues(c []*doc.Value, packageName string, packageImportPath string, fileSet *token.FileSet) []*Value {
newConsts := make([]*Value, len(c))
for i, c := range c {
position := fileSet.Position(c.Decl.TokPos)
newConsts[i] = &Value{
Doc: c.Doc,
Names: c.Names,
PackageName: packageName,
PackageImportPath: packageImportPath,
Type: c.Decl.Tok.String(),
Filename: position.Filename,
Line: position.Line,
}
}
return newConsts
}
// CopyPackage produces a json-annotated Package object from a GoDoc Package object.
func CopyPackage(pkg *doc.Package, fileSet *token.FileSet) Package {
newPkg := Package{
Type: "package",
Doc: pkg.Doc,
Name: pkg.Name,
ImportPath: pkg.ImportPath,
Imports: pkg.Imports,
Filenames: pkg.Filenames,
Bugs: pkg.Bugs,
}
newPkg.Notes = map[string][]*Note{}
for key, value := range pkg.Notes {
notes := make([]*Note, len(value))
for i, note := range value {
notes[i] = &Note{
Pos: note.Pos,
End: note.End,
UID: note.UID,
Body: note.Body,
}
}
newPkg.Notes[key] = notes
}
newPkg.Consts = CopyValues(pkg.Consts, pkg.Name, pkg.ImportPath, fileSet)
newPkg.Funcs = CopyFuncs(pkg.Funcs, pkg.Name, pkg.ImportPath, fileSet)
newPkg.Types = make([]*Type, len(pkg.Types))
for i, t := range pkg.Types {
newPkg.Types[i] = &Type{
Name: t.Name,
PackageName: pkg.Name,
PackageImportPath: pkg.ImportPath,
Type: "type",
Consts: CopyValues(t.Consts, pkg.Name, pkg.ImportPath, fileSet),
Doc: t.Doc,
Funcs: CopyFuncs(t.Funcs, pkg.Name, pkg.ImportPath, fileSet),
Methods: CopyFuncs(t.Methods, pkg.Name, pkg.ImportPath, fileSet),
Vars: CopyValues(t.Vars, pkg.Name, pkg.ImportPath, fileSet),
}
}
newPkg.Vars = CopyValues(pkg.Vars, pkg.Name, pkg.ImportPath, fileSet)
return newPkg
}
// Building filter function that can be used with parser.ParseDir
func GetExcludeFilter(re string) func(os.FileInfo) bool {
if re != "" {
return func(info os.FileInfo) bool {
matched, err := regexp.MatchString(re, info.Name())
if err != nil {
panic(err)
} else {
return !matched
}
}
}
// Returning nil by default results no filtering
return nil
}
func GetUsageText() {
log.Println("Usage of godocjson:")
log.Println("godocjson [-e] target_directory")
flag.PrintDefaults()
}
func main() {
var filter_regexp string
// Disable timestamps inside the log file as we will just use it as wrapper
// around stderr for now.
log.SetFlags(0)
flag.Usage = GetUsageText
flag.StringVar(&filter_regexp,"e", "", "Regex filter for excluding source files")
flag.Parse()
directory := flag.Arg(0)
if directory == "" {
flag.Usage()
log.Fatal("Fatal: Please specify a target_directory.", )
}
fileSet := token.NewFileSet()
pkgs, firstError := parser.ParseDir(fileSet, directory, GetExcludeFilter(filter_regexp), parser.ParseComments|parser.AllErrors)
if firstError != nil {
panic(firstError)
}
if len(pkgs) > 1 {
panic("Multiple packages found in directory!\n")
}
for _, pkg := range pkgs {
docPkg := doc.New(pkg, directory, 0)
cleanedPkg := CopyPackage(docPkg, fileSet)
pkgJSON, err := json.MarshalIndent(cleanedPkg, "", " ")
if err != nil {
log.Fatal("Failed to encode JSON: %s", err)
}
fmt.Printf("%s\n", pkgJSON)
}
}