Documentation
¶
Overview ¶
Package semver parses, compares, and evaluates Semantic Versioning 2.0.0 versions and version constraints on top of golang.org/x/mod/semver.
Unlike golang.org/x/mod/semver, every function in this package accepts version strings with or without a leading "v" prefix.
Index ¶
- Variables
- func Build[T Input](v T) string
- func Canonical[T Input](v T) string
- func Compare[T Input](a, b T) int
- func Filter[S ~[]E, E Input](c *Constraints, items S) S
- func IsStable[T Input](v T) bool
- func IsValid[T Input](v T) bool
- func IsValidStrict[S ~string](v S) bool
- func Major[T Input](v T) string
- func MajorMinor[T Input](v T) string
- func Max[T Input](a, b T) T
- func Prerelease[T Input](v T) string
- func Sort[S ~[]E, E Input](list S)
- func SortBy[S ~[]E, E any, K Input](items S, key func(E) K)
- type Constraints
- type Input
- type Version
- func (v Version) Canonical() string
- func (v Version) Compare(other Version) int
- func (v Version) Equal(other Version) bool
- func (v Version) GreaterThan(other Version) bool
- func (v Version) GreaterThanEqual(other Version) bool
- func (v Version) IsPrerelease() bool
- func (v Version) IsStable() bool
- func (v Version) LessThan(other Version) bool
- func (v Version) LessThanEqual(other Version) bool
- func (v Version) Major() uint64
- func (v Version) MajorMinor() string
- func (v Version) MarshalText() ([]byte, error)
- func (v Version) Metadata() string
- func (v Version) Minor() uint64
- func (v Version) Original() string
- func (v Version) Patch() uint64
- func (v Version) Prerelease() string
- func (v Version) String() string
- func (v *Version) UnmarshalText(text []byte) error
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidVersion reports that a string is not a valid semantic version. ErrInvalidVersion = errors.New("invalid semantic version") // ErrInvalidConstraint reports that a string is not a valid version constraint. ErrInvalidConstraint = errors.New("invalid version constraint") )
Functions ¶
func Build ¶
Build returns the build metadata suffix of v including its leading "+", or "" if v is invalid or has no build metadata.
func Canonical ¶
Canonical returns the canonical "vMAJOR.MINOR.PATCH[-prerelease]" form of v without build metadata, or "" if v is not valid.
func Compare ¶
Compare compares two versions (strings or parsed Version values) according to semantic version precedence. Invalid version strings compare less than valid ones, and two invalid strings compare equal.
func Filter ¶
func Filter[S ~[]E, E Input](c *Constraints, items S) S
Filter returns a new slice containing the elements of items that satisfy c.
func IsValid ¶
IsValid reports whether v is a valid semantic version (with an optional leading "v" and optional minor/patch components).
func IsValidStrict ¶
IsValidStrict reports whether v is a valid complete MAJOR.MINOR.PATCH semantic version (with an optional leading "v").
func MajorMinor ¶
MajorMinor returns the "vMAJOR.MINOR" prefix of v, or "" if v is not valid.
func Max ¶
func Max[T Input](a, b T) T
Max returns whichever of a and b has higher semantic version precedence.
func Prerelease ¶
Prerelease returns the prerelease suffix of v including its leading "-", or "" if v is invalid or has no prerelease.
Types ¶
type Constraints ¶
type Constraints struct {
// contains filtered or unexported fields
}
Constraints represents a parsed semantic version constraint expression supporting comparators (=, ==, !=, <, <=, >, >=), wildcards (*, x, X), tilde (~, ~>), caret (^), hyphen ranges (A - B), AND (space/comma), and OR (||) groups.
func MustParseConstraint ¶
func MustParseConstraint[S ~string](raw S) *Constraints
MustParseConstraint parses raw with NewConstraint and panics on error.
func NewConstraint ¶
func NewConstraint[S ~string](raw S) (*Constraints, error)
NewConstraint parses a semantic version constraint expression.
func (*Constraints) Check ¶
func (c *Constraints) Check(v Version) bool
Check reports whether v satisfies c.
Prerelease versions are only matched by an OR-group when at least one comparator in that group specifies a prerelease identifier.
func (*Constraints) CheckString ¶
func (c *Constraints) CheckString(raw string) bool
CheckString parses raw with Parse and reports whether it satisfies c.
func (*Constraints) String ¶
func (c *Constraints) String() string
String returns the original constraint expression.
type Version ¶
type Version struct {
// contains filtered or unexported fields
}
Version represents a parsed Semantic Version 2.0.0 value.
func MustParseStrict ¶
MustParseStrict parses raw with ParseStrict and panics if it is invalid.
func Parse ¶
Parse parses a semantic version string. An optional leading "v" is allowed, and missing minor or patch components default to 0 (for example, "1.2" parses as 1.2.0 and "v1" parses as 1.0.0).
func ParseStrict ¶
ParseStrict parses a complete Semantic Version 2.0.0 string requiring all three core components (MAJOR.MINOR.PATCH). An optional leading "v" is allowed.
func (Version) Canonical ¶
Canonical returns the canonical "vMAJOR.MINOR.PATCH[-prerelease]" string without build metadata.
func (Version) Compare ¶
Compare compares v and other according to Semantic Versioning 2.0.0 precedence rules, ignoring build metadata. It returns -1 if v < other, 0 if v == other, and +1 if v > other.
func (Version) GreaterThan ¶
GreaterThan reports whether v has higher precedence than other.
func (Version) GreaterThanEqual ¶
GreaterThanEqual reports whether v has higher or equal precedence to other.
func (Version) IsPrerelease ¶
IsPrerelease reports whether v has a prerelease identifier.
func (Version) LessThanEqual ¶
LessThanEqual reports whether v has lower or equal precedence to other.
func (Version) MajorMinor ¶
MajorMinor returns the "vMAJOR.MINOR" prefix of v.
func (Version) MarshalText ¶
MarshalText encodes v as its canonical SemVer string.
func (Version) Original ¶
Original returns the original input string used to construct v, or v.String() when v was not parsed from a string.
func (Version) Prerelease ¶
Prerelease returns the prerelease identifier without a leading "-".
func (Version) String ¶
String returns the canonical Semantic Version 2.0.0 string without a leading "v".
func (*Version) UnmarshalText ¶
UnmarshalText decodes a semantic version string into v.