Documentation
¶
Overview ¶
Package version provides NuGet version parsing and comparison.
It supports both NuGet SemVer 2.0 format and legacy 4-part versions.
Example:
v, err := version.Parse("1.2.3-beta.1")
if err != nil {
log.Fatal(err)
}
fmt.Println(v.Major, v.Minor, v.Patch) // 1 2 3
Index ¶
- func MustNormalize(s string) string
- func Normalize(s string) (string, error)
- func NormalizeOrOriginal(s string) string
- type FloatBehavior
- type FloatRange
- type NuGetVersion
- func (v *NuGetVersion) Compare(other *NuGetVersion) int
- func (v *NuGetVersion) CompareNumericOnly(other *NuGetVersion) int
- func (v *NuGetVersion) Equals(other *NuGetVersion) bool
- func (v *NuGetVersion) GreaterThan(other *NuGetVersion) bool
- func (v *NuGetVersion) GreaterThanOrEqual(other *NuGetVersion) bool
- func (v *NuGetVersion) IsPrerelease() bool
- func (v *NuGetVersion) LessThan(other *NuGetVersion) bool
- func (v *NuGetVersion) LessThanOrEqual(other *NuGetVersion) bool
- func (v *NuGetVersion) String() string
- func (v *NuGetVersion) ToNormalizedString() string
- type Range
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func MustNormalize ¶
MustNormalize normalizes a version string, panicking on error.
func Normalize ¶
Normalize parses a version string and returns its normalized form.
Normalization converts versions to their canonical string representation, removing leading zeros and applying consistent formatting.
Examples:
- "1.01.1" → "1.1.1"
- "1" → "1.0.0"
- "1.2" → "1.2.0"
- "1.0.0.0" → "1.0.0.0" (legacy preserved)
func NormalizeOrOriginal ¶
NormalizeOrOriginal attempts to normalize a version string. If normalization fails, returns the original string.
Types ¶
type FloatBehavior ¶
type FloatBehavior int
FloatBehavior defines how floating versions behave.
const ( // FloatNone means no floating FloatNone FloatBehavior = iota // FloatPrerelease floats to latest prerelease: 1.0.0-* FloatPrerelease // FloatRevision floats to latest revision: 1.0.0.* FloatRevision // FloatPatch floats to latest patch: 1.0.* FloatPatch // FloatMinor floats to latest minor: 1.* FloatMinor // FloatMajor floats to latest major: * FloatMajor )
func (FloatBehavior) String ¶
func (f FloatBehavior) String() string
String returns the string representation of FloatBehavior.
type FloatRange ¶
type FloatRange struct {
MinVersion *NuGetVersion
FloatBehavior FloatBehavior
}
FloatRange represents a floating version range.
func ParseFloatRange ¶
func ParseFloatRange(s string) (*FloatRange, error)
ParseFloatRange parses floating version ranges like 1.0.*, 1.0.0-*, or *.
func (*FloatRange) FindBestMatch ¶
func (f *FloatRange) FindBestMatch(versions []*NuGetVersion) *NuGetVersion
FindBestMatch finds the highest version that satisfies this floating range.
func (*FloatRange) Satisfies ¶
func (f *FloatRange) Satisfies(version *NuGetVersion) bool
Satisfies returns true if the version satisfies this floating range.
func (*FloatRange) String ¶
func (f *FloatRange) String() string
String returns the string representation of the floating range.
type NuGetVersion ¶
type NuGetVersion struct {
// Major version number
Major int
// Minor version number
Minor int
// Patch version number (or Build for legacy versions)
Patch int
// Revision is only used for legacy 4-part versions (Major.Minor.Build.Revision)
Revision int
// IsLegacyVersion indicates this is a 4-part version, not SemVer 2.0
IsLegacyVersion bool
// ReleaseLabels contains prerelease labels (e.g., ["beta", "1"] for "1.0.0-beta.1")
ReleaseLabels []string
// Metadata is the build metadata (e.g., "20241019" for "1.0.0+20241019")
// Metadata is ignored in version comparison per SemVer 2.0 spec
Metadata string
// contains filtered or unexported fields
}
NuGetVersion represents a NuGet package version.
It supports both SemVer 2.0 format (Major.Minor.Patch[-Prerelease][+Metadata]) and legacy 4-part versions (Major.Minor.Build.Revision).
func MustParse ¶
func MustParse(s string) *NuGetVersion
MustParse parses a version string and panics on error. Use this only when you know the version string is valid.
func Parse ¶
func Parse(s string) (*NuGetVersion, error)
Parse parses a version string into a NuGetVersion.
Supported formats:
- SemVer 2.0: Major.Minor.Patch[-Prerelease][+Metadata]
- Legacy: Major.Minor.Build.Revision
Returns an error if the version string is invalid.
Example:
v, err := Parse("1.0.0-beta.1+build.123")
if err != nil {
return err
}
func (*NuGetVersion) Compare ¶
func (v *NuGetVersion) Compare(other *NuGetVersion) int
Compare compares two NuGet versions.
Returns:
-1 if v < other 0 if v == other 1 if v > other
Comparison follows NuGet SemVer 2.0 rules:
- Compare Major, Minor, Patch numerically
- For legacy versions, compare Revision
- Release version > Prerelease version
- Compare prerelease labels lexicographically
- Metadata is ignored in comparison
func (*NuGetVersion) CompareNumericOnly ¶
func (v *NuGetVersion) CompareNumericOnly(other *NuGetVersion) int
CompareNumericOnly compares only the numeric parts (Major.Minor.Patch[.Revision]) ignoring prerelease labels. This is used for NU1103 detection where we need to check if a prerelease version would satisfy a range if the prerelease restriction were lifted.
func (*NuGetVersion) Equals ¶
func (v *NuGetVersion) Equals(other *NuGetVersion) bool
Equals returns true if v equals other.
func (*NuGetVersion) GreaterThan ¶
func (v *NuGetVersion) GreaterThan(other *NuGetVersion) bool
GreaterThan returns true if v > other.
func (*NuGetVersion) GreaterThanOrEqual ¶
func (v *NuGetVersion) GreaterThanOrEqual(other *NuGetVersion) bool
GreaterThanOrEqual returns true if v >= other.
func (*NuGetVersion) IsPrerelease ¶
func (v *NuGetVersion) IsPrerelease() bool
IsPrerelease returns true if pre-release labels exist for the version.
A version is considered a prerelease if it has any non-empty release labels (e.g., "1.0.0-beta", "1.0.0-rc.1").
Note: Metadata (after '+') does not affect prerelease status. For example, "1.0.0+build123" is NOT a prerelease.
Reference: SemanticVersion.IsPrerelease in NuGet.Client
func (*NuGetVersion) LessThan ¶
func (v *NuGetVersion) LessThan(other *NuGetVersion) bool
LessThan returns true if v < other.
func (*NuGetVersion) LessThanOrEqual ¶
func (v *NuGetVersion) LessThanOrEqual(other *NuGetVersion) bool
LessThanOrEqual returns true if v <= other.
func (*NuGetVersion) String ¶
func (v *NuGetVersion) String() string
String returns the string representation of the version.
func (*NuGetVersion) ToNormalizedString ¶
func (v *NuGetVersion) ToNormalizedString() string
ToNormalizedString returns the normalized version string.
Normalization rules:
- Remove leading zeros: 1.01.1 → 1.1.1
- Legacy 4-part versions preserve all parts: 1.0.0.0 → 1.0.0.0
- SemVer versions omit trailing zeros: 1.0.0 → 1.0.0
- Prerelease labels preserved: 1.0.0-beta → 1.0.0-beta
- Metadata preserved: 1.0.0+build → 1.0.0+build
type Range ¶
type Range struct {
MinVersion *NuGetVersion
MaxVersion *NuGetVersion
MinInclusive bool
MaxInclusive bool
}
Range represents a range of acceptable versions.
Syntax:
[1.0, 2.0] - 1.0 ≤ x ≤ 2.0 (inclusive) (1.0, 2.0) - 1.0 < x < 2.0 (exclusive) [1.0, 2.0) - 1.0 ≤ x < 2.0 (mixed) [1.0, ) - x ≥ 1.0 (open upper) (, 2.0] - x ≤ 2.0 (open lower) 1.0 - x ≥ 1.0 (implicit minimum)
func MustParseRange ¶
MustParseRange parses a version range string and panics on error. Use this only when you know the range string is valid.
func ParseVersionRange ¶
ParseVersionRange parses a version range string.
func (*Range) FindBestMatch ¶
func (r *Range) FindBestMatch(versions []*NuGetVersion) *NuGetVersion
FindBestMatch finds the best matching version that satisfies this range. Matches NuGet.Client behavior: favors LOWER versions (nearest wins). Reference: NuGet.Versioning/VersionRange.cs IsBetter method (line 93: "Favor lower versions")
Returns nil if no version satisfies the range.
func (*Range) Satisfies ¶
func (r *Range) Satisfies(version *NuGetVersion) bool
Satisfies returns true if the version satisfies this range.
func (*Range) SatisfiesNumericBounds ¶
func (r *Range) SatisfiesNumericBounds(version *NuGetVersion) bool
SatisfiesNumericBounds returns true if the version satisfies the numeric bounds of this range, ignoring prerelease restrictions. This is used for NU1103 detection to identify cases where prerelease versions would satisfy the range if the prerelease restriction were lifted.