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

Commit 31ede57

Browse files
committed
gps: source cache: add ok bool to singleSourceCache.getAllVersions() in order to avoid depending on len(0) vs nil distinction
1 parent ca3d62d commit 31ede57

7 files changed

+34
-32
lines changed

internal/gps/source.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -494,8 +494,10 @@ func (sg *sourceGateway) listVersions(ctx context.Context) ([]PairedVersion, err
494494
if err != nil {
495495
return nil, err
496496
}
497-
498-
return sg.cache.getAllVersions(), nil
497+
if pvs, ok := sg.cache.getAllVersions(); ok {
498+
return pvs, nil
499+
}
500+
return nil, nil
499501
}
500502

501503
func (sg *sourceGateway) revisionPresentIn(ctx context.Context, r Revision) (bool, error) {

internal/gps/source_cache.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ type singleSourceCache interface {
4343
getVersionsFor(Revision) ([]UnpairedVersion, bool)
4444

4545
// Gets all the version pairs currently known to the cache.
46-
getAllVersions() []PairedVersion
46+
getAllVersions() ([]PairedVersion, bool)
4747

4848
// Get the revision corresponding to the given unpaired version.
4949
getRevisionFor(UnpairedVersion) (Revision, bool)
@@ -169,17 +169,17 @@ func (c *singleSourceCacheMemory) getVersionsFor(r Revision) ([]UnpairedVersion,
169169
return versionList, has
170170
}
171171

172-
func (c *singleSourceCacheMemory) getAllVersions() []PairedVersion {
172+
func (c *singleSourceCacheMemory) getAllVersions() ([]PairedVersion, bool) {
173173
c.mut.Lock()
174174
vList := c.vList
175175
c.mut.Unlock()
176176

177177
if vList == nil {
178-
return nil
178+
return nil, false
179179
}
180180
cp := make([]PairedVersion, len(vList))
181181
copy(cp, vList)
182-
return cp
182+
return cp, true
183183
}
184184

185185
func (c *singleSourceCacheMemory) getRevisionFor(uv UnpairedVersion) (Revision, bool) {

internal/gps/source_cache_bolt.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -351,8 +351,7 @@ func (s *singleSourceCacheBolt) getVersionsFor(rev Revision) (uvs []UnpairedVers
351351
return
352352
}
353353

354-
func (s *singleSourceCacheBolt) getAllVersions() []PairedVersion {
355-
var pvs []PairedVersion
354+
func (s *singleSourceCacheBolt) getAllVersions() (pvs []PairedVersion, ok bool) {
356355
err := s.viewSourceBucket(func(src *bolt.Bucket) error {
357356
versions := cacheFindLatestValid(src, cacheVersion, s.epoch)
358357
if versions == nil {
@@ -369,14 +368,15 @@ func (s *singleSourceCacheBolt) getAllVersions() []PairedVersion {
369368
return err
370369
}
371370
pvs = append(pvs, uv.Pair(Revision(v)))
371+
ok = true
372372
return nil
373373
})
374374
})
375375
if err != nil {
376376
s.logger.Println(errors.Wrap(err, "failed to get all cached versions"))
377-
return nil
377+
return nil, false
378378
}
379-
return pvs
379+
return
380380
}
381381

382382
func (s *singleSourceCacheBolt) getRevisionFor(uv UnpairedVersion) (rev Revision, ok bool) {

internal/gps/source_cache_bolt_test.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ func TestBoltCacheTimeout(t *testing.T) {
119119
}
120120
comparePackageTree(t, ptree, got)
121121

122-
gotV := c.getAllVersions()
123-
if len(gotV) != len(pvs) {
122+
gotV, ok := c.getAllVersions()
123+
if !ok || len(gotV) != len(pvs) {
124124
t.Errorf("unexpected versions:\n\t(GOT): %#v\n\t(WNT): %#v", gotV, pvs)
125125
} else {
126126
SortPairedForDowngrade(gotV)
@@ -161,8 +161,8 @@ func TestBoltCacheTimeout(t *testing.T) {
161161
}
162162
comparePackageTree(t, ptree, gotPtree)
163163

164-
pvs := c.getAllVersions()
165-
if len(pvs) > 0 {
164+
pvs, ok := c.getAllVersions()
165+
if ok || len(pvs) > 0 {
166166
t.Errorf("expected no cached versions, but got:\n\t%#v", pvs)
167167
}
168168
}
@@ -194,8 +194,8 @@ func TestBoltCacheTimeout(t *testing.T) {
194194
}
195195
comparePackageTree(t, ptree, got)
196196

197-
gotV := c.getAllVersions()
198-
if len(gotV) != len(pvs) {
197+
gotV, ok := c.getAllVersions()
198+
if !ok || len(gotV) != len(pvs) {
199199
t.Errorf("unexpected versions:\n\t(GOT): %#v\n\t(WNT): %#v", gotV, pvs)
200200
} else {
201201
SortPairedForDowngrade(gotV)
@@ -282,8 +282,8 @@ func TestBoltCacheTimeout(t *testing.T) {
282282
}
283283
comparePackageTree(t, newPtree, got)
284284

285-
gotV := c.getAllVersions()
286-
if len(gotV) != len(newPVS) {
285+
gotV, ok := c.getAllVersions()
286+
if !ok || len(gotV) != len(newPVS) {
287287
t.Errorf("unexpected versions:\n\t(GOT): %#v\n\t(WNT): %#v", gotV, newPVS)
288288
} else {
289289
SortPairedForDowngrade(gotV)

internal/gps/source_cache_multi.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -77,19 +77,19 @@ func (c *multiCache) getVersionsFor(rev Revision) ([]UnpairedVersion, bool) {
7777
return c.disk.getVersionsFor(rev)
7878
}
7979

80-
func (c *multiCache) getAllVersions() []PairedVersion {
81-
pvs := c.mem.getAllVersions()
82-
if pvs != nil {
83-
return pvs
80+
func (c *multiCache) getAllVersions() ([]PairedVersion, bool) {
81+
pvs, ok := c.mem.getAllVersions()
82+
if ok {
83+
return pvs, true
8484
}
8585

86-
pvs = c.disk.getAllVersions()
87-
if pvs != nil {
86+
pvs, ok = c.disk.getAllVersions()
87+
if ok {
8888
c.mem.setVersionMap(pvs)
89-
return pvs
89+
return pvs, true
9090
}
9191

92-
return nil
92+
return nil, false
9393
}
9494

9595
func (c *multiCache) getRevisionFor(uv UnpairedVersion) (Revision, bool) {

internal/gps/source_cache_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,8 @@ func (test singleSourceCacheTest) run(t *testing.T) {
305305
}
306306

307307
t.Run("getAllVersions", func(t *testing.T) {
308-
got := c.getAllVersions()
309-
if len(got) != len(versions) {
308+
got, ok := c.getAllVersions()
309+
if !ok || len(got) != len(versions) {
310310
t.Errorf("unexpected versions:\n\t(GOT): %#v\n\t(WNT): %#v", got, versions)
311311
} else {
312312
SortPairedForDowngrade(got)
@@ -566,8 +566,8 @@ func (discardCache) getVersionsFor(Revision) ([]UnpairedVersion, bool) {
566566
return nil, false
567567
}
568568

569-
func (discardCache) getAllVersions() []PairedVersion {
570-
return nil
569+
func (discardCache) getAllVersions() ([]PairedVersion, bool) {
570+
return nil, false
571571
}
572572

573573
func (discardCache) getRevisionFor(UnpairedVersion) (Revision, bool) {

internal/gps/source_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ func testSourceGateway(t *testing.T) {
6060
t.Fatalf("error on cloning git repo: %s", err)
6161
}
6262

63-
cvlist := sg.cache.getAllVersions()
64-
if len(cvlist) != 4 {
63+
cvlist, ok := sg.cache.getAllVersions()
64+
if !ok || len(cvlist) != 4 {
6565
t.Fatalf("repo setup should've cached four versions, got %v: %s", len(cvlist), cvlist)
6666
}
6767

0 commit comments

Comments
 (0)