-
Notifications
You must be signed in to change notification settings - Fork 56
/
resolver.go
63 lines (49 loc) · 1.66 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package gobuild
import (
"go/build"
"github.com/dave/dst/decorator/resolver"
)
func New(dir string) *RestorerResolver {
return &RestorerResolver{Dir: dir}
}
func WithContext(dir string, context *build.Context) *RestorerResolver {
return &RestorerResolver{Dir: dir, Context: context}
}
func WithHints(dir string, hints map[string]string) *RestorerResolver {
return &RestorerResolver{Dir: dir, Hints: hints}
}
type RestorerResolver struct {
// FindPackage is called during Load to create the build.Package for a given import path from a
// given directory. If FindPackage is nil, (*build.Context).Import is used. A client may use
// this hook to adapt to a proprietary build system that does not follow the "go build" layout
// conventions, for example. It must be safe to call concurrently from multiple goroutines.
//
// It should be noted that Manager only uses the Name from the returned *build.Package, so all
// other fields can be left empty (as in SimpleFinder).
FindPackage func(ctxt *build.Context, importPath, fromDir string, mode build.ImportMode) (*build.Package, error)
Context *build.Context
Dir string
// Hints (package path -> name) is first checked before asking the build package
Hints map[string]string
}
func (r *RestorerResolver) ResolvePackage(importPath string) (string, error) {
if name, ok := r.Hints[importPath]; ok {
return name, nil
}
fp := r.FindPackage
if fp == nil {
fp = (*build.Context).Import
}
bc := r.Context
if bc == nil {
bc = &build.Default
}
p, err := fp(bc, importPath, r.Dir, 0)
if err != nil {
return "", err
}
if p == nil {
return "", resolver.ErrPackageNotFound
}
return p.Name, nil
}