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 NormalizeWithScheme(version, scheme string) (string, error)
- func Satisfies(version, constraint, scheme string) (bool, error)
- func ToVersString(r *Range, scheme string) string
- func Valid(version string) bool
- func ValidWithScheme(version, scheme 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) AdjacentWithScheme(other Interval, scheme string) bool
- func (i Interval) Contains(version string) bool
- func (i Interval) ContainsWithScheme(version, scheme string) bool
- func (i Interval) Intersect(other Interval) Interval
- func (i Interval) IntersectWithScheme(other Interval, scheme string) Interval
- func (i Interval) IsEmpty() bool
- func (i Interval) IsEmptyWithScheme(scheme string) bool
- func (i Interval) IsUnbounded() bool
- func (i Interval) Overlaps(other Interval) bool
- func (i Interval) OverlapsWithScheme(other Interval, scheme string) bool
- func (i Interval) String() string
- func (i Interval) StringWithScheme(scheme string) string
- func (i Interval) Union(other Interval) *Interval
- func (i Interval) UnionWithScheme(other Interval, scheme string) *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
- func (r *Range) Contains(version string) bool
- func (r *Range) Exclude(version string) *Range
- func (r *Range) Intersect(other *Range) *Range
- func (r *Range) IntersectChecked(other *Range) (*Range, error)
- func (r *Range) IsEmpty() bool
- func (r *Range) IsUnbounded() bool
- func (r *Range) String() string
- func (r *Range) Union(other *Range) *Range
- func (r *Range) UnionChecked(other *Range) (*Range, error)
- 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.3.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 NormalizeWithScheme ¶ added in v0.3.0
NormalizeWithScheme normalizes a version without discarding scheme-specific components. Schemes without a canonical text form are validated and trimmed.
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.
func ValidWithScheme ¶ added in v0.3.0
ValidWithScheme checks whether a version is valid for the given scheme.
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 ParseConstraintWithScheme ¶ added in v0.3.0
func ParseConstraintWithScheme(s, scheme string) (*Constraint, error)
ParseConstraintWithScheme parses a constraint using scheme-specific comparison rules.
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) SatisfiesWithScheme ¶ added in v0.3.0
func (c *Constraint) SatisfiesWithScheme(version, scheme string) bool
SatisfiesWithScheme checks the constraint using the specified version scheme.
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) AdjacentWithScheme ¶ added in v0.3.0
AdjacentWithScheme reports adjacency under a version scheme.
func (Interval) ContainsWithScheme ¶ added in v0.3.0
ContainsWithScheme checks containment under a version scheme.
func (Interval) IntersectWithScheme ¶ added in v0.3.0
IntersectWithScheme returns the intersection under a version scheme.
func (Interval) IsEmptyWithScheme ¶ added in v0.3.0
IsEmptyWithScheme reports whether the interval is empty under a version scheme.
func (Interval) IsUnbounded ¶
IsUnbounded returns true if this interval matches all versions.
func (Interval) OverlapsWithScheme ¶ added in v0.3.0
OverlapsWithScheme reports overlap under a version scheme.
func (Interval) StringWithScheme ¶ added in v0.3.0
StringWithScheme formats the interval under a version scheme.
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
// Scheme is the versioning scheme this range was parsed under.
// It selects the comparison rules used by Contains. Empty means generic.
Scheme string
}
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
- hex/elixir: ~> 1.2, >= 1.0 and < 2.0
- deb/debian: >= 1.0, << 2.0
- rpm: >= 1.0, <= 2.0
- conan: ^1.2, ~1.2, >1 <2, ||
- openssl: exact versions, optionally comma-separated
- nginx: 0.8.40+, 0.7.52-0.8.39
func (*Range) Intersect ¶
Intersect returns a new Range that is the intersection of this range and another.
func (*Range) IntersectChecked ¶ added in v0.3.0
IntersectChecked returns the intersection of ranges that use compatible schemes.
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.