Skip to content
This repository has been archived by the owner on Sep 9, 2020. It is now read-only.

gps: add new ProjectAnalyzerInfo type to return from ProjectAnalyzer.Info #753

Merged
merged 1 commit into from
Jun 15, 2017
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
9 changes: 6 additions & 3 deletions analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ func (a Analyzer) DeriveManifestAndLock(path string, n gps.ProjectRoot) (gps.Man
return m, nil, nil
}

// Info returns the name and version of this ProjectAnalyzer.
func (a Analyzer) Info() (string, int) {
return "dep", 1
// Info returns Analyzer's name and version info.
func (a Analyzer) Info() gps.ProjectAnalyzerInfo {
return gps.ProjectAnalyzerInfo{
Name: "dep",
Version: 1,
}
}
6 changes: 3 additions & 3 deletions analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ func TestAnalyzerDeriveManifestAndLockInvalidManifest(t *testing.T) {
func TestAnalyzerInfo(t *testing.T) {
a := Analyzer{}

name, vers := a.Info()
info := a.Info()

if name != "dep" || vers != 1 {
t.Fatalf("expected name to be 'dep' and version to be 1: name -> %q vers -> %d", name, vers)
if info.Name != "dep" || info.Version != 1 {
t.Fatalf("expected name to be 'dep' and version to be 1: name -> %q vers -> %d", info.Name, info.Version)
}
}
7 changes: 5 additions & 2 deletions cmd/dep/root_analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,16 @@ func (a *rootAnalyzer) FinalizeRootManifestAndLock(m *dep.Manifest, l *dep.Lock)
}
}

func (a *rootAnalyzer) Info() (string, int) {
func (a *rootAnalyzer) Info() gps.ProjectAnalyzerInfo {
name := "dep"
version := 1
if !a.skipTools {
name = "dep+import"
}
return name, version
return gps.ProjectAnalyzerInfo{
Name: name,
Version: version,
}
}

func lookupVersionForRevision(rev gps.Revision, pi gps.ProjectIdentifier, sm gps.SourceManager) (gps.Version, error) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/dep/root_analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestRootAnalyzer_Info(t *testing.T) {
}
for skipTools, want := range testCases {
a := rootAnalyzer{skipTools: skipTools}
got, _ := a.Info()
got := a.Info().Name
if got != want {
t.Errorf("Expected the name of the importer with skipTools=%t to be '%s', got '%s'", skipTools, want, got)
}
Expand Down
7 changes: 5 additions & 2 deletions internal/gps/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ func (a NaiveAnalyzer) DeriveManifestAndLock(path string, n gps.ProjectRoot) (gp

// Reports the name and version of the analyzer. This is used internally as part
// of gps' hashing memoization scheme.
func (a NaiveAnalyzer) Info() (name string, version int) {
return "example-analyzer", 1
func (a NaiveAnalyzer) Info() gps.ProjectAnalyzerInfo {
return gps.ProjectAnalyzerInfo{
Name: "example-analyzer",
Version: 1,
}
}
6 changes: 3 additions & 3 deletions internal/gps/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ func (s *solver) writeHashingInputs(w io.Writer) {
}

writeString(hhAnalyzer)
an, av := s.rd.an.Info()
writeString(an)
writeString(strconv.Itoa(av))
ai := s.rd.an.Info()
writeString(ai.Name)
writeString(strconv.Itoa(ai.Version))
}

// bytes.Buffer wrapper that injects newlines after each call to Write().
Expand Down
7 changes: 5 additions & 2 deletions internal/gps/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ func (naiveAnalyzer) DeriveManifestAndLock(string, ProjectRoot) (Manifest, Lock,
return nil, nil, nil
}

func (a naiveAnalyzer) Info() (name string, version int) {
return "naive-analyzer", 1
func (a naiveAnalyzer) Info() ProjectAnalyzerInfo {
return ProjectAnalyzerInfo{
Name: "naive-analyzer",
Version: 1,
}
}

func mkNaiveSM(t *testing.T) (*SourceMgr, func()) {
Expand Down
11 changes: 4 additions & 7 deletions internal/gps/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,8 @@ type solution struct {
// The hash digest of the input opts
hd []byte

// The analyzer name
analyzerName string

// The analyzer version
analyzerVersion int
// The analyzer info
analyzerInfo ProjectAnalyzerInfo

// The solver used in producing this solution
solv Solver
Expand Down Expand Up @@ -95,11 +92,11 @@ func (r solution) InputHash() []byte {
}

func (r solution) AnalyzerName() string {
return r.analyzerName
return r.analyzerInfo.Name
}

func (r solution) AnalyzerVersion() int {
return r.analyzerVersion
return r.analyzerInfo.Version
}

func (r solution) SolverName() string {
Expand Down
2 changes: 1 addition & 1 deletion internal/gps/result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func init() {
}, nil),
},
}
basicResult.analyzerName, basicResult.analyzerVersion = (naiveAnalyzer{}).Info()
basicResult.analyzerInfo = (naiveAnalyzer{}).Info()

// Just in case something needs punishing, kubernetes offers a complex,
// real-world set of dependencies, and this revision is known to work.
Expand Down
2 changes: 1 addition & 1 deletion internal/gps/solver.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ func (s *solver) Solve() (Solution, error) {
att: s.attempts,
solv: s,
}
soln.analyzerName, soln.analyzerVersion = s.rd.an.Info()
soln.analyzerInfo = s.rd.an.Info()
soln.hd = s.HashInputs()

// Convert ProjectAtoms into LockedProjects
Expand Down
7 changes: 3 additions & 4 deletions internal/gps/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ func (sg *sourceGateway) getManifestAndLock(ctx context.Context, pr ProjectRoot,
return nil, nil, err
}

m, l, has := sg.cache.getManifestAndLock(r, an)
m, l, has := sg.cache.getManifestAndLock(r, an.Info())
if has {
return m, l, nil
}
Expand All @@ -286,8 +286,7 @@ func (sg *sourceGateway) getManifestAndLock(ctx context.Context, pr ProjectRoot,
return nil, nil, err
}

name, vers := an.Info()
label := fmt.Sprintf("%s:%s.%v", sg.src.upstreamURL(), name, vers)
label := fmt.Sprintf("%s:%s", sg.src.upstreamURL(), an.Info())
err = sg.suprvsr.do(ctx, label, ctGetManifestAndLock, func(ctx context.Context) error {
m, l, err = sg.src.getManifestAndLock(ctx, pr, r, an)
return err
Expand Down Expand Up @@ -317,7 +316,7 @@ func (sg *sourceGateway) getManifestAndLock(ctx context.Context, pr ProjectRoot,
return nil, nil, err
}

sg.cache.setManifestAndLock(r, an, m, l)
sg.cache.setManifestAndLock(r, an.Info(), m, l)
return m, l, nil
}

Expand Down
18 changes: 9 additions & 9 deletions internal/gps/source_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ import (
type singleSourceCache interface {
// Store the manifest and lock information for a given revision, as defined by
// a particular ProjectAnalyzer.
setManifestAndLock(Revision, ProjectAnalyzer, Manifest, Lock)
setManifestAndLock(Revision, ProjectAnalyzerInfo, Manifest, Lock)

// Get the manifest and lock information for a given revision, as defined by
// a particular ProjectAnalyzer.
getManifestAndLock(Revision, ProjectAnalyzer) (Manifest, Lock, bool)
getManifestAndLock(Revision, ProjectAnalyzerInfo) (Manifest, Lock, bool)

// Store a PackageTree for a given revision.
setPackageTree(Revision, pkgtree.PackageTree)
Expand Down Expand Up @@ -63,15 +63,15 @@ type singleSourceCache interface {

type singleSourceCacheMemory struct {
mut sync.RWMutex // protects all maps
infos map[ProjectAnalyzer]map[Revision]projectInfo
infos map[ProjectAnalyzerInfo]map[Revision]projectInfo
ptrees map[Revision]pkgtree.PackageTree
vMap map[UnpairedVersion]Revision
rMap map[Revision][]UnpairedVersion
}

func newMemoryCache() singleSourceCache {
return &singleSourceCacheMemory{
infos: make(map[ProjectAnalyzer]map[Revision]projectInfo),
infos: make(map[ProjectAnalyzerInfo]map[Revision]projectInfo),
ptrees: make(map[Revision]pkgtree.PackageTree),
vMap: make(map[UnpairedVersion]Revision),
rMap: make(map[Revision][]UnpairedVersion),
Expand All @@ -83,12 +83,12 @@ type projectInfo struct {
Lock
}

func (c *singleSourceCacheMemory) setManifestAndLock(r Revision, an ProjectAnalyzer, m Manifest, l Lock) {
func (c *singleSourceCacheMemory) setManifestAndLock(r Revision, pai ProjectAnalyzerInfo, m Manifest, l Lock) {
c.mut.Lock()
inner, has := c.infos[an]
inner, has := c.infos[pai]
if !has {
inner = make(map[Revision]projectInfo)
c.infos[an] = inner
c.infos[pai] = inner
}
inner[r] = projectInfo{Manifest: m, Lock: l}

Expand All @@ -100,11 +100,11 @@ func (c *singleSourceCacheMemory) setManifestAndLock(r Revision, an ProjectAnaly
c.mut.Unlock()
}

func (c *singleSourceCacheMemory) getManifestAndLock(r Revision, an ProjectAnalyzer) (Manifest, Lock, bool) {
func (c *singleSourceCacheMemory) getManifestAndLock(r Revision, pai ProjectAnalyzerInfo) (Manifest, Lock, bool) {
c.mut.Lock()
defer c.mut.Unlock()

inner, has := c.infos[an]
inner, has := c.infos[pai]
if !has {
return nil, nil, false
}
Expand Down
15 changes: 13 additions & 2 deletions internal/gps/source_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,19 @@ type ProjectAnalyzer interface {
// expected files containing Manifest and Lock data are merely absent.
DeriveManifestAndLock(path string, importRoot ProjectRoot) (Manifest, Lock, error)

// Report the name and version of this ProjectAnalyzer.
Info() (name string, version int)
// Info reports this project analyzer's info.
Info() ProjectAnalyzerInfo
}

// ProjectAnalyzerInfo indicates a ProjectAnalyzer's name and version.
type ProjectAnalyzerInfo struct {
Name string
Version int
}

// String returns a string like: "<name>.<decimal version>"
func (p ProjectAnalyzerInfo) String() string {
return fmt.Sprintf("%s.%d", p.Name, p.Version)
}

// SourceMgr is the default SourceManager for gps.
Expand Down