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

Commit

Permalink
gps: source cache: move protobufs to internal package
Browse files Browse the repository at this point in the history
  • Loading branch information
jmank88 committed Sep 12, 2017
1 parent 5847aa6 commit e78bc49
Show file tree
Hide file tree
Showing 13 changed files with 304 additions and 295 deletions.
7 changes: 4 additions & 3 deletions internal/gps/constraint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"testing"

"github.com/golang/dep/internal/gps/internal/pb"
"github.com/golang/protobuf/proto"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -976,7 +977,7 @@ func TestConstraintEncoding(t *testing.T) {
{"rev", Revision("test")},
} {
t.Run(test.name, func(t *testing.T) {
var msg ConstraintMsg
var msg pb.Constraint
test.c.copyTo(&msg)
b, err := proto.Marshal(&msg)
if err != nil {
Expand All @@ -986,15 +987,15 @@ func TestConstraintEncoding(t *testing.T) {
if err := proto.Unmarshal(b, &msg); err != nil {
t.Fatal(err)
}
got, err := msg.asConstraint()
got, err := constraintFromCache(&msg)
if err != nil {
t.Error("failed to decode:", err)
} else if !got.identical(test.c) {
t.Errorf("decoded non-identical Constraint:\n\t(GOT): %#v\n\t(WNT): %#v", got, test.c)
}

if _, ok := test.c.(UnpairedVersion); ok {
got, err := msg.asUnpairedVersion()
got, err := unpairedVersionFromCache(&msg)
if err != nil {
t.Error("failed to decode:", err)
} else if !got.identical(test.c) {
Expand Down
41 changes: 21 additions & 20 deletions internal/gps/constraints.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"sort"

"github.com/Masterminds/semver"
"github.com/golang/dep/internal/gps/internal/pb"
)

var (
Expand Down Expand Up @@ -53,9 +54,9 @@ type Constraint interface {
// design goal of the system.
typedString() string

// copyTo copies fields into a serializable representation which can be deserialized into
// an identical Constraint with decodeConstraint.
copyTo(*ConstraintMsg)
// copyTo copies fields into a serializable representation which can be
// converted back into an identical Constraint with constraintFromCache.
copyTo(*pb.Constraint)

// identical returns true if the constraints are identical.
//
Expand All @@ -66,35 +67,35 @@ type Constraint interface {
identical(Constraint) bool
}

// asConstraint returns a Constraint identical to the one which produced m.
func (m *ConstraintMsg) asConstraint() (Constraint, error) {
// constraintFromCache returns a Constraint identical to the one which produced m.
func constraintFromCache(m *pb.Constraint) (Constraint, error) {
switch m.Type {
case ConstraintMsg_Revision:
case pb.Constraint_Revision:
return Revision(m.Value), nil
case ConstraintMsg_Branch:
case pb.Constraint_Branch:
return NewBranch(m.Value), nil
case ConstraintMsg_DefaultBranch:
case pb.Constraint_DefaultBranch:
return newDefaultBranch(m.Value), nil
case ConstraintMsg_Version:
case pb.Constraint_Version:
return plainVersion(m.Value), nil
case ConstraintMsg_Semver:
case pb.Constraint_Semver:
return NewSemverConstraint(m.Value)

default:
return nil, fmt.Errorf("unrecognized Constraint type: %#v", m)
}
}

// asUnpairedVersion returns an UnpairedVersion identical to the one which produced m.
func (m *ConstraintMsg) asUnpairedVersion() (UnpairedVersion, error) {
// unpairedVersionFromCache returns an UnpairedVersion identical to the one which produced m.
func unpairedVersionFromCache(m *pb.Constraint) (UnpairedVersion, error) {
switch m.Type {
case ConstraintMsg_Branch:
case pb.Constraint_Branch:
return NewBranch(m.Value), nil
case ConstraintMsg_DefaultBranch:
case pb.Constraint_DefaultBranch:
return newDefaultBranch(m.Value), nil
case ConstraintMsg_Version:
case pb.Constraint_Version:
return plainVersion(m.Value), nil
case ConstraintMsg_Semver:
case pb.Constraint_Semver:
sv, err := semver.NewVersion(m.Value)
if err != nil {
return nil, err
Expand Down Expand Up @@ -232,8 +233,8 @@ func (c semverConstraint) identical(c2 Constraint) bool {
return c.c.String() == sc2.c.String()
}

func (c semverConstraint) copyTo(msg *ConstraintMsg) {
msg.Type = ConstraintMsg_Semver
func (c semverConstraint) copyTo(msg *pb.Constraint) {
msg.Type = pb.Constraint_Semver
msg.Value = c.String()
}

Expand Down Expand Up @@ -280,7 +281,7 @@ func (anyConstraint) identical(c Constraint) bool {
return IsAny(c)
}

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

Expand Down Expand Up @@ -317,7 +318,7 @@ func (noneConstraint) identical(c Constraint) bool {
return ok
}

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

Expand Down
4 changes: 4 additions & 0 deletions internal/gps/internal/pb/pb.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Package pb provides generated Protocol Buffers for cache serialization.

//go:generate protoc --go_out=. source_cache.proto
package pb
199 changes: 199 additions & 0 deletions internal/gps/internal/pb/source_cache.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions internal/gps/internal/pb/source_cache.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
syntax = "proto3";
package pb;

// Constraint is a serializable representation of a gps.Constraint or gps.UnpairedVersion.
message Constraint {
enum Type {
Revision = 0;
Branch = 1;
DefaultBranch = 2;
Version = 3;
Semver = 4;
}
Type type = 1;
string value = 2;
//TODO strongly typed Semver field
}

// ProjectProperties is a serializable representation of gps.ProjectRoot and gps.ProjectProperties.
message ProjectProperties {
string root = 1;
string source = 2;
Constraint constraint = 3;
}

// LockedProject is a serializable representation of gps.LockedProject.
message LockedProject {
string root = 1;
string source = 2;
Constraint unpairedVersion = 3;
string revision = 4;
repeated string packages = 5;
}
Loading

0 comments on commit e78bc49

Please sign in to comment.