-
-
Notifications
You must be signed in to change notification settings - Fork 126
/
registry.go
190 lines (165 loc) · 4.66 KB
/
registry.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 registry
import (
"errors"
"fmt"
"go/types"
"path/filepath"
"sort"
"strings"
"golang.org/x/tools/go/packages"
)
// Registry encapsulates types information for the source and mock
// destination package. For the mock package, it tracks the list of
// imports and ensures there are no conflicts in the imported package
// qualifiers.
type Registry struct {
srcPkg *packages.Package
moqPkgPath string
aliases map[string]string
imports map[string]*Package
}
// New loads the source package info and returns a new instance of
// Registry.
func New(srcDir, moqPkg string) (*Registry, error) {
srcPkg, err := pkgInfoFromPath(
srcDir, packages.NeedName|packages.NeedSyntax|packages.NeedTypes|packages.NeedTypesInfo,
)
if err != nil {
return nil, fmt.Errorf("couldn't load source package: %s", err)
}
return &Registry{
srcPkg: srcPkg,
moqPkgPath: findPkgPath(moqPkg, srcPkg),
aliases: parseImportsAliases(srcPkg),
imports: make(map[string]*Package),
}, nil
}
// SrcPkg returns the types info for the source package.
func (r Registry) SrcPkg() *types.Package {
return r.srcPkg.Types
}
// SrcPkgName returns the name of the source package.
func (r Registry) SrcPkgName() string {
return r.srcPkg.Name
}
// LookupInterface returns the underlying interface definition of the
// given interface name.
func (r Registry) LookupInterface(name string) (*types.Interface, error) {
obj := r.SrcPkg().Scope().Lookup(name)
if obj == nil {
return nil, fmt.Errorf("interface not found: %s", name)
}
if !types.IsInterface(obj.Type()) {
return nil, fmt.Errorf("%s (%s) is not an interface", name, obj.Type())
}
return obj.Type().Underlying().(*types.Interface).Complete(), nil
}
// MethodScope returns a new MethodScope.
func (r *Registry) MethodScope() *MethodScope {
return &MethodScope{
registry: r,
moqPkgPath: r.moqPkgPath,
conflicted: map[string]bool{},
}
}
// AddImport adds the given package to the set of imports. It generates a
// suitable alias if there are any conflicts with previously imported
// packages.
func (r *Registry) AddImport(pkg *types.Package) *Package {
path := stripVendorPath(pkg.Path())
if path == r.moqPkgPath {
return nil
}
if imprt, ok := r.imports[path]; ok {
return imprt
}
imprt := Package{pkg: pkg, Alias: r.aliases[path]}
if conflict, ok := r.searchImport(imprt.Qualifier()); ok {
resolveImportConflict(&imprt, conflict, 0)
}
r.imports[path] = &imprt
return &imprt
}
// Imports returns the list of imported packages. The list is sorted by
// path.
func (r Registry) Imports() []*Package {
imports := make([]*Package, 0, len(r.imports))
for _, imprt := range r.imports {
imports = append(imports, imprt)
}
sort.Slice(imports, func(i, j int) bool {
return imports[i].Path() < imports[j].Path()
})
return imports
}
func (r Registry) searchImport(name string) (*Package, bool) {
for _, imprt := range r.imports {
if imprt.Qualifier() == name {
return imprt, true
}
}
return nil, false
}
func pkgInfoFromPath(srcDir string, mode packages.LoadMode) (*packages.Package, error) {
pkgs, err := packages.Load(&packages.Config{
Mode: mode,
Dir: srcDir,
})
if err != nil {
return nil, err
}
if len(pkgs) == 0 {
return nil, errors.New("package not found")
}
if len(pkgs) > 1 {
return nil, errors.New("found more than one package")
}
if errs := pkgs[0].Errors; len(errs) != 0 {
if len(errs) == 1 {
return nil, errs[0]
}
return nil, fmt.Errorf("%s (and %d more errors)", errs[0], len(errs)-1)
}
return pkgs[0], nil
}
func findPkgPath(pkgInputVal string, srcPkg *packages.Package) string {
if pkgInputVal == "" {
return srcPkg.PkgPath
}
if pkgInDir(srcPkg.PkgPath, pkgInputVal) {
return srcPkg.PkgPath
}
subdirectoryPath := filepath.Join(srcPkg.PkgPath, pkgInputVal)
if pkgInDir(subdirectoryPath, pkgInputVal) {
return subdirectoryPath
}
return ""
}
func pkgInDir(pkgName, dir string) bool {
currentPkg, err := pkgInfoFromPath(dir, packages.NeedName)
if err != nil {
return false
}
return currentPkg.Name == pkgName || currentPkg.Name+"_test" == pkgName
}
func parseImportsAliases(pkg *packages.Package) map[string]string {
aliases := make(map[string]string)
for _, syntax := range pkg.Syntax {
for _, imprt := range syntax.Imports {
if imprt.Name != nil && imprt.Name.Name != "." {
aliases[strings.Trim(imprt.Path.Value, `"`)] = imprt.Name.Name
}
}
}
return aliases
}
// resolveImportConflict generates and assigns a unique alias for
// packages with conflicting qualifiers.
func resolveImportConflict(a, b *Package, lvl int) {
u1, u2 := a.uniqueName(lvl), b.uniqueName(lvl)
if u1 != u2 {
a.Alias, b.Alias = u1, u2
return
}
resolveImportConflict(a, b, lvl+1)
}