Skip to content

Commit bb00604

Browse files
authored
feat: add support for collections (#2)
1 parent 3f6796a commit bb00604

File tree

3 files changed

+115
-1
lines changed

3 files changed

+115
-1
lines changed

README.md

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# go-version
22

33
[![Go Report Card](https://goreportcard.com/badge/github.com/bitnami/go-version)](https://goreportcard.com/report/github.com/bitnami/go-version)
4-
[![CI](https://github.com/bitnami/gonit/actions/workflows/go.yml/badge.svg)](https://github.com/bitnami/gonit/actions/workflows/go.yml)
4+
[![CI](https://github.com/bitnami/go-version/actions/workflows/go.yml/badge.svg)](https://github.com/bitnami/go-version/actions/workflows/go.yml)
55

66
go-version is a library for parsing Bitnami packages versions and version constraints, and verifying versions against a set of constraints.
77

@@ -10,6 +10,7 @@ go-version is a library for parsing Bitnami packages versions and version constr
1010

1111
- [Usage](#usage)
1212
- [Version parsing and comparison](#version-parsing-and-comparison)
13+
- [Sorting](#sorting)
1314
- [Version constraints](#version-constraints)
1415
- [Version revision](#version-revision)
1516
- [Missing major/minor/patch versions](#missing-majorminorpatch-versions)
@@ -37,6 +38,22 @@ if v1.LessThan(v2) {
3738
}
3839
```
3940

41+
### Sorting
42+
43+
Collections of versions can be sorted using the `sort.Sort` function from the standard library.
44+
45+
```go
46+
versionsRaw := []string{"1.1.0", "0.7.1", "1.4.0", "1.4.0-alpha", "1.4.1-beta", "1.4.0-alpha.2+20130313144700"}
47+
versions := make(version.Collection, len(versionsRaw))
48+
for i, raw := range versionsRaw {
49+
v, _ := version.Parse(raw)
50+
versions[i] = v
51+
}
52+
53+
// After this, the versions are properly sorted
54+
sort.Sort(versions)
55+
```
56+
4057
### Version constraints
4158

4259
Comma-separated version constraints are considered an `AND`. For example, `>= 1.2.3, < 2.0.0` means the version needs to be greater than or equal to `1.2` and less than `3.0.0`.

pkg/version/version_collection.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package version
2+
3+
// Collection is a type that implements the sort.Interface interface
4+
// so that versions can be sorted.
5+
type Collection []Version
6+
7+
func (v Collection) Len() int {
8+
return len(v)
9+
}
10+
11+
func (v Collection) Less(i, j int) bool {
12+
return v[i].LessThan(v[j])
13+
}
14+
15+
func (v Collection) Swap(i, j int) {
16+
v[i], v[j] = v[j], v[i]
17+
}
+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package version
2+
3+
import (
4+
"sort"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func TestCollection(t *testing.T) {
12+
tests := []struct {
13+
name string
14+
versions []string
15+
want []string
16+
}{
17+
{
18+
name: "happy path",
19+
versions: []string{
20+
"1.1.1",
21+
"1.0.0",
22+
"1.2.0",
23+
"2.0.0",
24+
"0.7.1",
25+
},
26+
want: []string{
27+
"0.7.1",
28+
"1.0.0",
29+
"1.1.1",
30+
"1.2.0",
31+
"2.0.0",
32+
},
33+
},
34+
{
35+
name: "revisions",
36+
versions: []string{
37+
"1.0.0-1.1",
38+
"1.0.0-3.1",
39+
"1.0.0",
40+
"1.0.0-2.2",
41+
"1.0.0-2.11",
42+
"1.0.0-1",
43+
"1.0.0-1.2",
44+
"1.0.0-2",
45+
},
46+
want: []string{
47+
"1.0.0",
48+
"1.0.0-1",
49+
"1.0.0-1.1",
50+
"1.0.0-1.2",
51+
"1.0.0-2",
52+
"1.0.0-2.2",
53+
"1.0.0-2.11",
54+
"1.0.0-3.1",
55+
},
56+
},
57+
}
58+
t.Parallel()
59+
for _, testToRun := range tests {
60+
test := testToRun
61+
t.Run(test.name, func(tt *testing.T) {
62+
tt.Parallel()
63+
versions := make(Collection, len(test.versions))
64+
for i, raw := range test.versions {
65+
v, err := Parse(raw)
66+
require.NoError(tt, err)
67+
versions[i] = v
68+
}
69+
70+
sort.Sort(versions)
71+
72+
got := make([]string, len(versions))
73+
for i, v := range versions {
74+
got[i] = v.String()
75+
}
76+
77+
assert.Equal(tt, test.want, got)
78+
})
79+
}
80+
}

0 commit comments

Comments
 (0)