Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for collections #2

Merged
merged 6 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# go-version

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

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

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

- [Usage](#usage)
- [Version parsing and comparison](#version-parsing-and-comparison)
- [Sorting](#sorting)
- [Version constraints](#version-constraints)
- [Version revision](#version-revision)
- [Missing major/minor/patch versions](#missing-majorminorpatch-versions)
Expand Down Expand Up @@ -37,6 +38,22 @@ if v1.LessThan(v2) {
}
```

### Sorting

Collections of versions can be sorted the `sort.Sort` function from the standard library.
juan131 marked this conversation as resolved.
Show resolved Hide resolved

```go
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"}
versions := make(version.Collection, len(versionsRaw))
for i, raw := range versionsRaw {
v, _ := version.Parse(raw)
versions[i] = v
}

// After this, the versions are properly sorted
sort.Sort(versions)
```

### Version constraints

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`.
Expand Down
17 changes: 17 additions & 0 deletions pkg/version/version_collection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package version

// Collection is a type that implements the sort.Interface interface
// so that versions can be sorted.
type Collection []Version

func (v Collection) Len() int {
return len(v)
}

func (v Collection) Less(i, j int) bool {
return v[i].LessThan(v[j])
}

func (v Collection) Swap(i, j int) {
v[i], v[j] = v[j], v[i]
}
80 changes: 80 additions & 0 deletions pkg/version/version_collection_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package version

import (
"sort"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestCollection(t *testing.T) {
tests := []struct {
name string
versions []string
want []string
}{
{
name: "happy path",
versions: []string{
"1.1.1",
"1.0.0",
"1.2.0",
"2.0.0",
"0.7.1",
},
want: []string{
"0.7.1",
"1.0.0",
"1.1.1",
"1.2.0",
"2.0.0",
},
},
{
name: "revisions",
versions: []string{
"1.0.0-1.1",
"1.0.0-3.1",
"1.0.0",
"1.0.0-2.2",
"1.0.0-2.11",
"1.0.0-1",
"1.0.0-1.2",
"1.0.0-2",
},
want: []string{
"1.0.0",
"1.0.0-1",
"1.0.0-1.1",
"1.0.0-1.2",
"1.0.0-2",
"1.0.0-2.2",
"1.0.0-2.11",
"1.0.0-3.1",
},
},
}
t.Parallel()
for _, testToRun := range tests {
test := testToRun
t.Run(test.name, func(tt *testing.T) {
tt.Parallel()
versions := make(Collection, len(test.versions))
for i, raw := range test.versions {
v, err := Parse(raw)
require.NoError(tt, err)
versions[i] = v
}

sort.Sort(versions)

got := make([]string, len(versions))
for i, v := range versions {
got[i] = v.String()
}

assert.Equal(tt, test.want, got)
})
}
}