/
importer.go
147 lines (128 loc) · 3.12 KB
/
importer.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
package gotype
import (
"fmt"
"go/ast"
"go/build"
goparser "go/parser"
"go/token"
"os"
)
// Importer Go source type analyzer
type Importer struct {
fset *token.FileSet
mode goparser.Mode
bufType map[string]Type
bufBuild map[string]*build.Package
errorHandler func(error)
isCommentLocator bool
}
// NewImporter creates a new importer
func NewImporter(options ...Option) *Importer {
i := &Importer{
fset: token.NewFileSet(),
mode: goparser.ParseComments,
bufType: map[string]Type{},
bufBuild: map[string]*build.Package{},
errorHandler: func(err error) {
return
},
}
for _, v := range options {
v(i)
}
return i
}
// Import returns go package scope
func (i *Importer) Import(path string) (Type, error) {
return i.importParse(path, ".")
}
// ImportPackage returns go package scope
func (i *Importer) ImportPackage(path string, pkg *ast.Package) (Type, error) {
np := newParser(i, i.isCommentLocator, path, false)
t := np.ParsePackage(pkg)
i.bufType[path] = t
return t, nil
}
// ImportFile returns go package scope
func (i *Importer) ImportFile(path string, f *ast.File) (Type, error) {
np := newParser(i, i.isCommentLocator, path, false)
t := np.ParseFile(f)
i.bufType[path] = t
return t, nil
}
// ImportSource returns go package scope
func (i *Importer) ImportSource(path string, src []byte) (Type, error) {
f, err := goparser.ParseFile(i.fset, path, src, i.mode)
if err != nil {
return nil, err
}
return i.ImportFile(path, f)
}
// ImportBuild returns details about the Go package named by the import path.
func (i *Importer) ImportBuild(path string) (*build.Package, error) {
imp, err := i.importBuild(path, ".")
if err != nil {
return nil, err
}
return imp, nil
}
// FileSet returns the FileSet
func (i *Importer) FileSet() *token.FileSet {
return i.fset
}
func (i *Importer) importBuild(path string, src string) (*build.Package, error) {
k := path + " " + src
if v, ok := i.bufBuild[k]; ok {
return v, nil
}
imp, err := build.Import(path, src, 0)
if err != nil {
if src != "." {
imp, err = build.Import(path, ".", 0)
}
if err != nil {
i.errorHandler(err)
return nil, err
}
}
i.bufBuild[k] = imp
return imp, nil
}
func (i *Importer) importName(path string, src string) (name string, goroot bool) {
imp, err := i.importBuild(path, src)
if err != nil {
return "", false
}
return imp.Name, imp.Goroot
}
func (i *Importer) importParse(path string, src string) (Type, error) {
imp, err := i.importBuild(path, src)
if err != nil {
return nil, err
}
dir := imp.Dir
tt, ok := i.bufType[dir]
if ok {
return tt, nil
}
m := map[string]bool{}
for _, v := range imp.GoFiles {
m[v] = true
}
p, err := goparser.ParseDir(i.fset, dir, func(fi os.FileInfo) bool {
return m[fi.Name()]
}, i.mode)
if err != nil {
i.errorHandler(err)
return nil, err
}
for _, v := range p {
np := newParser(i, i.isCommentLocator, imp.ImportPath, imp.Goroot)
t := np.ParsePackage(v)
i.bufType[dir] = t
return t, nil
}
err = fmt.Errorf(`No go source code was found under the package path "%s"`, path)
i.errorHandler(err)
return nil, err
}