Browse Source

Documentation, code cleaning

Benedikt Lang 11 years ago
parent
commit
31b736133b
4 changed files with 50 additions and 49 deletions
  1. +39
    -42
      semver.go
  2. +4
    -0
      sort.go
  3. +2
    -2
      sql.go
  4. +5
    -5
      sql_test.go

+ 39
- 42
semver.go View File

@ -13,13 +13,14 @@ const (
alphanum = alphas + numbers
)
// Latest fully supported spec version
var SPEC_VERSION = Version{
// SpecVersion is the latest fully supported spec version of semver
var SpecVersion = Version{
Major: 2,
Minor: 0,
Patch: 0,
}
// Version represents a semver compatible version
type Version struct {
Major uint64
Minor uint64
@ -60,52 +61,52 @@ func (v Version) String() string {
return string(b)
}
// Checks if v is equal to o.
// Equals checks if v is equal to o.
func (v Version) Equals(o Version) bool {
return (v.Compare(o) == 0)
}
// Checks if v is equal to o.
// EQ checks if v is equal to o.
func (v Version) EQ(o Version) bool {
return (v.Compare(o) == 0)
}
// Checks if v is not equal to o.
// NE checks if v is not equal to o.
func (v Version) NE(o Version) bool {
return (v.Compare(o) != 0)
}
// Checks if v is greater than o.
// GT checks if v is greater than o.
func (v Version) GT(o Version) bool {
return (v.Compare(o) == 1)
}
// Checks if v is greater than or equal to o.
// GTE checks if v is greater than or equal to o.
func (v Version) GTE(o Version) bool {
return (v.Compare(o) >= 0)
}
// Checks if v is greater than or equal to o.
// GE checks if v is greater than or equal to o.
func (v Version) GE(o Version) bool {
return (v.Compare(o) >= 0)
}
// Checks if v is less than o.
// LT checks if v is less than o.
func (v Version) LT(o Version) bool {
return (v.Compare(o) == -1)
}
// Checks if v is less than or equal to o.
// LTE checks if v is less than or equal to o.
func (v Version) LTE(o Version) bool {
return (v.Compare(o) <= 0)
}
// Checks if v is less than or equal to o.
// LE checks if v is less than or equal to o.
func (v Version) LE(o Version) bool {
return (v.Compare(o) <= 0)
}
// Compares Versions v to o:
// Compare compares Versions v to o:
// -1 == v is less than o
// 0 == v is equal to o
// 1 == v is greater than o
@ -113,23 +114,20 @@ func (v Version) Compare(o Version) int {
if v.Major != o.Major {
if v.Major > o.Major {
return 1
} else {
return -1
}
return -1
}
if v.Minor != o.Minor {
if v.Minor > o.Minor {
return 1
} else {
return -1
}
return -1
}
if v.Patch != o.Patch {
if v.Patch > o.Patch {
return 1
} else {
return -1
}
return -1
}
// Quick comparison if a version has no prerelease versions
@ -139,32 +137,31 @@ func (v Version) Compare(o Version) int {
return 1
} else if len(v.Pre) > 0 && len(o.Pre) == 0 {
return -1
} else {
i := 0
for ; i < len(v.Pre) && i < len(o.Pre); i++ {
if comp := v.Pre[i].Compare(o.Pre[i]); comp == 0 {
continue
} else if comp == 1 {
return 1
} else {
return -1
}
}
}
// If all pr versions are the equal but one has further prversion, this one greater
if i == len(v.Pre) && i == len(o.Pre) {
return 0
} else if i == len(v.Pre) && i < len(o.Pre) {
return -1
} else {
i := 0
for ; i < len(v.Pre) && i < len(o.Pre); i++ {
if comp := v.Pre[i].Compare(o.Pre[i]); comp == 0 {
continue
} else if comp == 1 {
return 1
} else {
return -1
}
}
// If all pr versions are the equal but one has further prversion, this one greater
if i == len(v.Pre) && i == len(o.Pre) {
return 0
} else if i == len(v.Pre) && i < len(o.Pre) {
return -1
} else {
return 1
}
}
// Validates v and returns error in case
// Validate validates v and returns error in case
func (v Version) Validate() error {
// Major, Minor, Patch already validated using uint64
@ -301,14 +298,14 @@ func MustParse(s string) Version {
return v
}
// PreRelease Version
// PRVersion represents a PreRelease Version
type PRVersion struct {
VersionStr string
VersionNum uint64
IsNum bool
}
// Creates a new valid prerelease version
// NewPRVersion creates a new valid prerelease version
func NewPRVersion(s string) (PRVersion, error) {
if len(s) == 0 {
return PRVersion{}, errors.New("Prerelease is empty")
@ -335,12 +332,12 @@ func NewPRVersion(s string) (PRVersion, error) {
return v, nil
}
// Is pre release version numeric?
// IsNumeric checks if prerelease-version is numeric
func (v PRVersion) IsNumeric() bool {
return v.IsNum
}
// Compares PreRelease Versions v to o:
// Compare compares two PreRelease Versions v and o:
// -1 == v is less than o
// 0 == v is equal to o
// 1 == v is greater than o
@ -386,7 +383,7 @@ func hasLeadingZeroes(s string) bool {
return len(s) > 1 && s[0] == '0'
}
// Creates a new valid build version
// NewBuildVersion creates a new valid build version
func NewBuildVersion(s string) (string, error) {
if len(s) == 0 {
return "", errors.New("Buildversion is empty")


+ 4
- 0
sort.go View File

@ -4,16 +4,20 @@ import (
"sort"
)
// Versions represents multiple versions.
type Versions []Version
// Len returns length of version collection
func (s Versions) Len() int {
return len(s)
}
// Swap swaps two versions inside the collection by its indices
func (s Versions) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
// Less checks if version at index i is less than version at index j
func (s Versions) Less(i, j int) bool {
return s[i].LT(s[j])
}


+ 2
- 2
sql.go View File

@ -25,6 +25,6 @@ func (v *Version) Scan(src interface{}) (err error) {
}
// Value implements the database/sql/driver.Valuer interface.
func (s Version) Value() (driver.Value, error) {
return s.String(), nil
func (v Version) Value() (driver.Value, error) {
return v.String(), nil
}

+ 5
- 5
sql_test.go View File

@ -11,11 +11,11 @@ type scanTest struct {
}
var scanTests = []scanTest{
scanTest{"1.2.3", false, "1.2.3"},
scanTest{[]byte("1.2.3"), false, "1.2.3"},
scanTest{7, true, ""},
scanTest{7e4, true, ""},
scanTest{true, true, ""},
{"1.2.3", false, "1.2.3"},
{[]byte("1.2.3"), false, "1.2.3"},
{7, true, ""},
{7e4, true, ""},
{true, true, ""},
}
func TestScanString(t *testing.T) {


Loading…
Cancel
Save