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

Commit

Permalink
Merge pull request #1127 from jmank88/source_cache_proto
Browse files Browse the repository at this point in the history
gps: source cache: protobuf integration
  • Loading branch information
jmank88 authored Sep 22, 2017
2 parents c564e78 + d67284a commit 59eca1b
Show file tree
Hide file tree
Showing 63 changed files with 15,790 additions and 414 deletions.
1 change: 1 addition & 0 deletions .codeclimate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ exclude_paths:
- internal/gps/_testdata
- cmd/dep/testdata
- testdata
- internal/gps/internal/pb
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"

[[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\)$"\
-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

0 comments on commit 59eca1b

Please sign in to comment.