-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
275 lines (246 loc) · 7.03 KB
/
api.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
package main
import (
"bytes"
"fmt"
"go/constant"
"go/types"
"sort"
"strings"
)
var constStr = map[constant.Kind]string{
constant.Bool: "bool",
constant.String: "string",
constant.Int: "int",
constant.Float: "float",
constant.Complex: "complex",
}
func panicf(format string, v ...interface{}) {
panic(fmt.Sprintf(format, v...))
}
// packagePath returns the full, non-vendored package path of the package.
func packagePath(pkg *types.Package) string {
const vendor = "/vendor/"
p := pkg.Path()
i := strings.LastIndex(p, vendor)
if i < 0 {
return p
}
return p[i+len(vendor):]
}
func formatAPI(pkg *types.Package, info *types.Info, buff *bytes.Buffer) {
p := &printer{
pkgPath: packagePath(pkg),
}
for _, def := range info.Defs {
if def == nil || !def.Exported() {
continue
}
// Only consider objects at the package scope.
if def.Parent() != nil && def.Parent() != pkg.Scope() {
continue
}
p.formatObj(def)
}
p.write(buff)
}
// ignoreRecv determines if we should ignored a delcared method based on the receiver's
// type. For example, we should ignore any methods on interfaces since they're printed
// when navigating the interface. We should also ignore any exported methods on unexported
// structs.
func ignoreRecv(recv types.Type) bool {
switch t := recv.(type) {
case *types.Named:
if _, ok := t.Underlying().(*types.Interface); ok {
// Ignore all methods with an interface receiver.
return true
}
// Determine if the underlying type is exported.
return !t.Obj().Exported()
case *types.Pointer:
// recurse
return ignoreRecv(t.Elem())
}
return false
}
// isRecvInterface determines if a receiver is an interface type. Methods on interfaces
// are populated at the top level package declarations, so we need to be able to filter
// them out.
func isRecvInterface(recv *types.Var) bool {
named, ok := recv.Type().(*types.Named)
if !ok {
return false
}
_, ok = named.Underlying().(*types.Interface)
return ok
}
type printer struct {
// lines to be printed out to the user.
lines []string
// Package that's being printed. This lets us omit the package name for
// types within the package. For example if we're in package "a" ensure
// types are printed as "T" and not "a.T"
pkgPath string
}
// formatObj processes the given object, adding lines to the printer that represent that
// object's public API signature.
func (p *printer) formatObj(o types.Object) {
switch o := o.(type) {
case *types.Const:
p.printf("const %s %s", o.Name(), constStr[o.Val().Kind()])
case *types.Var:
if o.IsField() {
// Field on a struct. Ignore since this is captured when walking the strut.
return
}
p.printf("var %s %s", o.Name(), p.formatType(o.Type().Underlying()))
case *types.TypeName:
switch u := o.Type().Underlying().(type) {
case *types.Struct:
p.printf("type %s struct", o.Name())
for i := 0; i < u.NumFields(); i++ {
f := u.Field(i)
if !f.Exported() {
continue
}
p.printf("type %s struct, %s %s", o.Name(), f.Name(), p.formatType(f.Type()))
}
case *types.Interface:
var methodNames []string
for i := 0; i < u.NumMethods(); i++ {
f := u.Method(i)
if !f.Exported() {
continue
}
// Per package documentation, Type() of a Func is always a Signature.
sig := f.Type().(*types.Signature)
p.printf("type %s interface, %s%s", o.Name(), f.Name(), p.formatSignature(sig))
methodNames = append(methodNames, f.Name())
}
if len(methodNames) == 0 {
p.printf("type %s interface {}", o.Name())
} else {
sort.Strings(methodNames)
p.printf("type %s interface { %s }", o.Name(), strings.Join(methodNames, ", "))
}
default:
p.printf("type %s %s", o.Name(), p.formatType(u))
}
case *types.Func:
// Per package documentation, Type() of a Func is always a Signature.
sig := o.Type().(*types.Signature)
if rec := sig.Recv(); rec != nil {
if ignoreRecv(rec.Type()) {
// Receiver is an interface value or unexported.
return
}
p.printf("method (%s) %s%s", p.formatType(rec.Type()), o.Name(), p.formatSignature(sig))
} else {
p.printf("func %s%s", o.Name(), p.formatSignature(sig))
}
default:
panicf("unexpected type %T %s", o, o)
}
}
func (p *printer) printf(format string, v ...interface{}) {
p.lines = append(p.lines, fmt.Sprintf(format, v...))
}
// formatType compactly represents a types signature.
func (p *printer) formatType(t types.Type) string {
switch t := t.(type) {
case *types.Basic:
return t.String()
case *types.Struct:
return t.String()
case *types.Named:
// TODO: Handle paths with "vendor" in them.
typeName := t.Obj()
if typeName.Pkg() == nil || packagePath(typeName.Pkg()) == p.pkgPath {
// builtin type without a package like "error" or a type in
// the package we're currently printing.
return typeName.Name()
}
return packagePath(typeName.Pkg()) + "." + typeName.Name()
case *types.Pointer:
return "*" + p.formatType(t.Elem())
case *types.Slice:
return "[]" + p.formatType(t.Elem())
case *types.Array:
return fmt.Sprintf("[%d]%s", t.Len(), p.formatType(t.Elem()))
case *types.Signature:
// Since this isn't the top level definition, there wont be a receiver.
return "func" + p.formatSignature(t)
case *types.Interface:
return t.String()
case *types.Map:
return "map[" + p.formatType(t.Key()) + "]" + p.formatType(t.Elem())
case *types.Chan:
switch t.Dir() {
case types.SendRecv:
return "chan " + p.formatType(t.Elem())
case types.SendOnly:
return "chan <- " + p.formatType(t.Elem())
case types.RecvOnly:
return "<- chan " + p.formatType(t.Elem())
}
default:
panicf("unexpected type %T %s", t, t)
}
return ""
}
// formatSignature formats the arguments and return values of a function
// without inspecting the receiver or name. Example results include:
//
// (string, string) error
// (string) (bool, error)
// (string) (*url.URL, error)
//
func (p *printer) formatSignature(sig *types.Signature) string {
sigStr := "("
params := sig.Params()
// Does this signature end with a "..." argument?
variadic := sig.Variadic()
if params != nil {
for i := 0; i < params.Len(); i++ {
if i != 0 {
sigStr += ", "
}
if variadic && i == params.Len()-1 {
// If function is variadic and this is the last parameter it MUST be
// of type "*types.Slice", right?
sigStr += "..." + p.formatType(params.At(i).Type().(*types.Slice).Elem())
} else {
sigStr += p.formatType(params.At(i).Type())
}
}
}
sigStr += ")"
r := sig.Results()
if r != nil {
switch r.Len() {
case 0:
case 1:
// Special case a single result tuple and don't surround with ().
sigStr += " " + p.formatType(r.At(0).Type())
default:
sigStr += " ("
for i := 0; i < r.Len(); i++ {
if i != 0 {
sigStr += ", "
}
sigStr += p.formatType(r.At(i).Type())
}
sigStr += ")"
}
}
return sigStr
}
func (p *printer) write(buff *bytes.Buffer) {
sort.Strings(p.lines)
for _, line := range p.lines {
buff.WriteString("pkg ")
buff.WriteString(p.pkgPath)
buff.WriteString(", ")
buff.WriteString(line)
buff.WriteRune('\n')
}
}