/
cache.go
33 lines (26 loc) · 849 Bytes
/
cache.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
package generator
import "golang.org/x/tools/go/packages"
type Cache struct {
packageMap map[string]interface{}
}
type FakeCache struct{}
func (c *FakeCache) Load(packagePath string) ([]*packages.Package, bool) { return nil, false }
func (c *FakeCache) Store(packagePath string, packages []*packages.Package) {}
type Cacher interface {
Load(packagePath string) ([]*packages.Package, bool)
Store(packagePath string, packages []*packages.Package)
}
func (c *Cache) Load(packagePath string) ([]*packages.Package, bool) {
p, ok := c.packageMap[packagePath]
if !ok {
return nil, false
}
packages, ok := p.([]*packages.Package)
return packages, ok
}
func (c *Cache) Store(packagePath string, packages []*packages.Package) {
if c.packageMap == nil {
c.packageMap = map[string]interface{}{}
}
c.packageMap[packagePath] = packages
}