Skip to content

Commit a7cf57d

Browse files
author
Per Goncalves da Silva
committed
Address reviewer comments
Signed-off-by: Per Goncalves da Silva <pegoncal@redhat.com>
1 parent fdebc24 commit a7cf57d

File tree

8 files changed

+121
-137
lines changed

8 files changed

+121
-137
lines changed

internal/catalogmetadata/filter/bundle_predicates.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import (
66
"github.com/operator-framework/operator-registry/alpha/declcfg"
77

88
"github.com/operator-framework/operator-controller/internal/bundleutil"
9-
filterutil "github.com/operator-framework/operator-controller/internal/util/filter"
9+
"github.com/operator-framework/operator-controller/internal/util/filter"
1010
)
1111

12-
func InMastermindsSemverRange(semverRange *mmsemver.Constraints) filterutil.Predicate[declcfg.Bundle] {
12+
func InMastermindsSemverRange(semverRange *mmsemver.Constraints) filter.Predicate[declcfg.Bundle] {
1313
return func(b declcfg.Bundle) bool {
1414
bVersion, err := bundleutil.GetVersion(b)
1515
if err != nil {
@@ -27,7 +27,7 @@ func InMastermindsSemverRange(semverRange *mmsemver.Constraints) filterutil.Pred
2727
}
2828
}
2929

30-
func InAnyChannel(channels ...declcfg.Channel) filterutil.Predicate[declcfg.Bundle] {
30+
func InAnyChannel(channels ...declcfg.Channel) filter.Predicate[declcfg.Bundle] {
3131
return func(bundle declcfg.Bundle) bool {
3232
for _, ch := range channels {
3333
for _, entry := range ch.Entries {

internal/catalogmetadata/filter/successors.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import (
99
"github.com/operator-framework/operator-registry/alpha/declcfg"
1010

1111
ocv1 "github.com/operator-framework/operator-controller/api/v1"
12-
filterutil "github.com/operator-framework/operator-controller/internal/util/filter"
12+
"github.com/operator-framework/operator-controller/internal/util/filter"
1313
)
1414

15-
func SuccessorsOf(installedBundle ocv1.BundleMetadata, channels ...declcfg.Channel) (filterutil.Predicate[declcfg.Bundle], error) {
15+
func SuccessorsOf(installedBundle ocv1.BundleMetadata, channels ...declcfg.Channel) (filter.Predicate[declcfg.Bundle], error) {
1616
installedBundleVersion, err := mmsemver.NewVersion(installedBundle.Version)
1717
if err != nil {
1818
return nil, fmt.Errorf("parsing installed bundle %q version %q: %w", installedBundle.Name, installedBundle.Version, err)
@@ -29,13 +29,13 @@ func SuccessorsOf(installedBundle ocv1.BundleMetadata, channels ...declcfg.Chann
2929
}
3030

3131
// We need either successors or current version (no upgrade)
32-
return filterutil.Or(
32+
return filter.Or(
3333
successorsPredicate,
3434
InMastermindsSemverRange(installedVersionConstraint),
3535
), nil
3636
}
3737

38-
func legacySuccessor(installedBundle ocv1.BundleMetadata, channels ...declcfg.Channel) (filterutil.Predicate[declcfg.Bundle], error) {
38+
func legacySuccessor(installedBundle ocv1.BundleMetadata, channels ...declcfg.Channel) (filter.Predicate[declcfg.Bundle], error) {
3939
installedBundleVersion, err := bsemver.Parse(installedBundle.Version)
4040
if err != nil {
4141
return nil, fmt.Errorf("error parsing installed bundle version: %w", err)

internal/catalogmetadata/filter/successors_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
ocv1 "github.com/operator-framework/operator-controller/api/v1"
1717
"github.com/operator-framework/operator-controller/internal/bundleutil"
1818
"github.com/operator-framework/operator-controller/internal/catalogmetadata/compare"
19-
slicesutil "github.com/operator-framework/operator-controller/internal/util/slices"
19+
"github.com/operator-framework/operator-controller/internal/util/filter"
2020
)
2121

2222
func TestSuccessorsPredicate(t *testing.T) {
@@ -161,7 +161,7 @@ func TestSuccessorsPredicate(t *testing.T) {
161161
for _, bundle := range bundleSet {
162162
allBundles = append(allBundles, bundle)
163163
}
164-
result := slicesutil.RemoveInPlace(allBundles, successors)
164+
result := filter.InPlace(allBundles, successors)
165165

166166
// sort before comparison for stable order
167167
slices.SortFunc(result, compare.ByVersion)

internal/resolve/catalog.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"github.com/operator-framework/operator-controller/internal/catalogmetadata/compare"
2424
"github.com/operator-framework/operator-controller/internal/catalogmetadata/filter"
2525
filterutil "github.com/operator-framework/operator-controller/internal/util/filter"
26-
slicesutil "github.com/operator-framework/operator-controller/internal/util/slices"
2726
)
2827

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

122121
// Apply the predicates to get the candidate bundles
123-
packageFBC.Bundles = slicesutil.RemoveInPlace(packageFBC.Bundles, filterutil.And(predicates...))
122+
packageFBC.Bundles = filterutil.InPlace(packageFBC.Bundles, filterutil.And(predicates...))
124123
cs.MatchedBundles = len(packageFBC.Bundles)
125124
if len(packageFBC.Bundles) == 0 {
126125
return nil

internal/util/filter/predicates.go renamed to internal/util/filter/filter.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package filter
22

3+
import "slices"
4+
35
// Predicate returns true if the object should be kept when filtering
46
type Predicate[T any] func(entity T) bool
57

@@ -30,3 +32,20 @@ func Not[T any](predicate Predicate[T]) Predicate[T] {
3032
return !predicate(obj)
3133
}
3234
}
35+
36+
// Filter creates a new slice with all elements from s for which the test returns true
37+
func Filter[T any](s []T, test Predicate[T]) []T {
38+
var out []T
39+
for i := 0; i < len(s); i++ {
40+
if test(s[i]) {
41+
out = append(out, s[i])
42+
}
43+
}
44+
return slices.Clip(out)
45+
}
46+
47+
// InPlace keeps all elements from s for which test returns true and removes all others.
48+
// Elements between new length and original length are zeroed out.
49+
func InPlace[T any](s []T, test Predicate[T]) []T {
50+
return slices.DeleteFunc(s, Not(test))
51+
}

internal/util/filter/predicates_test.go renamed to internal/util/filter/filter_test.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,95 @@ func TestNot(t *testing.T) {
145145
})
146146
}
147147
}
148+
149+
func TestFilter(t *testing.T) {
150+
tests := []struct {
151+
name string
152+
slice []int
153+
predicate filter.Predicate[int]
154+
want []int
155+
}{
156+
{
157+
name: "all match",
158+
slice: []int{1, 2, 3, 4, 5},
159+
predicate: func(i int) bool { return i > 0 },
160+
want: []int{1, 2, 3, 4, 5},
161+
},
162+
{
163+
name: "some match",
164+
slice: []int{1, 2, 3, 4, 5},
165+
predicate: func(i int) bool { return i > 3 },
166+
want: []int{4, 5},
167+
},
168+
{
169+
name: "none match",
170+
slice: []int{1, 2, 3, 4, 5},
171+
predicate: func(i int) bool { return i > 5 },
172+
want: nil,
173+
},
174+
{
175+
name: "empty slice",
176+
slice: []int{},
177+
predicate: func(i int) bool { return i > 5 },
178+
want: nil,
179+
},
180+
{
181+
name: "nil slice",
182+
slice: nil,
183+
predicate: func(i int) bool { return i > 5 },
184+
want: nil,
185+
},
186+
}
187+
for _, tt := range tests {
188+
t.Run(tt.name, func(t *testing.T) {
189+
got := filter.Filter(tt.slice, tt.predicate)
190+
require.Equal(t, tt.want, got, "Filter() = %v, want %v", got, tt.want)
191+
})
192+
}
193+
}
194+
195+
func TestInPlace(t *testing.T) {
196+
tests := []struct {
197+
name string
198+
slice []int
199+
predicate filter.Predicate[int]
200+
want []int
201+
}{
202+
{
203+
name: "all match",
204+
slice: []int{1, 2, 3, 4, 5},
205+
predicate: func(i int) bool { return i > 0 },
206+
want: []int{1, 2, 3, 4, 5},
207+
},
208+
{
209+
name: "some match",
210+
slice: []int{1, 2, 3, 4, 5},
211+
predicate: func(i int) bool { return i > 3 },
212+
want: []int{4, 5},
213+
},
214+
{
215+
name: "none match",
216+
slice: []int{1, 2, 3, 4, 5},
217+
predicate: func(i int) bool { return i > 5 },
218+
want: []int{},
219+
},
220+
{
221+
name: "empty slice",
222+
slice: []int{},
223+
predicate: func(i int) bool { return i > 5 },
224+
want: []int{},
225+
},
226+
{
227+
name: "nil slice",
228+
slice: nil,
229+
predicate: func(i int) bool { return i > 5 },
230+
want: nil,
231+
},
232+
}
233+
for _, tt := range tests {
234+
t.Run(tt.name, func(t *testing.T) {
235+
got := filter.InPlace(tt.slice, tt.predicate)
236+
require.Equal(t, tt.want, got, "Filter() = %v, want %v", got, tt.want)
237+
})
238+
}
239+
}

internal/util/slices/slices.go

Lines changed: 0 additions & 24 deletions
This file was deleted.

internal/util/slices/slices_test.go

Lines changed: 0 additions & 102 deletions
This file was deleted.

0 commit comments

Comments
 (0)