-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FEATURE: use source-based types.Importer
This importer reads type information directly from up-to-date source. See golang/go#11415.
- Loading branch information
Showing
4 changed files
with
74 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package generic | ||
|
||
import ( | ||
"go/ast" | ||
"go/parser" | ||
"go/token" | ||
"go/types" | ||
) | ||
|
||
type im struct { | ||
cache map[string]*types.Package | ||
} | ||
|
||
// NewImporter creates a new types.Importer. | ||
// | ||
// See https://github.com/golang/go/issues/11415. | ||
// Many applications use the gcimporter package to read type information from compiled object files. | ||
// There's no guarantee that those files are even remotely recent. | ||
func NewImporter() types.Importer { | ||
return &im{ | ||
cache: make(map[string]*types.Package), | ||
} | ||
} | ||
|
||
func (i *im) Import(pkgPath string) (*types.Package, error) { | ||
if pkgPath == "unsafe" { | ||
return types.Unsafe, nil | ||
} | ||
|
||
if pkg, ok := i.cache[pkgPath]; ok { | ||
return pkg, nil | ||
} | ||
|
||
fset := token.NewFileSet() | ||
var files []*ast.File | ||
err := walkSource(pkgPath, func(path string) error { | ||
f, err := parser.ParseFile(fset, path, nil, 0) | ||
if err != nil { | ||
return err | ||
} | ||
files = append(files, f) | ||
return nil | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
conf := types.Config{ | ||
Importer: i, | ||
} | ||
|
||
pkg, err := conf.Check(pkgPath, fset, files, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
i.cache[pkgPath] = pkg | ||
return pkg, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters