Skip to content

Commit

Permalink
Use well-typed return for Version.Type()
Browse files Browse the repository at this point in the history
  • Loading branch information
sdboyer committed Dec 16, 2016
1 parent 680c17e commit 41fd676
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
2 changes: 1 addition & 1 deletion bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ func (vtu versionTypeUnion) String() string {
// This should generally not be called, but is required for the interface. If it
// is called, we have a bigger problem (the type has escaped the solver); thus,
// panic.
func (vtu versionTypeUnion) Type() string {
func (vtu versionTypeUnion) Type() VersionType {
panic("versionTypeUnion should never need to answer a Type() call; it is solver internal-only")
}

Expand Down
32 changes: 22 additions & 10 deletions version.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@ import (
"github.com/Masterminds/semver"
)

// VersionType indicates a type for a Version that conveys some additional
// semantics beyond that which is literally embedded on the Go type.
type VersionType uint8

// VersionTypes for the four major classes of version we deal with
const (
IsRevision VersionType = iota
IsVersion
IsSemver
IsBranch
)

// Version represents one of the different types of versions used by gps.
//
// Version composes Constraint, because all versions can be used as a constraint
Expand All @@ -22,7 +34,7 @@ type Version interface {
Constraint

// Indicates the type of version - Revision, Branch, Version, or Semver
Type() string
Type() VersionType
}

// PairedVersion represents a normal Version, but paired with its corresponding,
Expand Down Expand Up @@ -108,8 +120,8 @@ func (r Revision) String() string {
}

// Type indicates the type of version - for revisions, "revision".
func (r Revision) Type() string {
return "revision"
func (r Revision) Type() VersionType {
return IsRevision
}

// Matches is the Revision acting as a constraint; it checks to see if the provided
Expand Down Expand Up @@ -179,8 +191,8 @@ func (v branchVersion) String() string {
return string(v.name)
}

func (v branchVersion) Type() string {
return "branch"
func (v branchVersion) Type() VersionType {
return IsBranch
}

func (v branchVersion) Matches(v2 Version) bool {
Expand Down Expand Up @@ -252,8 +264,8 @@ func (v plainVersion) String() string {
return string(v)
}

func (v plainVersion) Type() string {
return "version"
func (v plainVersion) Type() VersionType {
return IsVersion
}

func (v plainVersion) Matches(v2 Version) bool {
Expand Down Expand Up @@ -331,8 +343,8 @@ func (v semVersion) String() string {
return str
}

func (v semVersion) Type() string {
return "semver"
func (v semVersion) Type() VersionType {
return IsSemver
}

func (v semVersion) Matches(v2 Version) bool {
Expand Down Expand Up @@ -411,7 +423,7 @@ func (v versionPair) String() string {
return v.v.String()
}

func (v versionPair) Type() string {
func (v versionPair) Type() VersionType {
return v.v.Type()
}

Expand Down

0 comments on commit 41fd676

Please sign in to comment.