Package comver
provides the ability to work with composer supported versions in Go.
Built with ♥ by Typist Tech
Note
See full API documentation at pkg.go.dev.
NewVersion
parses a given version string, attempts to coerce a version string into a Version
object or return an error if unable to parse the version string.
If there is a leading v or a version listed without all parts (e.g. v1.2.p5+foo) it will attempt to coerce it into a valid composer version (e.g. 1.2.0.0-patch5). In both cases a Version
object is returned that can be sorted, compared, and used in constraints.
Warning
Due to implementation complexity, it only supports a subset of composer versioning.
Refer to the version_test.go
for examples.
ss := []string{
"1.2.3",
"v1.2.p5+foo",
"v1.2.3.4.p5+foo",
"2010-01-02",
"2010-01-02.5",
"not a version",
"1.0.0-meh",
"20100102.0.3.4",
"1.0.0-alpha.beta",
}
for _, s := range ss {
v, err := comver.NewVersion(s)
if err != nil {
fmt.Println(s, " => ", err)
continue
}
fmt.Println(s, " => ", v)
}
// Output:
// 1.2.3 => 1.2.3.0
// v1.2.p5+foo => 1.2.0.0-patch5
// v1.2.3.4.p5+foo => 1.2.3.4-patch5
// 2010-01-02 => 2010.1.2.0
// 2010-01-02.5 => 2010.1.2.5
// not a version => error parsing version string "not a version"
// 1.0.0-meh => error parsing version string "1.0.0-meh"
// 20100102.0.3.4 => error parsing version string "20100102.0.3.4"
// 1.0.0-alpha.beta => error parsing version string "1.0.0-alpha.beta"
v1, _ := comver.NewVersion("1")
v2, _ := comver.NewVersion("2")
v3, _ := comver.NewVersion("3")
v4, _ := comver.NewVersion("4")
cs := []any{
comver.NewGreaterThanConstraint(v1),
comver.NewGreaterThanOrEqualToConstraint(v2),
comver.NewLessThanOrEqualToConstraint(v3),
comver.NewLessThanConstraint(v4),
}
for _, c := range cs {
fmt.Println(c)
}
// Output:
// >1
// >=2
// <=3
// <4
interval
represents the intersection (logical AND) of two constraints.
v1, _ := comver.NewVersion("1")
v2, _ := comver.NewVersion("2")
v3, _ := comver.NewVersion("3")
g1l3, _ := comver.NewInterval(
comver.NewGreaterThanConstraint(v1),
comver.NewLessThanConstraint(v3),
)
if g1l3.Check(v2) {
fmt.Println(v2.Short(), "satisfies", g1l3)
}
if !g1l3.Check(v3) {
fmt.Println(v2.Short(), "doesn't satisfy", g1l3)
}
// Output:
// 2 satisfies >1 <3
// 2 doesn't satisfy >1 <3
Intervals
represent the union (logical OR) of multiple intervals.
v1, _ := comver.NewVersion("1")
v2, _ := comver.NewVersion("2")
v3, _ := comver.NewVersion("3")
v4, _ := comver.NewVersion("4")
g1l3, _ := comver.NewInterval(
comver.NewGreaterThanConstraint(v1),
comver.NewLessThanConstraint(v3),
)
ge2le4, _ := comver.NewInterval(
comver.NewGreaterThanOrEqualToConstraint(v2),
comver.NewLessThanOrEqualToConstraint(v4),
)
is := comver.Intervals{g1l3, ge2le4}
fmt.Println(is)
is = comver.Compact(is)
fmt.Println(is)
// Output:
// >1 <3 || >=2 <=4
// >1 <=4
comver
is a Typist Tech project and maintained by Tang Rufus, freelance developer for hire.
Full list of contributors can be found here.
This project is a free software distributed under the terms of the MIT license. For the full license, see LICENSE.
Feedbacks / bug reports / pull requests are welcome.