Documentation
¶
Overview ¶
Package vers provides version range parsing and comparison according to the VERS specification.
VERS (Version Range Specification) is a universal format for expressing version ranges across different package ecosystems. This package supports parsing vers URIs, native package manager syntax, and provides version comparison functionality.
Quick Start:
// Parse a vers URI
r, _ := vers.Parse("vers:npm/>=1.2.3|<2.0.0")
r.Contains("1.5.0") // true
// Parse native package manager syntax
r, _ = vers.ParseNative("^1.2.3", "npm")
// Check if version satisfies constraint
vers.Satisfies("1.5.0", ">=1.0.0,<2.0.0", "npm") // true
// Compare versions
vers.Compare("1.2.3", "1.2.4") // -1
See https://github.com/package-url/purl-spec/blob/main/VERSION-RANGE-SPEC.rst
Index ¶
- Constants
- Variables
- func Compare(a, b string) int
- func CompareVersions(a, b string) int
- func CompareWithScheme(a, b, scheme string) int
- func HighestSatisfying(versions []string, constraint, scheme string) (string, error)
- func Normalize(version string) (string, error)
- func Satisfies(version, constraint, scheme string) (bool, error)
- func ToVersString(r *Range, scheme string) string
- func Valid(version string) bool
- type Constraint
- type Interval
- func EmptyInterval() Interval
- func ExactInterval(version string) Interval
- func GreaterThanInterval(version string, inclusive bool) Interval
- func LessThanInterval(version string, inclusive bool) Interval
- func NewInterval(min, max string, minInclusive, maxInclusive bool) Interval
- func UnboundedInterval() Interval
- func (i Interval) Adjacent(other Interval) bool
- func (i Interval) Contains(version string) bool
- func (i Interval) Intersect(other Interval) Interval
- func (i Interval) IsEmpty() bool
- func (i Interval) IsUnbounded() bool
- func (i Interval) Overlaps(other Interval) bool
- func (i Interval) String() string
- func (i Interval) Union(other Interval) *Interval
- type Parser
- type Range
- func Empty() *Range
- func Exact(version string) *Range
- func GreaterThan(version string, inclusive bool) *Range
- func LessThan(version string, inclusive bool) *Range
- func NewRange(intervals []Interval) *Range
- func Parse(versURI string) (*Range, error)
- func ParseNative(constraint string, scheme string) (*Range, error)
- func Unbounded() *Range
- type VersionInfo
- func (v *VersionInfo) Compare(other *VersionInfo) int
- func (v *VersionInfo) IncrementMajor() *VersionInfo
- func (v *VersionInfo) IncrementMinor() *VersionInfo
- func (v *VersionInfo) IncrementPatch() *VersionInfo
- func (v *VersionInfo) IsPrerelease() bool
- func (v *VersionInfo) IsStable() bool
- func (v *VersionInfo) String() string
Constants ¶
const Version = "0.1.0"
Version is the library version.
Variables ¶
var SemanticVersionRegex = regexp.MustCompile(`^v?(\d+)(?:\.(\d+))?(?:\.(\d+))?(?:-([^+]+))?(?:\+(.+))?$`)
SemanticVersionRegex matches semantic version strings (with optional v prefix).
var ValidOperators = []string{"=", "!=", "<", "<=", ">", ">="}
Valid constraint operators.
Functions ¶
func CompareVersions ¶
CompareVersions compares two version strings. Returns -1 if a < b, 0 if a == b, 1 if a > b.
func CompareWithScheme ¶ added in v0.2.0
CompareWithScheme compares two version strings using scheme-specific rules. Returns -1 if a < b, 0 if a == b, 1 if a > b.
func HighestSatisfying ¶ added in v0.2.6
HighestSatisfying returns the highest version in versions that satisfies constraint under the given scheme. Versions that fail to parse are skipped. Returns ("", nil) when no version in the list satisfies the constraint — a non-nil error is reserved for a constraint that itself fails to parse.
Common shape for package-manager resolvers: fetch the list of available versions from the registry, then pick the highest one that still satisfies the user's manifest constraint.
If scheme is empty, constraint is parsed as a vers URI.
func Satisfies ¶
Satisfies checks if a version satisfies a constraint.
If scheme is empty, constraint is parsed as a vers URI. Otherwise, constraint is parsed as native package manager syntax.
func ToVersString ¶
ToVersString converts a Range back to a vers URI string.
Types ¶
type Constraint ¶
Constraint represents a single version constraint (e.g., ">=1.2.3").
func ParseConstraint ¶
func ParseConstraint(s string) (*Constraint, error)
ParseConstraint parses a constraint string into a Constraint.
func (*Constraint) IsExclusion ¶
func (c *Constraint) IsExclusion() bool
IsExclusion returns true if this is an exclusion constraint (!=).
func (*Constraint) Satisfies ¶
func (c *Constraint) Satisfies(version string) bool
Satisfies checks if a version satisfies this constraint.
func (*Constraint) String ¶
func (c *Constraint) String() string
String returns the constraint as a string.
func (*Constraint) ToInterval ¶
func (c *Constraint) ToInterval() (Interval, bool)
ToInterval converts this constraint to an interval. Returns nil for exclusion constraints (!=).
type Interval ¶
Interval represents a mathematical interval of versions. For example, [1.0.0, 2.0.0) represents versions from 1.0.0 (inclusive) to 2.0.0 (exclusive).
func EmptyInterval ¶
func EmptyInterval() Interval
EmptyInterval creates an interval that matches no versions.
func ExactInterval ¶
ExactInterval creates an interval that matches exactly one version.
func GreaterThanInterval ¶
GreaterThanInterval creates an interval for versions greater than the given version.
func LessThanInterval ¶
LessThanInterval creates an interval for versions less than the given version.
func NewInterval ¶
NewInterval creates a new interval with the given bounds.
func UnboundedInterval ¶
func UnboundedInterval() Interval
UnboundedInterval creates an interval that matches all versions.
func (Interval) IsUnbounded ¶
IsUnbounded returns true if this interval matches all versions.
type Parser ¶
type Parser struct{}
Parser handles parsing of vers URIs and native package manager syntax.
func (*Parser) ParseNative ¶
ParseNative parses a native package manager version range into a Range.
type Range ¶
type Range struct {
Intervals []Interval
Exclusions []string // Versions to exclude (from != constraints)
// RawConstraints stores the original constraints for VERS output (not merged)
RawConstraints []Interval
}
Range represents a version range as a collection of intervals. Multiple intervals represent a union (OR) of ranges.
func GreaterThan ¶
GreaterThan creates a range for versions greater than (or equal to) the specified version.
func LessThan ¶
LessThan creates a range for versions less than (or equal to) the specified version.
func Parse ¶
Parse parses a vers URI string into a Range.
The vers URI format is: vers:<scheme>/<constraints> For example: vers:npm/>=1.2.3|<2.0.0
Use vers:<scheme>/* for an unbounded range that matches all versions.
func ParseNative ¶
ParseNative parses a native package manager version range into a Range.
Supported schemes:
- npm: ^1.2.3, ~1.2.3, 1.2.3 - 2.0.0, >=1.0.0 <2.0.0, ||
- gem/rubygems: ~> 1.2, >= 1.0, < 2.0
- pypi: >=1.0,<2.0, ~=1.4.2, !=1.5.0
- maven: [1.0,2.0), (1.0,2.0], [1.0,)
- nuget: [1.0,2.0), (1.0,2.0]
- cargo: ^1.2.3, ~1.2.3, >=1.0.0, <2.0.0
- go: >=1.0.0, <2.0.0
- deb/debian: >= 1.0, << 2.0
- rpm: >= 1.0, <= 2.0
func (*Range) Intersect ¶
Intersect returns a new Range that is the intersection of this range and another.
func (*Range) IsUnbounded ¶
IsUnbounded returns true if this range matches all versions.
type VersionInfo ¶
type VersionInfo struct {
Major int
Minor int
Patch int
Prerelease string
Build string
Original string
}
VersionInfo represents a parsed version with its components.
func ParseVersion ¶
func ParseVersion(s string) (*VersionInfo, error)
ParseVersion parses a version string into its components.
func (*VersionInfo) Compare ¶
func (v *VersionInfo) Compare(other *VersionInfo) int
Compare compares this version to another. Returns -1 if v < other, 0 if v == other, 1 if v > other.
func (*VersionInfo) IncrementMajor ¶
func (v *VersionInfo) IncrementMajor() *VersionInfo
IncrementMajor returns a new version with major incremented.
func (*VersionInfo) IncrementMinor ¶
func (v *VersionInfo) IncrementMinor() *VersionInfo
IncrementMinor returns a new version with minor incremented.
func (*VersionInfo) IncrementPatch ¶
func (v *VersionInfo) IncrementPatch() *VersionInfo
IncrementPatch returns a new version with patch incremented.
func (*VersionInfo) IsPrerelease ¶
func (v *VersionInfo) IsPrerelease() bool
IsPrerelease returns true if this is a prerelease version.
func (*VersionInfo) IsStable ¶
func (v *VersionInfo) IsStable() bool
IsStable returns true if this is a stable release (no prerelease).
func (*VersionInfo) String ¶
func (v *VersionInfo) String() string
String returns the normalized version string.