-
Notifications
You must be signed in to change notification settings - Fork 32
/
interface.go
190 lines (181 loc) · 4.57 KB
/
interface.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
package interfaces
import (
"errors"
"fmt"
"go/types"
"sort"
"unicode"
"golang.org/x/tools/go/loader"
)
// Interface represents a typed interface.
type Interface []Func
// New builds an interface definition for a type specified by the query.
// Supported query format is "package".Type (similar to what gorename
// tool accepts).
//
// The function expects sources for the requested type to be present
// in current GOPATH.
func New(query string) (Interface, error) {
q, err := ParseQuery(query)
if err != nil {
return nil, errors.New("invalid query: " + err.Error())
}
opts := &Options{
Query: q,
}
return NewWithOptions(opts)
}
// NewWithOptions builds an interface definition for a type specified by
// the given Options.
//
// The Options may be used to specify e.g. different GOPATH if sources
// for requested type are not available in the current one.
func NewWithOptions(opts *Options) (Interface, error) {
if opts == nil || opts.Query == nil {
panic("interfacer: called NewWithOptions with nil Options or nil Query")
}
if err := opts.Query.valid(); err != nil {
return nil, errors.New("invalid query: " + err.Error())
}
return buildInterface(opts)
}
// Deps gives a list of packages the interface depends on.
func (i Interface) Deps() []string {
pkgs := make(map[string]struct{})
for _, fn := range i {
for _, pkg := range fn.Deps() {
pkgs[pkg] = struct{}{}
}
}
if len(pkgs) == 0 {
return nil
}
deps := make([]string, 0, len(pkgs))
for pkg := range pkgs {
deps = append(deps, pkg)
}
sort.Strings(deps)
return deps
}
func buildInterface(opts *Options) (Interface, error) {
cfg := &loader.Config{
AllowErrors: true,
Build: opts.context(),
ImportPkgs: map[string]bool{opts.Query.Package: true},
TypeCheckFuncBodies: func(string) bool { return false },
}
cfg.ImportWithTests(opts.Query.Package)
prog, err := cfg.Load()
if err != nil {
return nil, err
}
pkg, ok := prog.Imported[opts.Query.Package]
if !ok {
return nil, fmt.Errorf("parsing successful, but package %q not found",
opts.Query.Package)
}
i, err := buildInterfaceForPkg(pkg, opts)
if err == nil {
return i, nil
}
// If a requested type is defined in an external test package try to
// build the interface using it before returning an error.
queryCopy := *opts.Query
queryCopy.Package += "_test"
optsCopy := *opts
optsCopy.Query = &queryCopy
for _, pkg := range prog.Created {
if pkg.Pkg.Path() == optsCopy.Query.Package {
return buildInterfaceForPkg(pkg, &optsCopy)
}
}
return nil, err
}
func buildInterfaceForPkg(pkg *loader.PackageInfo, opts *Options) (Interface, error) {
var typ *types.Named
for _, obj := range pkg.Defs {
if obj == nil {
continue
}
if obj.Name() != opts.Query.TypeName || obj.Pkg().Path() != opts.Query.Package {
continue
}
tmp, ok := obj.Type().(*types.Named)
if !ok {
continue
}
if tmp.Obj() == obj {
typ = tmp
break
}
}
if typ == nil {
return nil, notFoundErr(opts)
}
var inter Interface
var methods = make(map[string]*types.Func)
collectMethods(methods, typ, 0, nil)
for _, method := range methods {
// TODO(rjeczalik): read rune
if unicode.IsLower(rune(method.Name()[0])) && !opts.Unexported {
continue
}
sig, ok := method.Type().(*types.Signature)
if !ok {
continue
}
ins := sig.Params()
outs := sig.Results()
fn := Func{
Name: method.Name(),
Ins: make([]Type, ins.Len()),
Outs: make([]Type, outs.Len()),
IsVariadic: sig.Variadic(),
}
for i := range fn.Ins {
fn.Ins[i] = newType(ins.At(i))
fixup(&fn.Ins[i], opts.Query)
}
for i := range fn.Outs {
fn.Outs[i] = newType(outs.At(i))
fixup(&fn.Outs[i], opts.Query)
}
inter = append(inter, fn)
}
if len(inter) == 0 {
return nil, notFoundErr(opts)
}
sort.Sort(funcs(inter))
return inter, nil
}
func collectMethods(methods map[string]*types.Func, typ *types.Named, depth int, orig types.Type) {
if orig == nil {
orig = typ
}
// TODO(rjeczalik): recursive types support
if depth > 128 {
panic("recursive types not supported: " + orig.String())
}
for i := 0; i < typ.NumMethods(); i++ {
method := typ.Method(i)
if _, ok := methods[method.Name()]; ok {
continue
}
methods[method.Name()] = method
}
if typ, ok := typ.Underlying().(*types.Struct); ok {
for i := 0; i < typ.NumFields(); i++ {
field := typ.Field(i)
if !field.Anonymous() {
continue
}
typ := field.Type()
if p, ok := typ.(*types.Pointer); ok {
typ = p.Elem()
}
if named, ok := typ.(*types.Named); ok {
collectMethods(methods, named, depth+1, orig)
}
}
}
}