Skip to content
This repository was archived by the owner on Sep 9, 2020. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e78bc49

Browse files
committedSep 12, 2017
gps: source cache: move protobufs to internal package
1 parent 5847aa6 commit e78bc49

13 files changed

+304
-295
lines changed
 

‎internal/gps/constraint_test.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"fmt"
99
"testing"
1010

11+
"github.com/golang/dep/internal/gps/internal/pb"
1112
"github.com/golang/protobuf/proto"
1213
"github.com/pkg/errors"
1314
)
@@ -976,7 +977,7 @@ func TestConstraintEncoding(t *testing.T) {
976977
{"rev", Revision("test")},
977978
} {
978979
t.Run(test.name, func(t *testing.T) {
979-
var msg ConstraintMsg
980+
var msg pb.Constraint
980981
test.c.copyTo(&msg)
981982
b, err := proto.Marshal(&msg)
982983
if err != nil {
@@ -986,15 +987,15 @@ func TestConstraintEncoding(t *testing.T) {
986987
if err := proto.Unmarshal(b, &msg); err != nil {
987988
t.Fatal(err)
988989
}
989-
got, err := msg.asConstraint()
990+
got, err := constraintFromCache(&msg)
990991
if err != nil {
991992
t.Error("failed to decode:", err)
992993
} else if !got.identical(test.c) {
993994
t.Errorf("decoded non-identical Constraint:\n\t(GOT): %#v\n\t(WNT): %#v", got, test.c)
994995
}
995996

996997
if _, ok := test.c.(UnpairedVersion); ok {
997-
got, err := msg.asUnpairedVersion()
998+
got, err := unpairedVersionFromCache(&msg)
998999
if err != nil {
9991000
t.Error("failed to decode:", err)
10001001
} else if !got.identical(test.c) {

‎internal/gps/constraints.go

+21-20
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"sort"
1010

1111
"github.com/Masterminds/semver"
12+
"github.com/golang/dep/internal/gps/internal/pb"
1213
)
1314

1415
var (
@@ -53,9 +54,9 @@ type Constraint interface {
5354
// design goal of the system.
5455
typedString() string
5556

56-
// copyTo copies fields into a serializable representation which can be deserialized into
57-
// an identical Constraint with decodeConstraint.
58-
copyTo(*ConstraintMsg)
57+
// copyTo copies fields into a serializable representation which can be
58+
// converted back into an identical Constraint with constraintFromCache.
59+
copyTo(*pb.Constraint)
5960

6061
// identical returns true if the constraints are identical.
6162
//
@@ -66,35 +67,35 @@ type Constraint interface {
6667
identical(Constraint) bool
6768
}
6869

69-
// asConstraint returns a Constraint identical to the one which produced m.
70-
func (m *ConstraintMsg) asConstraint() (Constraint, error) {
70+
// constraintFromCache returns a Constraint identical to the one which produced m.
71+
func constraintFromCache(m *pb.Constraint) (Constraint, error) {
7172
switch m.Type {
72-
case ConstraintMsg_Revision:
73+
case pb.Constraint_Revision:
7374
return Revision(m.Value), nil
74-
case ConstraintMsg_Branch:
75+
case pb.Constraint_Branch:
7576
return NewBranch(m.Value), nil
76-
case ConstraintMsg_DefaultBranch:
77+
case pb.Constraint_DefaultBranch:
7778
return newDefaultBranch(m.Value), nil
78-
case ConstraintMsg_Version:
79+
case pb.Constraint_Version:
7980
return plainVersion(m.Value), nil
80-
case ConstraintMsg_Semver:
81+
case pb.Constraint_Semver:
8182
return NewSemverConstraint(m.Value)
8283

8384
default:
8485
return nil, fmt.Errorf("unrecognized Constraint type: %#v", m)
8586
}
8687
}
8788

88-
// asUnpairedVersion returns an UnpairedVersion identical to the one which produced m.
89-
func (m *ConstraintMsg) asUnpairedVersion() (UnpairedVersion, error) {
89+
// unpairedVersionFromCache returns an UnpairedVersion identical to the one which produced m.
90+
func unpairedVersionFromCache(m *pb.Constraint) (UnpairedVersion, error) {
9091
switch m.Type {
91-
case ConstraintMsg_Branch:
92+
case pb.Constraint_Branch:
9293
return NewBranch(m.Value), nil
93-
case ConstraintMsg_DefaultBranch:
94+
case pb.Constraint_DefaultBranch:
9495
return newDefaultBranch(m.Value), nil
95-
case ConstraintMsg_Version:
96+
case pb.Constraint_Version:
9697
return plainVersion(m.Value), nil
97-
case ConstraintMsg_Semver:
98+
case pb.Constraint_Semver:
9899
sv, err := semver.NewVersion(m.Value)
99100
if err != nil {
100101
return nil, err
@@ -232,8 +233,8 @@ func (c semverConstraint) identical(c2 Constraint) bool {
232233
return c.c.String() == sc2.c.String()
233234
}
234235

235-
func (c semverConstraint) copyTo(msg *ConstraintMsg) {
236-
msg.Type = ConstraintMsg_Semver
236+
func (c semverConstraint) copyTo(msg *pb.Constraint) {
237+
msg.Type = pb.Constraint_Semver
237238
msg.Value = c.String()
238239
}
239240

@@ -280,7 +281,7 @@ func (anyConstraint) identical(c Constraint) bool {
280281
return IsAny(c)
281282
}
282283

283-
func (anyConstraint) copyTo(*ConstraintMsg) {
284+
func (anyConstraint) copyTo(*pb.Constraint) {
284285
panic("anyConstraint should never be serialized; it is solver internal-only")
285286
}
286287

@@ -317,7 +318,7 @@ func (noneConstraint) identical(c Constraint) bool {
317318
return ok
318319
}
319320

320-
func (noneConstraint) copyTo(*ConstraintMsg) {
321+
func (noneConstraint) copyTo(*pb.Constraint) {
321322
panic("noneConstraint should never be serialized; it is solver internal-only")
322323
}
323324

‎internal/gps/internal/pb/pb.go

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Package pb provides generated Protocol Buffers for cache serialization.
2+
3+
//go:generate protoc --go_out=. source_cache.proto
4+
package pb

‎internal/gps/internal/pb/source_cache.pb.go

+199
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
syntax = "proto3";
2+
package pb;
3+
4+
// Constraint is a serializable representation of a gps.Constraint or gps.UnpairedVersion.
5+
message Constraint {
6+
enum Type {
7+
Revision = 0;
8+
Branch = 1;
9+
DefaultBranch = 2;
10+
Version = 3;
11+
Semver = 4;
12+
}
13+
Type type = 1;
14+
string value = 2;
15+
//TODO strongly typed Semver field
16+
}
17+
18+
// ProjectProperties is a serializable representation of gps.ProjectRoot and gps.ProjectProperties.
19+
message ProjectProperties {
20+
string root = 1;
21+
string source = 2;
22+
Constraint constraint = 3;
23+
}
24+
25+
// LockedProject is a serializable representation of gps.LockedProject.
26+
message LockedProject {
27+
string root = 1;
28+
string source = 2;
29+
Constraint unpairedVersion = 3;
30+
string revision = 4;
31+
repeated string packages = 5;
32+
}

‎internal/gps/source_cache.pb.go

-199
This file was deleted.

‎internal/gps/source_cache.proto

-32
This file was deleted.

‎internal/gps/source_cache_bolt.go

+9-8
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"time"
1313

1414
"github.com/boltdb/bolt"
15+
"github.com/golang/dep/internal/gps/internal/pb"
1516
"github.com/golang/dep/internal/gps/pkgtree"
1617
"github.com/golang/protobuf/proto"
1718
"github.com/jmank88/nuts"
@@ -278,7 +279,7 @@ func (s *singleSourceCacheBolt) setVersionMap(pvs []PairedVersion) {
278279

279280
revVersions := make(map[Revision]*bolt.Bucket)
280281
key := make(nuts.Key, nuts.KeyLen(uint64(len(pvs)-1)))
281-
var msg ConstraintMsg
282+
var msg pb.Constraint
282283
for i, pv := range pvs {
283284
uv, rev := pv.Unpair(), pv.Revision()
284285
uv.copyTo(&msg)
@@ -330,12 +331,12 @@ func (s *singleSourceCacheBolt) getVersionsFor(rev Revision) (uvs []UnpairedVers
330331

331332
ok = true
332333

333-
var msg ConstraintMsg
334+
var msg pb.Constraint
334335
return versions.ForEach(func(_, v []byte) error {
335336
if err := proto.Unmarshal(v, &msg); err != nil {
336337
return err
337338
}
338-
uv, err := msg.asUnpairedVersion()
339+
uv, err := unpairedVersionFromCache(&msg)
339340
if err != nil {
340341
return err
341342
}
@@ -358,12 +359,12 @@ func (s *singleSourceCacheBolt) getAllVersions() []PairedVersion {
358359
return nil
359360
}
360361

361-
var msg ConstraintMsg
362+
var msg pb.Constraint
362363
return versions.ForEach(func(k, v []byte) error {
363364
if err := proto.Unmarshal(k, &msg); err != nil {
364365
return err
365366
}
366-
uv, err := msg.asUnpairedVersion()
367+
uv, err := unpairedVersionFromCache(&msg)
367368
if err != nil {
368369
return err
369370
}
@@ -385,7 +386,7 @@ func (s *singleSourceCacheBolt) getRevisionFor(uv UnpairedVersion) (rev Revision
385386
return nil
386387
}
387388

388-
var msg ConstraintMsg
389+
var msg pb.Constraint
389390
uv.copyTo(&msg)
390391
b, err := proto.Marshal(&msg)
391392
if err != nil {
@@ -437,12 +438,12 @@ func (s *singleSourceCacheBolt) toUnpaired(v Version) (uv UnpairedVersion, ok bo
437438
if len(v) == 0 {
438439
return nil
439440
}
440-
var msg ConstraintMsg
441+
var msg pb.Constraint
441442
if err := proto.Unmarshal(v, &msg); err != nil {
442443
return err
443444
}
444445
var err error
445-
uv, err = msg.asUnpairedVersion()
446+
uv, err = unpairedVersionFromCache(&msg)
446447
if err != nil {
447448
return err
448449
}

‎internal/gps/source_cache_bolt_encode.go

+18-17
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"time"
1010

1111
"github.com/boltdb/bolt"
12+
"github.com/golang/dep/internal/gps/internal/pb"
1213
"github.com/golang/dep/internal/gps/pkgtree"
1314
"github.com/golang/protobuf/proto"
1415
"github.com/jmank88/nuts"
@@ -31,16 +32,16 @@ var (
3132
cacheV = byte('v')
3233
)
3334

34-
// properties returns a new ProjectRoot and ProjectProperties with the fields from m.
35-
func (m *ProjectPropertiesMsg) properties() (ProjectRoot, ProjectProperties, error) {
35+
// propertiesFromCache returns a new ProjectRoot and ProjectProperties with the fields from m.
36+
func propertiesFromCache(m *pb.ProjectProperties) (ProjectRoot, ProjectProperties, error) {
3637
ip := ProjectRoot(m.Root)
3738
var pp ProjectProperties
3839
pp.Source = m.Source
3940

4041
if m.Constraint == nil {
4142
pp.Constraint = Any()
4243
} else {
43-
c, err := m.Constraint.asConstraint()
44+
c, err := constraintFromCache(m.Constraint)
4445
if err != nil {
4546
return "", ProjectProperties{}, err
4647
}
@@ -52,8 +53,8 @@ func (m *ProjectPropertiesMsg) properties() (ProjectRoot, ProjectProperties, err
5253

5354
// projectPropertiesMsgs is a convenience tuple.
5455
type projectPropertiesMsgs struct {
55-
pp ProjectPropertiesMsg
56-
c ConstraintMsg
56+
pp pb.ProjectProperties
57+
c pb.Constraint
5758
}
5859

5960
// copyFrom sets the ProjectPropertiesMsg fields from ip and pp.
@@ -174,12 +175,12 @@ func cacheGetManifest(b *bolt.Bucket) (RootManifest, error) {
174175

175176
// Constraints
176177
if cs := b.Bucket(cacheKeyC); cs != nil {
177-
var msg ProjectPropertiesMsg
178+
var msg pb.ProjectProperties
178179
err := cs.ForEach(func(_, v []byte) error {
179180
if err := proto.Unmarshal(v, &msg); err != nil {
180181
return err
181182
}
182-
ip, pp, err := msg.properties()
183+
ip, pp, err := propertiesFromCache(&msg)
183184
if err != nil {
184185
return err
185186
}
@@ -204,12 +205,12 @@ func cacheGetManifest(b *bolt.Bucket) (RootManifest, error) {
204205

205206
// Overrides
206207
if os := b.Bucket(cacheKeyO); os != nil {
207-
var msg ProjectPropertiesMsg
208+
var msg pb.ProjectProperties
208209
err := os.ForEach(func(_, v []byte) error {
209210
if err := proto.Unmarshal(v, &msg); err != nil {
210211
return err
211212
}
212-
ip, pp, err := msg.properties()
213+
ip, pp, err := propertiesFromCache(&msg)
213214
if err != nil {
214215
return err
215216
}
@@ -236,7 +237,7 @@ func cacheGetManifest(b *bolt.Bucket) (RootManifest, error) {
236237
}
237238

238239
// copyTo returns a serializable representation of lp.
239-
func (lp LockedProject) copyTo(msg *LockedProjectMsg, c *ConstraintMsg) {
240+
func (lp LockedProject) copyTo(msg *pb.LockedProject, c *pb.Constraint) {
240241
if lp.v == nil {
241242
msg.UnpairedVersion = nil
242243
} else {
@@ -249,12 +250,12 @@ func (lp LockedProject) copyTo(msg *LockedProjectMsg, c *ConstraintMsg) {
249250
msg.Packages = lp.pkgs
250251
}
251252

252-
// toLockedProject returns a new LockedProject with fields from m.
253-
func (m LockedProjectMsg) toLockedProject() (LockedProject, error) {
253+
// lockedProjectFromCache returns a new LockedProject with fields from m.
254+
func lockedProjectFromCache(m *pb.LockedProject) (LockedProject, error) {
254255
var uv UnpairedVersion
255256
var err error
256257
if m.UnpairedVersion != nil {
257-
uv, err = m.UnpairedVersion.asUnpairedVersion()
258+
uv, err = unpairedVersionFromCache(m.UnpairedVersion)
258259
if err != nil {
259260
return LockedProject{}, err
260261
}
@@ -286,8 +287,8 @@ func cachePutLock(b *bolt.Bucket, l Lock) error {
286287
return err
287288
}
288289
key := make(nuts.Key, nuts.KeyLen(uint64(len(projects)-1)))
289-
var msg LockedProjectMsg
290-
var cMsg ConstraintMsg
290+
var msg pb.LockedProject
291+
var cMsg pb.Constraint
291292
for i, lp := range projects {
292293
lp.copyTo(&msg, &cMsg)
293294
v, err := proto.Marshal(&msg)
@@ -310,12 +311,12 @@ func cacheGetLock(b *bolt.Bucket) (*safeLock, error) {
310311
h: b.Get(cacheKeyH),
311312
}
312313
if locked := b.Bucket(cacheKeyL); locked != nil {
313-
var msg LockedProjectMsg
314+
var msg pb.LockedProject
314315
err := locked.ForEach(func(_, v []byte) error {
315316
if err := proto.Unmarshal(v, &msg); err != nil {
316317
return err
317318
}
318-
lp, err := msg.toLockedProject()
319+
lp, err := lockedProjectFromCache(&msg)
319320
if err != nil {
320321
return err
321322
}

‎internal/gps/source_cache_bolt_encode_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"github.com/golang/protobuf/proto"
1313
)
1414

15-
func TestCacheEncodingProjectProperties(t *testing.T) {
15+
func TestPropertiesFromCache(t *testing.T) {
1616
for _, test := range []struct {
1717
name string
1818
ip ProjectRoot
@@ -40,7 +40,7 @@ func TestCacheEncodingProjectProperties(t *testing.T) {
4040
if err := proto.Unmarshal(v, &buf.pp); err != nil {
4141
t.Fatal(err)
4242
} else {
43-
ip, pp, err := buf.pp.properties()
43+
ip, pp, err := propertiesFromCache(&buf.pp)
4444
if err != nil {
4545
t.Fatal(err)
4646
}
@@ -58,7 +58,7 @@ func TestCacheEncodingProjectProperties(t *testing.T) {
5858
}
5959
}
6060

61-
func TestCacheEncodingTimestampedKey(t *testing.T) {
61+
func TestCacheTimestampedKey(t *testing.T) {
6262
pre := byte('p')
6363
for _, test := range []struct {
6464
ts time.Time

‎internal/gps/source_cache_gen.go

-2
This file was deleted.

‎internal/gps/version.go

+11-10
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"sort"
1010

1111
"github.com/Masterminds/semver"
12+
"github.com/golang/dep/internal/gps/internal/pb"
1213
)
1314

1415
// VersionType indicates a type for a Version that conveys some additional
@@ -198,8 +199,8 @@ func (r Revision) identical(c Constraint) bool {
198199
return r == r2
199200
}
200201

201-
func (r Revision) copyTo(msg *ConstraintMsg) {
202-
msg.Type = ConstraintMsg_Revision
202+
func (r Revision) copyTo(msg *pb.Constraint) {
203+
msg.Type = pb.Constraint_Revision
203204
msg.Value = string(r)
204205
}
205206

@@ -295,11 +296,11 @@ func (v branchVersion) identical(c Constraint) bool {
295296
return v == v2
296297
}
297298

298-
func (v branchVersion) copyTo(msg *ConstraintMsg) {
299+
func (v branchVersion) copyTo(msg *pb.Constraint) {
299300
if v.isDefault {
300-
msg.Type = ConstraintMsg_DefaultBranch
301+
msg.Type = pb.Constraint_DefaultBranch
301302
} else {
302-
msg.Type = ConstraintMsg_Branch
303+
msg.Type = pb.Constraint_Branch
303304
}
304305
msg.Value = v.name
305306
}
@@ -393,8 +394,8 @@ func (v plainVersion) identical(c Constraint) bool {
393394
return v == v2
394395
}
395396

396-
func (v plainVersion) copyTo(msg *ConstraintMsg) {
397-
msg.Type = ConstraintMsg_Version
397+
func (v plainVersion) copyTo(msg *pb.Constraint) {
398+
msg.Type = pb.Constraint_Version
398399
msg.Value = string(v)
399400
}
400401

@@ -497,8 +498,8 @@ func (v semVersion) identical(c Constraint) bool {
497498
return v == v2
498499
}
499500

500-
func (v semVersion) copyTo(msg *ConstraintMsg) {
501-
msg.Type = ConstraintMsg_Semver
501+
func (v semVersion) copyTo(msg *pb.Constraint) {
502+
msg.Type = pb.Constraint_Semver
502503
msg.Value = v.String() //TODO better encoding which doesn't require re-parsing
503504
}
504505

@@ -614,7 +615,7 @@ func (v versionPair) identical(c Constraint) bool {
614615
return v.v.identical(v2.v)
615616
}
616617

617-
func (v versionPair) copyTo(*ConstraintMsg) {
618+
func (v versionPair) copyTo(*pb.Constraint) {
618619
panic("versionPair should never be serialized; it is solver internal-only")
619620
}
620621

‎internal/gps/version_unifier.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
package gps
66

7+
import "github.com/golang/dep/internal/gps/internal/pb"
8+
79
// versionUnifier facilitates cross-type version comparison and set operations.
810
type versionUnifier struct {
911
b sourceBridge
@@ -295,6 +297,6 @@ outter:
295297
return true
296298
}
297299

298-
func (vtu versionTypeUnion) copyTo(*ConstraintMsg) {
300+
func (vtu versionTypeUnion) copyTo(*pb.Constraint) {
299301
panic("versionTypeUnion should never be serialized; it is solver internal-only")
300302
}

0 commit comments

Comments
 (0)
This repository has been archived.