-
Notifications
You must be signed in to change notification settings - Fork 0
/
package.go
83 lines (72 loc) · 2.08 KB
/
package.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
package patch
import (
"fmt"
"go/ast"
"go/token"
"go/types"
"log"
"strings"
"google.golang.org/protobuf/compiler/protogen"
)
// Package represents a Go package for patching.
type Package struct {
pkg *types.Package
files []*ast.File
filesByName map[string]*ast.File
}
// NewPackage returns an initialized Package.
func NewPackage(path, name string) *Package {
log.Printf("Go package:\t%s %q", name, path)
return &Package{
pkg: types.NewPackage(path, name),
filesByName: make(map[string]*ast.File),
}
}
// File returns the ast.File for filename, if it exists.
func (pkg *Package) File(filename string) *ast.File {
return pkg.filesByName[filename]
}
// AddFile adds a parsed file to pkg.
func (pkg *Package) AddFile(filename string, f *ast.File) error {
if _, ok := pkg.filesByName[filename]; ok {
return fmt.Errorf("package %s: file already added: %s", pkg.pkg.Name(), filename)
}
pkg.files = append(pkg.files, f)
pkg.filesByName[filename] = f
return nil
}
// Reset resets pkg type-checks.
func (pkg *Package) Reset() {
pkg.pkg = types.NewPackage(pkg.pkg.Path(), pkg.pkg.Name())
}
// Check type-checks pkg.
func (pkg *Package) Check(importer types.Importer, fset *token.FileSet, info *types.Info) error {
log.Printf("Type-check:\t%s", pkg.pkg.Path())
cfg := &types.Config{
Error: func(err error) {
// log.Printf("Warning: %v", err)
},
Importer: importer,
}
checker := types.NewChecker(cfg, fset, pkg.pkg, info)
_ = checker.Files(pkg.files)
return nil // TODO: return an actual error?
}
// Find finds id in Package pkg, and any ancestor(s), or nil if the id is not found in pkg.
func (pkg *Package) Find(id protogen.GoIdent) (obj types.Object, ancestors []types.Object) {
for _, name := range strings.Split(id.GoName, ".") {
if obj == nil {
obj = pkg.pkg.Scope().Lookup(name)
} else {
ancestors = append(ancestors, obj)
obj, _, _ = types.LookupFieldOrMethod(obj.Type(), true, obj.Pkg(), name)
}
if obj == nil {
break
}
}
if obj == nil {
log.Printf("Warning: unable to find declaration %s.%s", id.GoImportPath, id.GoName)
}
return
}