Skip to content

Commit

Permalink
Merge pull request #112 from mattfarina/restore-old-interfaces
Browse files Browse the repository at this point in the history
Restoring v1 Go package API
  • Loading branch information
mattfarina authored Sep 3, 2019
2 parents 0c77334 + 6c684ba commit 5bc3b91
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 81 deletions.
18 changes: 9 additions & 9 deletions benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,9 @@ func benchNewVersion(v string, b *testing.B) {
}
}

func benchCoerceNewVersion(v string, b *testing.B) {
func benchStrictNewVersion(v string, b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = semver.CoerceNewVersion(v)
_, _ = semver.StrictNewVersion(v)
}
}

Expand All @@ -209,7 +209,7 @@ func BenchmarkNewVersionSimple(b *testing.B) {
func BenchmarkCoerceNewVersionSimple(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
benchCoerceNewVersion("1.0.0", b)
benchStrictNewVersion("1.0.0", b)
}

func BenchmarkNewVersionPre(b *testing.B) {
Expand All @@ -218,10 +218,10 @@ func BenchmarkNewVersionPre(b *testing.B) {
benchNewVersion("1.0.0-alpha", b)
}

func BenchmarkCoerceNewVersionPre(b *testing.B) {
func BenchmarkStrictNewVersionPre(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
benchCoerceNewVersion("1.0.0-alpha", b)
benchStrictNewVersion("1.0.0-alpha", b)
}

func BenchmarkNewVersionMeta(b *testing.B) {
Expand All @@ -230,10 +230,10 @@ func BenchmarkNewVersionMeta(b *testing.B) {
benchNewVersion("1.0.0+metadata", b)
}

func BenchmarkCoerceNewVersionMeta(b *testing.B) {
func BenchmarkStrictNewVersionMeta(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
benchCoerceNewVersion("1.0.0+metadata", b)
benchStrictNewVersion("1.0.0+metadata", b)
}

func BenchmarkNewVersionMetaDash(b *testing.B) {
Expand All @@ -242,8 +242,8 @@ func BenchmarkNewVersionMetaDash(b *testing.B) {
benchNewVersion("1.0.0-alpha.1+meta.data", b)
}

func BenchmarkCoerceNewVersionMetaDash(b *testing.B) {
func BenchmarkStrictNewVersionMetaDash(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
benchCoerceNewVersion("1.0.0-alpha.1+meta.data", b)
benchStrictNewVersion("1.0.0-alpha.1+meta.data", b)
}
2 changes: 1 addition & 1 deletion collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package semver
// Collection is a collection of Version instances and implements the sort
// interface. See the sort package for more details.
// https://golang.org/pkg/sort/
type Collection []Version
type Collection []*Version

// Len returns the length of a collection. The number of Version instances
// on the slice.
Expand Down
4 changes: 2 additions & 2 deletions collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ func TestCollection(t *testing.T) {
"0.4.2",
}

vs := make([]Version, len(raw))
vs := make([]*Version, len(raw))
for i, r := range raw {
v, err := CoerceNewVersion(r)
v, err := NewVersion(r)
if err != nil {
t.Errorf("Error parsing version: %s", err)
}
Expand Down
28 changes: 14 additions & 14 deletions constraints.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func NewConstraint(c string) (*Constraints, error) {
}

// Check tests if a version satisfies the constraints.
func (cs Constraints) Check(v Version) bool {
func (cs Constraints) Check(v *Version) bool {
// loop over the ORs and check the inner ANDs
for _, o := range cs.constraints {
joy := true
Expand All @@ -62,7 +62,7 @@ func (cs Constraints) Check(v Version) bool {

// Validate checks if a version satisfies a constraint. If not a slice of
// reasons for the failure are returned in addition to a bool.
func (cs Constraints) Validate(v Version) (bool, []error) {
func (cs Constraints) Validate(v *Version) (bool, []error) {
// loop over the ORs and check the inner ANDs
var e []error

Expand Down Expand Up @@ -160,7 +160,7 @@ type constraint struct {

// The version used in the constraint check. For example, if a constraint
// is '<= 2.0.0' the con a version instance representing 2.0.0.
con Version
con *Version

// The original parsed version (e.g., 4.x from != 4.x)
orig string
Expand All @@ -172,11 +172,11 @@ type constraint struct {
}

// Check if a version meets the constraint
func (c *constraint) check(v Version) bool {
func (c *constraint) check(v *Version) bool {
return c.function(v, c)
}

type cfunc func(v Version, c *constraint) bool
type cfunc func(v *Version, c *constraint) bool

func parseConstraint(c string) (*constraint, error) {
m := constraintRegex.FindStringSubmatch(c)
Expand All @@ -202,7 +202,7 @@ func parseConstraint(c string) (*constraint, error) {
ver = fmt.Sprintf("%s%s.0%s", m[3], m[4], m[6])
}

con, err := CoerceNewVersion(ver)
con, err := NewVersion(ver)
if err != nil {

// The constraintRegex should catch any regex parsing errors. So,
Expand All @@ -223,7 +223,7 @@ func parseConstraint(c string) (*constraint, error) {
}

// Constraint functions
func constraintNotEqual(v Version, c *constraint) bool {
func constraintNotEqual(v *Version, c *constraint) bool {
if c.dirty {

// If there is a pre-release on the version but the constraint isn't looking
Expand Down Expand Up @@ -254,7 +254,7 @@ func constraintNotEqual(v Version, c *constraint) bool {
return !v.Equal(c.con)
}

func constraintGreaterThan(v Version, c *constraint) bool {
func constraintGreaterThan(v *Version, c *constraint) bool {

// If there is a pre-release on the version but the constraint isn't looking
// for them assume that pre-releases are not compatible. See issue 21 for
Expand Down Expand Up @@ -286,7 +286,7 @@ func constraintGreaterThan(v Version, c *constraint) bool {
return v.Compare(c.con) == 1
}

func constraintLessThan(v Version, c *constraint) bool {
func constraintLessThan(v *Version, c *constraint) bool {
// If there is a pre-release on the version but the constraint isn't looking
// for them assume that pre-releases are not compatible. See issue 21 for
// more details.
Expand All @@ -297,7 +297,7 @@ func constraintLessThan(v Version, c *constraint) bool {
return v.Compare(c.con) < 0
}

func constraintGreaterThanEqual(v Version, c *constraint) bool {
func constraintGreaterThanEqual(v *Version, c *constraint) bool {

// If there is a pre-release on the version but the constraint isn't looking
// for them assume that pre-releases are not compatible. See issue 21 for
Expand All @@ -309,7 +309,7 @@ func constraintGreaterThanEqual(v Version, c *constraint) bool {
return v.Compare(c.con) >= 0
}

func constraintLessThanEqual(v Version, c *constraint) bool {
func constraintLessThanEqual(v *Version, c *constraint) bool {
// If there is a pre-release on the version but the constraint isn't looking
// for them assume that pre-releases are not compatible. See issue 21 for
// more details.
Expand All @@ -336,7 +336,7 @@ func constraintLessThanEqual(v Version, c *constraint) bool {
// ~1.2, ~1.2.x, ~>1.2, ~>1.2.x --> >=1.2.0, <1.3.0
// ~1.2.3, ~>1.2.3 --> >=1.2.3, <1.3.0
// ~1.2.0, ~>1.2.0 --> >=1.2.0, <1.3.0
func constraintTilde(v Version, c *constraint) bool {
func constraintTilde(v *Version, c *constraint) bool {
// If there is a pre-release on the version but the constraint isn't looking
// for them assume that pre-releases are not compatible. See issue 21 for
// more details.
Expand Down Expand Up @@ -368,7 +368,7 @@ func constraintTilde(v Version, c *constraint) bool {

// When there is a .x (dirty) status it automatically opts in to ~. Otherwise
// it's a straight =
func constraintTildeOrEqual(v Version, c *constraint) bool {
func constraintTildeOrEqual(v *Version, c *constraint) bool {
// If there is a pre-release on the version but the constraint isn't looking
// for them assume that pre-releases are not compatible. See issue 21 for
// more details.
Expand All @@ -389,7 +389,7 @@ func constraintTildeOrEqual(v Version, c *constraint) bool {
// ^1.2, ^1.2.x --> >=1.2.0, <2.0.0
// ^1.2.3 --> >=1.2.3, <2.0.0
// ^1.2.0 --> >=1.2.0, <2.0.0
func constraintCaret(v Version, c *constraint) bool {
func constraintCaret(v *Version, c *constraint) bool {
// If there is a pre-release on the version but the constraint isn't looking
// for them assume that pre-releases are not compatible. See issue 21 for
// more details.
Expand Down
10 changes: 5 additions & 5 deletions constraints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func TestConstraintCheck(t *testing.T) {
continue
}

v, err := CoerceNewVersion(tc.version)
v, err := NewVersion(tc.version)
if err != nil {
t.Errorf("err: %s", err)
continue
Expand Down Expand Up @@ -296,7 +296,7 @@ func TestConstraintsCheck(t *testing.T) {
continue
}

v, err := CoerceNewVersion(tc.version)
v, err := NewVersion(tc.version)
if err != nil {
t.Errorf("err: %s", err)
continue
Expand Down Expand Up @@ -432,7 +432,7 @@ func TestConstraintsValidate(t *testing.T) {
continue
}

v, err := CoerceNewVersion(tc.version)
v, err := NewVersion(tc.version)
if err != nil {
t.Errorf("err: %s", err)
continue
Expand All @@ -452,7 +452,7 @@ func TestConstraintsValidate(t *testing.T) {
// }
}

v, err := NewVersion("1.2.3")
v, err := StrictNewVersion("1.2.3")
if err != nil {
t.Errorf("err: %s", err)
}
Expand Down Expand Up @@ -517,7 +517,7 @@ func TestConstraintsValidate(t *testing.T) {
continue
}

v, err := NewVersion(tc.version)
v, err := StrictNewVersion(tc.version)
if err != nil {
t.Errorf("err: %s", err)
continue
Expand Down
Loading

0 comments on commit 5bc3b91

Please sign in to comment.