Skip to content

Commit

Permalink
Address reviewer comments
Browse files Browse the repository at this point in the history
Signed-off-by: Per Goncalves da Silva <[email protected]>
  • Loading branch information
Per Goncalves da Silva committed Feb 10, 2025
1 parent 2cc81ae commit 364c6b3
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 130 deletions.
4 changes: 2 additions & 2 deletions internal/catalogmetadata/filter/successors_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package filter

import (
"github.com/operator-framework/operator-controller/internal/util/filter"

Check failure on line 4 in internal/catalogmetadata/filter/successors_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not properly formatted (gci)
"slices"
"testing"

Expand All @@ -16,7 +17,6 @@ import (
ocv1 "github.com/operator-framework/operator-controller/api/v1"
"github.com/operator-framework/operator-controller/internal/bundleutil"
"github.com/operator-framework/operator-controller/internal/catalogmetadata/compare"

Check failure on line 19 in internal/catalogmetadata/filter/successors_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not properly formatted (gci)
slicesutil "github.com/operator-framework/operator-controller/internal/util/slices"
)

func TestSuccessorsPredicate(t *testing.T) {
Expand Down Expand Up @@ -161,7 +161,7 @@ func TestSuccessorsPredicate(t *testing.T) {
for _, bundle := range bundleSet {
allBundles = append(allBundles, bundle)
}
result := slicesutil.RemoveInPlace(allBundles, successors)
result := filter.InPlace(allBundles, successors)

// sort before comparison for stable order
slices.SortFunc(result, compare.ByVersion)
Expand Down
3 changes: 1 addition & 2 deletions internal/resolve/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"github.com/operator-framework/operator-controller/internal/catalogmetadata/compare"
"github.com/operator-framework/operator-controller/internal/catalogmetadata/filter"
filterutil "github.com/operator-framework/operator-controller/internal/util/filter"
slicesutil "github.com/operator-framework/operator-controller/internal/util/slices"
)

type ValidationFunc func(*declcfg.Bundle) error
Expand Down Expand Up @@ -120,7 +119,7 @@ func (r *CatalogResolver) Resolve(ctx context.Context, ext *ocv1.ClusterExtensio
}

// Apply the predicates to get the candidate bundles
packageFBC.Bundles = slicesutil.RemoveInPlace(packageFBC.Bundles, filterutil.And(predicates...))
packageFBC.Bundles = filterutil.InPlace(packageFBC.Bundles, filterutil.And(predicates...))
cs.MatchedBundles = len(packageFBC.Bundles)
if len(packageFBC.Bundles) == 0 {
return nil
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package filter

import "slices"

// Predicate returns true if the object should be kept when filtering
type Predicate[T any] func(entity T) bool

Expand Down Expand Up @@ -30,3 +32,20 @@ func Not[T any](predicate Predicate[T]) Predicate[T] {
return !predicate(obj)
}
}

// Filter creates a new slice with all elements from s for which the test returns true
func Filter[T any](s []T, test Predicate[T]) []T {
var out []T
for i := 0; i < len(s); i++ {
if test(s[i]) {
out = append(out, s[i])
}
}
return slices.Clip(out)
}

// InPlace keeps all elements from s for which test returns true and removes all others.
// Elements between new length and original length are zeroed out.
func InPlace[T any](s []T, test Predicate[T]) []T {
return slices.DeleteFunc(s, Not(test))
}
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,95 @@ func TestNot(t *testing.T) {
})
}
}

func TestFilter(t *testing.T) {
tests := []struct {
name string
slice []int
predicate filter.Predicate[int]
want []int
}{
{
name: "all match",
slice: []int{1, 2, 3, 4, 5},
predicate: func(i int) bool { return i > 0 },
want: []int{1, 2, 3, 4, 5},
},
{
name: "some match",
slice: []int{1, 2, 3, 4, 5},
predicate: func(i int) bool { return i > 3 },
want: []int{4, 5},
},
{
name: "none match",
slice: []int{1, 2, 3, 4, 5},
predicate: func(i int) bool { return i > 5 },
want: nil,
},
{
name: "empty slice",
slice: []int{},
predicate: func(i int) bool { return i > 5 },
want: nil,
},
{
name: "nil slice",
slice: nil,
predicate: func(i int) bool { return i > 5 },
want: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := filter.Filter(tt.slice, tt.predicate)
require.Equal(t, tt.want, got, "Filter() = %v, want %v", got, tt.want)
})
}
}

func TestInPlace(t *testing.T) {
tests := []struct {
name string
slice []int
predicate filter.Predicate[int]
want []int
}{
{
name: "all match",
slice: []int{1, 2, 3, 4, 5},
predicate: func(i int) bool { return i > 0 },
want: []int{1, 2, 3, 4, 5},
},
{
name: "some match",
slice: []int{1, 2, 3, 4, 5},
predicate: func(i int) bool { return i > 3 },
want: []int{4, 5},
},
{
name: "none match",
slice: []int{1, 2, 3, 4, 5},
predicate: func(i int) bool { return i > 5 },
want: []int{},
},
{
name: "empty slice",
slice: []int{},
predicate: func(i int) bool { return i > 5 },
want: []int{},
},
{
name: "nil slice",
slice: nil,
predicate: func(i int) bool { return i > 5 },
want: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := filter.InPlace(tt.slice, tt.predicate)
require.Equal(t, tt.want, got, "Filter() = %v, want %v", got, tt.want)
})
}
}
24 changes: 0 additions & 24 deletions internal/util/slices/slices.go

This file was deleted.

102 changes: 0 additions & 102 deletions internal/util/slices/slices_test.go

This file was deleted.

0 comments on commit 364c6b3

Please sign in to comment.