version

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 4, 2025 License: MIT Imports: 3 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func MustNormalize

func MustNormalize(s string) string

MustNormalize normalizes a version string, panicking on error.

func Normalize

func Normalize(s string) (string, error)

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

func NormalizeOrOriginal(s string) string

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:

  1. Compare Major, Minor, Patch numerically
  2. For legacy versions, compare Revision
  3. Release version > Prerelease version
  4. Compare prerelease labels lexicographically
  5. 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

func MustParseRange(s string) *Range

MustParseRange parses a version range string and panics on error. Use this only when you know the range string is valid.

func ParseVersionRange

func ParseVersionRange(s string) (*Range, error)

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.

func (*Range) String

func (r *Range) String() string

String returns the string representation of the range.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL