Files
version/version.go
T

60 lines
1.7 KiB
Go
Raw Normal View History

2024-07-11 22:04:40 +01:00
package version
import (
"fmt"
)
2024-07-11 22:59:09 +01:00
// Version is a structured representation of a version number.
2024-07-11 22:04:40 +01:00
type Version struct {
2024-07-11 22:59:09 +01:00
Major int // Major version number.
Minor int // Minor version number.
Patch int // Patch version number.
Extension string // Version extension, such as pre-release number or build metdata.
2024-07-11 22:04:40 +01:00
2024-07-11 22:59:09 +01:00
Text string // Original version string, if this version was created via the Parse function.
2024-07-11 22:04:40 +01:00
}
// Compare this version (a) with another version (b).
// This function returns -1 if a is less than b, 1 if a is greater than b, or 0 if a is equal to b.
//
// Extensions such as pre-release version or build metadata are ignored when comparing versions.
func (a *Version) Compare(b *Version) int {
if a.Major == b.Major {
if a.Minor == b.Minor {
if a.Patch == b.Patch {
return 0
} else if a.Patch > b.Patch {
return 1
}
} else if a.Minor > b.Minor {
return 1
}
} else if a.Major > b.Major {
return 1
}
return -1
}
// Less performs a simple comparison of this version (a) with another version (b).
// This function returns true if a is less than b, or false otherwise.
//
// Extensions such as pre-release version or build metadata are ignored when comparing versions.
func (a *Version) Less(b *Version) bool {
return a.Compare(b) < 0
}
// SemanticString returns a version string conforming to the standard described in Semantic Versioning 2.0.0.
//
// See https://semver.org/#is-v123-a-semantic-version
func (v *Version) SemanticString() string {
return fmt.Sprintf("%d.%d.%d%s", v.Major, v.Minor, v.Patch, v.Extension)
}
func (v *Version) String() string {
if v.Text != "" {
return v.Text
}
return v.SemanticString()
}