Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gogen/packages: Importer.Cache #414

Merged
merged 2 commits into from
Mar 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/imp.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ func (p *Importer) SetCache(cache Cache) {
p.cache = cache
}

// Cache returns the cache of the importer.
func (p *Importer) Cache() Cache {
return p.cache
}

// Import returns the imported package for the given import path.
func (p *Importer) Import(pkgPath string) (pkg *types.Package, err error) {
return p.ImportFrom(pkgPath, p.dir, 0)
Expand Down Expand Up @@ -108,7 +113,6 @@ func (p *Importer) loadByExport(expfile string, pkgPath string) (ret *types.Pack
// ----------------------------------------------------------------------------

// findExport lookups export file (.a) of a package by its pkgPath.
// It returns empty if pkgPath not found.
func (p *Importer) findExport(dir, pkgPath string) (expfile string, err error) {
if c := p.cache; c != nil {
return c.Find(dir, pkgPath)
Expand Down
3 changes: 3 additions & 0 deletions packages/imp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ func TestCache(t *testing.T) {
t.Fatal("Import:", err)
}
p.SetCache(diskCache{imp: NewImporter(nil)})
if p.Cache() == nil {
t.Fatal("Cache nil")
}
pkg, err := p.Import("fmt")
if err != nil || pkg.Path() != "fmt" {
t.Fatal("Import fmt:", pkg, err)
Expand Down