-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds sort subpackage to catalogmetadata
Signed-off-by: Mikalai Radchuk <mradchuk@redhat.com>
- Loading branch information
Showing
2 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | ||
} |