pkg/version: lint and add comments
This commit is contained in:
parent
63363f2d49
commit
6f66d8a30f
1 changed files with 24 additions and 18 deletions
|
@ -5,53 +5,59 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Version provides utility methods for comparing versions.
|
||||||
type Version string
|
type Version string
|
||||||
|
|
||||||
func (me Version) compareTo(other Version) int {
|
func (v Version) compareTo(other Version) int {
|
||||||
var (
|
var (
|
||||||
meTab = strings.Split(string(me), ".")
|
currTab = strings.Split(string(v), ".")
|
||||||
otherTab = strings.Split(string(other), ".")
|
otherTab = strings.Split(string(other), ".")
|
||||||
)
|
)
|
||||||
|
|
||||||
max := len(meTab)
|
max := len(currTab)
|
||||||
if len(otherTab) > max {
|
if len(otherTab) > max {
|
||||||
max = len(otherTab)
|
max = len(otherTab)
|
||||||
}
|
}
|
||||||
for i := 0; i < max; i++ {
|
for i := 0; i < max; i++ {
|
||||||
var meInt, otherInt int
|
var currInt, otherInt int
|
||||||
|
|
||||||
if len(meTab) > i {
|
if len(currTab) > i {
|
||||||
meInt, _ = strconv.Atoi(meTab[i])
|
currInt, _ = strconv.Atoi(currTab[i])
|
||||||
}
|
}
|
||||||
if len(otherTab) > i {
|
if len(otherTab) > i {
|
||||||
otherInt, _ = strconv.Atoi(otherTab[i])
|
otherInt, _ = strconv.Atoi(otherTab[i])
|
||||||
}
|
}
|
||||||
if meInt > otherInt {
|
if currInt > otherInt {
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
if otherInt > meInt {
|
if otherInt > currInt {
|
||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (me Version) LessThan(other Version) bool {
|
// LessThan checks if a version is less than another version
|
||||||
return me.compareTo(other) == -1
|
func (v Version) LessThan(other Version) bool {
|
||||||
|
return v.compareTo(other) == -1
|
||||||
}
|
}
|
||||||
|
|
||||||
func (me Version) LessThanOrEqualTo(other Version) bool {
|
// LessThanOrEqualTo checks if a version is less than or equal to another
|
||||||
return me.compareTo(other) <= 0
|
func (v Version) LessThanOrEqualTo(other Version) bool {
|
||||||
|
return v.compareTo(other) <= 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (me Version) GreaterThan(other Version) bool {
|
// GreaterThan checks if a version is greater than another one
|
||||||
return me.compareTo(other) == 1
|
func (v Version) GreaterThan(other Version) bool {
|
||||||
|
return v.compareTo(other) == 1
|
||||||
}
|
}
|
||||||
|
|
||||||
func (me Version) GreaterThanOrEqualTo(other Version) bool {
|
// GreaterThanOrEqualTo checks ia version is greater than or equal to another
|
||||||
return me.compareTo(other) >= 0
|
func (v Version) GreaterThanOrEqualTo(other Version) bool {
|
||||||
|
return v.compareTo(other) >= 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (me Version) Equal(other Version) bool {
|
// Equal checks if a version is equal to another
|
||||||
return me.compareTo(other) == 0
|
func (v Version) Equal(other Version) bool {
|
||||||
|
return v.compareTo(other) == 0
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue