Skip to content

Commit

Permalink
Adds sort subpackage to catalogmetadata
Browse files Browse the repository at this point in the history
Signed-off-by: Mikalai Radchuk <mradchuk@redhat.com>
  • Loading branch information
m1kola committed Sep 8, 2023
1 parent b2174a4 commit b5f00f5
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
34 changes: 34 additions & 0 deletions internal/catalogmetadata/sort/sort.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package sort

import (
"github.com/operator-framework/operator-controller/internal/catalogmetadata"
)

// ByVersion is a sort "less" function that orders bundles
// in inverse version order (higher versions on top).
func ByVersion(b1, b2 *catalogmetadata.Bundle) bool {
ver1, err1 := b1.Version()
ver2, err2 := b2.Version()
errComp := compareErrors(err1, err2)
if errComp != 0 {
return errComp < 0
}

// Check for "greater than" because
// we want higher versions on top
return ver1.GT(*ver2)
}

// compareErrors returns 0 if both errors are either nil or not nil
// -1 if err1 is nil and err2 is not nil
// +1 if err1 is not nil and err2 is nil
func compareErrors(err1 error, err2 error) int {
if err1 != nil && err2 == nil {
return 1
}

if err1 == nil && err2 != nil {
return -1
}
return 0
}
64 changes: 64 additions & 0 deletions internal/catalogmetadata/sort/sort_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package sort_test

import (
"encoding/json"
"sort"
"testing"

"github.com/stretchr/testify/assert"

"github.com/operator-framework/operator-registry/alpha/declcfg"
"github.com/operator-framework/operator-registry/alpha/property"

"github.com/operator-framework/operator-controller/internal/catalogmetadata"
catalogsort "github.com/operator-framework/operator-controller/internal/catalogmetadata/sort"
)

func TestByVersion(t *testing.T) {
b1 := &catalogmetadata.Bundle{Bundle: declcfg.Bundle{
Properties: []property.Property{
{
Type: property.TypePackage,
Value: json.RawMessage(`{"packageName": "package1", "version": "1.0.0"}`),
},
},
}}
b2 := &catalogmetadata.Bundle{Bundle: declcfg.Bundle{
Properties: []property.Property{
{
Type: property.TypePackage,
Value: json.RawMessage(`{"packageName": "package1", "version": "0.0.1"}`),
},
},
}}
b3 := &catalogmetadata.Bundle{Bundle: declcfg.Bundle{
Properties: []property.Property{
{
Type: property.TypePackage,
Value: json.RawMessage(`{"packageName": "package1", "version": "1.0.0-alpha+001"}`),
},
},
}}
b4noVersion := &catalogmetadata.Bundle{}

toSort := []*catalogmetadata.Bundle{b3, b2, b1}
sort.SliceStable(toSort, func(i, j int) bool {
return catalogsort.ByVersion(toSort[i], toSort[j])
})

assert.Len(t, toSort, 3)
assert.Equal(t, b1, toSort[0])
assert.Equal(t, b3, toSort[1])
assert.Equal(t, b2, toSort[2])

toSort = []*catalogmetadata.Bundle{b3, b4noVersion, b2, b1}
sort.SliceStable(toSort, func(i, j int) bool {
return catalogsort.ByVersion(toSort[i], toSort[j])
})

assert.Len(t, toSort, 4)
assert.Equal(t, b1, toSort[0])
assert.Equal(t, b3, toSort[1])
assert.Equal(t, b2, toSort[2])
assert.Equal(t, b4noVersion, toSort[3])
}

0 comments on commit b5f00f5

Please sign in to comment.