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

gps: source cache: protobuf integration #1127

Merged
merged 3 commits into from
Sep 22, 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
1 change: 1 addition & 0 deletions .codeclimate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ exclude_paths:
- internal/gps/_testdata
- cmd/dep/testdata
- testdata
- internal/gps/internal/pb
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something like **.pb.go (if legal here) would be more general and continue to work as things change.

14 changes: 13 additions & 1 deletion Gopkg.lock

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

8 changes: 8 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,11 @@
[[constraint]]
name = "github.com/boltdb/bolt"
version = "1.0.0"

[[constraint]]
name = "github.com/jmank88/nuts"
version = "0.2.0"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't have to vendor this if we don't want to pollute. Usages are few enough to copy in.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'm fine with bringing it in


[[constraint]]
name = "github.com/golang/protobuf"
branch = "master"
2 changes: 1 addition & 1 deletion hack/validate-licence.bash
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
set -e

go build ./hack/licenseok
find . -path ./vendor -prune -o -type f -name "*.go"\
find . -path ./vendor -prune -o -regex ".+\.pb\.go$" -prune -o -type f -regex ".*\.\(go\|proto\)$"\
Copy link
Collaborator Author

@jmank88 jmank88 Sep 18, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The intention was to include *.proto files and exclude the generated *.pb.go files (but not pb.go!). I stumbled my way into this, but perhaps there is a simpler or more readable way to do so.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i have no immediate ideas about better ways of doing this; i'm fine with a slightly wonky regex.

-printf '%P\n' | xargs ./licenseok
43 changes: 43 additions & 0 deletions internal/gps/constraint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"fmt"
"testing"

"github.com/golang/dep/internal/gps/internal/pb"
"github.com/golang/protobuf/proto"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -962,3 +964,44 @@ func testSemverConstraint(t *testing.T, body string) Constraint {
}
return c
}

func TestConstraintEncoding(t *testing.T) {
for _, test := range []struct {
name string
c Constraint
}{
{"defaultBranch", newDefaultBranch("test")},
{"branch", NewBranch("test")},
{"ver", NewVersion("test")},
{"semver", testSemverConstraint(t, "^1.0.0")},
{"rev", Revision("test")},
} {
t.Run(test.name, func(t *testing.T) {
var msg pb.Constraint
test.c.copyTo(&msg)
b, err := proto.Marshal(&msg)
if err != nil {
t.Fatal(err)
}

if err := proto.Unmarshal(b, &msg); err != nil {
t.Fatal(err)
}
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 := unpairedVersionFromCache(&msg)
if err != nil {
t.Error("failed to decode:", err)
} else if !got.identical(test.c) {
t.Errorf("decoded non-identical UnpairedVersion:\n\t(GOT): %#v\n\t(WNT): %#v", got, test.c)
}
}
})
}
}
58 changes: 58 additions & 0 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,6 +54,10 @@ type Constraint interface {
// design goal of the system.
typedString() string

// 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.
//
// Identical Constraints behave identically for all methods defined by the
Expand All @@ -62,6 +67,46 @@ type Constraint interface {
identical(Constraint) bool
}

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

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

// unpairedVersionFromCache returns an UnpairedVersion identical to the one which produced m.
func unpairedVersionFromCache(m *pb.Constraint) (UnpairedVersion, error) {
switch m.Type {
case pb.Constraint_Branch:
return NewBranch(m.Value), nil
case pb.Constraint_DefaultBranch:
return newDefaultBranch(m.Value), nil
case pb.Constraint_Version:
return plainVersion(m.Value), nil
case pb.Constraint_Semver:
sv, err := semver.NewVersion(m.Value)
if err != nil {
return nil, err
}
return semVersion{sv: sv}, nil

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

// NewSemverConstraint attempts to construct a semver Constraint object from the
// input string.
//
Expand Down Expand Up @@ -188,6 +233,11 @@ func (c semverConstraint) identical(c2 Constraint) bool {
return c.c.String() == sc2.c.String()
}

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

// IsAny indicates if the provided constraint is the wildcard "Any" constraint.
func IsAny(c Constraint) bool {
_, ok := c.(anyConstraint)
Expand Down Expand Up @@ -231,6 +281,10 @@ func (anyConstraint) identical(c Constraint) bool {
return IsAny(c)
}

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

// noneConstraint is the empty set - it matches no versions. It mirrors the
// behavior of the semver package's none type.
type noneConstraint struct{}
Expand Down Expand Up @@ -264,6 +318,10 @@ func (noneConstraint) identical(c Constraint) bool {
return ok
}

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

// A ProjectConstraint combines a ProjectIdentifier with a Constraint. It
// indicates that, if packages contained in the ProjectIdentifier enter the
// depgraph, they must do so at a version that is allowed by the Constraint.
Expand Down
8 changes: 8 additions & 0 deletions internal/gps/internal/pb/pb.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Package pb provides generated Protocol Buffers for cache serialization.
package pb

//go:generate protoc --go_out=. source_cache.proto
Loading