Skip to content

Commit

Permalink
Merge pull request #409 from xushiwei/q
Browse files Browse the repository at this point in the history
gogen/packages: optional DiskCache
  • Loading branch information
xushiwei authored Mar 24, 2024
2 parents d435e48 + 0986cf9 commit 37ef51d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
21 changes: 17 additions & 4 deletions packages/imp.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,19 @@ import (

// ----------------------------------------------------------------------------

type DiskCache interface {
Find(dir, pkgPath string) (expfile string, err error)
}

type Importer struct {
loaded map[string]*types.Package
fset *token.FileSet
dir string
cache DiskCache
m sync.RWMutex
}

// NewImporter creates an Importer object that meets types.Importer interface.
// NewImporter creates an Importer object that meets types.ImporterFrom and types.Importer interface.
func NewImporter(fset *token.FileSet, workDir ...string) *Importer {
dir := ""
if len(workDir) > 0 {
Expand All @@ -48,6 +53,11 @@ func NewImporter(fset *token.FileSet, workDir ...string) *Importer {
return &Importer{loaded: loaded, fset: fset, dir: dir}
}

// SetDiskCache sets an optional disk cache for the importer.
func (p *Importer) SetDiskCache(cache DiskCache) {
p.cache = cache
}

func (p *Importer) Import(pkgPath string) (pkg *types.Package, err error) {
return p.ImportFrom(pkgPath, p.dir, 0)
}
Expand All @@ -68,7 +78,7 @@ func (p *Importer) ImportFrom(pkgPath, dir string, mode types.ImportMode) (*type
return ret, nil
}
p.m.RUnlock()
expfile, err := FindExport(dir, pkgPath)
expfile, err := p.findExport(dir, pkgPath)
if err != nil {
return nil, err
}
Expand All @@ -93,9 +103,12 @@ func (p *Importer) loadByExport(expfile string, pkgPath string) (ret *types.Pack

// ----------------------------------------------------------------------------

// FindExport lookups export file (.a) of a package by its pkgPath.
// findExport lookups export file (.a) of a package by its pkgPath.
// It returns empty if pkgPath not found.
func FindExport(dir, pkgPath string) (expfile string, err error) {
func (p *Importer) findExport(dir, pkgPath string) (expfile string, err error) {
if c := p.cache; c != nil {
return c.Find(dir, pkgPath)
}
data, err := golistExport(dir, pkgPath)
if err != nil {
return
Expand Down
29 changes: 28 additions & 1 deletion packages/imp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,34 @@ func Test_loadByExport(t *testing.T) {
if _, err := p.loadByExport("/not-found", "notfound"); !os.IsNotExist(err) {
t.Fatal("Test_loadByExport: no error?")
}
FindExport(".", "C")
p.findExport(".", "C")
}

// ----------------------------------------------------------------------------

type diskCache struct {
imp *Importer
}

func (p diskCache) Find(dir, pkgPath string) (expfile string, err error) {
if p.imp != nil {
return p.imp.findExport(dir, pkgPath)
}
return "", os.ErrNotExist
}

func TestDiskCache(t *testing.T) {
p := NewImporter(nil)
p.SetDiskCache(diskCache{})
_, err := p.Import("fmt")
if err != os.ErrNotExist {
t.Fatal("Import:", err)
}
p.SetDiskCache(diskCache{imp: NewImporter(nil)})
pkg, err := p.Import("fmt")
if err != nil || pkg.Path() != "fmt" {
t.Fatal("Import fmt:", pkg, err)
}
}

// ----------------------------------------------------------------------------

0 comments on commit 37ef51d

Please sign in to comment.