-
Notifications
You must be signed in to change notification settings - Fork 2
/
resolver.go
43 lines (34 loc) · 1.03 KB
/
resolver.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
package tracegen
import (
"fmt"
"github.com/dave/dst"
"github.com/dave/dst/decorator"
)
type SimpleResolver map[string]string
func NewSimpleResolver(pkg *decorator.Package, file *dst.File, hints map[string]string) SimpleResolver {
r := make(SimpleResolver)
for name, pkg := range pkg.Imports {
r[name] = pkg.Name
}
// To ensure we only resolve file-level imports, we omit any package-level
// imports that aren't used in this file directly. This is because the pkg
// has greater fidelity (its Imports attribute is map[string]*Package, and
// the file's is []*ImportSpec) and we need a fully-qualified mapping from
// e.g. github.com/pkg/errors => errors
for _, imp := range file.Imports {
path := imp.Path.Value
if _, ok := r[path]; !ok {
delete(r, path)
}
}
for pkgPath, name := range hints {
r[pkgPath] = name
}
return r
}
func (r SimpleResolver) ResolvePackage(importPath string) (string, error) {
if n, ok := r[importPath]; ok {
return n, nil
}
return "", fmt.Errorf("package %s was not found", importPath)
}