A library for handling a superset of semantic versioning in golang.
See the godoc here: https://godoc.org/go.bug.st/relaxed-semver
This library tries to implement the semantic versioning specification 2.0.0 with an exception: the numeric format major.minor.patch
like 1.3.2
may be truncated if a number is zero, so:
1.2.0
or1.2.0-beta
may be written as1.2
or1.2-beta
respectively1.0.0
or1.0.0-beta
may be written1
or1-beta
respectively0.0.0
may be written as the empty string, but0.0.0-beta
may not be written as-beta
You can parse a semver version string with the Parse
function that returns a Version
object that can be used to be compared with other Version
objects using the CompareTo
, LessThan
, LessThanOrEqual
, Equal
, GreaterThan
and GreaterThanOrEqual
methods.
The Parse
function returns an error
if the string does not comply to the above specification. Alternatively the MustParse
function can be used, it returns only the Version
object or panics if a parsing error occurs.
This library allows the use of an even more relaxed semver specification using the RelaxedVersion
object. It works with the following rules:
- If the parsed string is a valid semver (following the rules above), then the
RelaxedVersion
will behave exactly as a normalVersion
object - if the parsed string is not a valid semver, then the string is kept as-is inside the
RelaxedVersion
object as a custom version string - when comparing two
RelaxedVersion
the rule is simple: if both are valid semver, the semver rules applies; if both are custom version string they are compared as alphanumeric strings; if one is valid semver and the other is a custom version string the valid semver is always greater - two
RelaxedVersion
are compatible (by theCompatibleWith
operation) only if- they are equal
- they are both valid semver and they are compatible as per semver specification
The RelaxedVersion
object is basically made to allow systems that do not use semver to soft transition to semantic versioning, because it allows an intermediate period where the invalid version is still tolerated.
To parse a RelaxedVersion
you can use the ParseRelaxed
function.
Dependency version matching can be specified via version constraints, which might be a version range or an exact version.
The following operators are supported:
= |
equal to |
> |
greater than |
>= |
greater than or equal to |
< |
less than |
<= |
less than or equal to |
^ |
compatible-with |
! |
NOT |
&& |
AND |
|| |
OR |
( , ) |
constraint group |
Given the following releases of a dependency:
0.1.0
0.1.1
0.2.0
1.0.0
2.0.0
2.0.5
2.0.6
2.1.0
3.0.0
constraints conditions would match as follows:
The following condition... | will match with versions... |
---|---|
=1.0.0 |
1.0.0 |
>1.0.0 |
2.0.0 , 2.0.5 , 2.0.6 , 2.1.0 , 3.0.0 |
>=1.0.0 |
1.0.0 , 2.0.0 , 2.0.5 , 2.0.6 , 2.1.0 , 3.0.0 |
<2.0.0 |
0.1.0 , 0.1.1 , 0.2.0 , 1.0.0 |
<=2.0.0 |
0.1.0 , 0.1.1 , 0.2.0 , 1.0.0 , 2.0.0 |
!=1.0.0 |
0.1.0 , 0.1.1 , 0.2.0 , 2.0.0 , 2.0.5 , 2.0.6 , 2.1.0 , 3.0.0 |
>1.0.0 && <2.1.0 |
2.0.0 , 2.0.5 , 2.0.6 |
<1.0.0 || >2.0.0 |
0.1.0 , 0.1.1 , 0.2.0 , 2.0.5 , 2.0.6 , 2.1.0 , 3.0.0 |
(>0.1.0 && <2.0.0) || >2.0.5 |
0.1.1 , 0.2.0 , 1.0.0 , 2.0.6 , 2.1.0 , 3.0.0 |
^2.0.5 |
2.0.5 , 2.0.6 , 2.1.0 |
^0.1.0 |
0.1.0 , 0.1.1 |
The Version
and RelaxedVersion
have the JSON un/marshaler implemented so they can be JSON decoded/encoded.
The Version
and RelaxedVersion
provides optimized MarshalBinary
/UnmarshalBinary
methods for binary encoding.
The Version
and RelaxedVersion
have the YAML un/marshaler implemented so they can be YAML decoded/encoded with the excellent gopkg.in/yaml.v3
library.