Skip to content

Commit 9864ba7

Browse files
perdasilvaPer Goncalves da Silva
andauthored
🌱 Refactor filter package (#1734)
* Move filter code to util/slices Signed-off-by: Per Goncalves da Silva <pegoncal@redhat.com> * Rename Filter to RemoveInPlace Signed-off-by: Per Goncalves da Silva <pegoncal@redhat.com> * Add Filter function and update unit tests Signed-off-by: Per Goncalves da Silva <pegoncal@redhat.com> * Refactor slice utils package structure Signed-off-by: Per Goncalves da Silva <pegoncal@redhat.com> * Address reviewer comments Signed-off-by: Per Goncalves da Silva <pegoncal@redhat.com> --------- Signed-off-by: Per Goncalves da Silva <pegoncal@redhat.com> Co-authored-by: Per Goncalves da Silva <pegoncal@redhat.com>
1 parent c0a2964 commit 9864ba7

File tree

7 files changed

+271
-96
lines changed

7 files changed

+271
-96
lines changed

internal/catalogmetadata/filter/bundle_predicates.go

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

88
"github.com/operator-framework/operator-controller/internal/bundleutil"
9+
"github.com/operator-framework/operator-controller/internal/util/filter"
910
)
1011

11-
func InMastermindsSemverRange(semverRange *mmsemver.Constraints) Predicate[declcfg.Bundle] {
12+
func InMastermindsSemverRange(semverRange *mmsemver.Constraints) filter.Predicate[declcfg.Bundle] {
1213
return func(b declcfg.Bundle) bool {
1314
bVersion, err := bundleutil.GetVersion(b)
1415
if err != nil {
@@ -26,7 +27,7 @@ func InMastermindsSemverRange(semverRange *mmsemver.Constraints) Predicate[declc
2627
}
2728
}
2829

29-
func InAnyChannel(channels ...declcfg.Channel) Predicate[declcfg.Bundle] {
30+
func InAnyChannel(channels ...declcfg.Channel) filter.Predicate[declcfg.Bundle] {
3031
return func(bundle declcfg.Bundle) bool {
3132
for _, ch := range channels {
3233
for _, entry := range ch.Entries {

internal/catalogmetadata/filter/filter_test.go

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

internal/catalogmetadata/filter/successors.go

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

1111
ocv1 "github.com/operator-framework/operator-controller/api/v1"
12+
"github.com/operator-framework/operator-controller/internal/util/filter"
1213
)
1314

14-
func SuccessorsOf(installedBundle ocv1.BundleMetadata, channels ...declcfg.Channel) (Predicate[declcfg.Bundle], error) {
15+
func SuccessorsOf(installedBundle ocv1.BundleMetadata, channels ...declcfg.Channel) (filter.Predicate[declcfg.Bundle], error) {
1516
installedBundleVersion, err := mmsemver.NewVersion(installedBundle.Version)
1617
if err != nil {
1718
return nil, fmt.Errorf("parsing installed bundle %q version %q: %w", installedBundle.Name, installedBundle.Version, err)
@@ -28,13 +29,13 @@ func SuccessorsOf(installedBundle ocv1.BundleMetadata, channels ...declcfg.Chann
2829
}
2930

3031
// We need either successors or current version (no upgrade)
31-
return Or(
32+
return filter.Or(
3233
successorsPredicate,
3334
InMastermindsSemverRange(installedVersionConstraint),
3435
), nil
3536
}
3637

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

internal/catalogmetadata/filter/successors_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +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+
"github.com/operator-framework/operator-controller/internal/util/filter"
1920
)
2021

2122
func TestSuccessorsPredicate(t *testing.T) {
@@ -160,7 +161,7 @@ func TestSuccessorsPredicate(t *testing.T) {
160161
for _, bundle := range bundleSet {
161162
allBundles = append(allBundles, bundle)
162163
}
163-
result := Filter(allBundles, successors)
164+
result := filter.InPlace(allBundles, successors)
164165

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

internal/resolve/catalog.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/operator-framework/operator-controller/internal/bundleutil"
2323
"github.com/operator-framework/operator-controller/internal/catalogmetadata/compare"
2424
"github.com/operator-framework/operator-controller/internal/catalogmetadata/filter"
25+
filterutil "github.com/operator-framework/operator-controller/internal/util/filter"
2526
)
2627

2728
type ValidationFunc func(*declcfg.Bundle) error
@@ -75,7 +76,7 @@ func (r *CatalogResolver) Resolve(ctx context.Context, ext *ocv1.ClusterExtensio
7576

7677
var catStats []*catStat
7778

78-
resolvedBundles := []foundBundle{}
79+
var resolvedBundles []foundBundle
7980
var priorDeprecation *declcfg.Deprecation
8081

8182
listOptions := []client.ListOption{
@@ -96,7 +97,7 @@ func (r *CatalogResolver) Resolve(ctx context.Context, ext *ocv1.ClusterExtensio
9697
cs.PackageFound = true
9798
cs.TotalBundles = len(packageFBC.Bundles)
9899

99-
var predicates []filter.Predicate[declcfg.Bundle]
100+
var predicates []filterutil.Predicate[declcfg.Bundle]
100101
if len(channels) > 0 {
101102
channelSet := sets.New(channels...)
102103
filteredChannels := slices.DeleteFunc(packageFBC.Channels, func(c declcfg.Channel) bool {
@@ -118,7 +119,7 @@ func (r *CatalogResolver) Resolve(ctx context.Context, ext *ocv1.ClusterExtensio
118119
}
119120

120121
// Apply the predicates to get the candidate bundles
121-
packageFBC.Bundles = filter.Filter(packageFBC.Bundles, filter.And(predicates...))
122+
packageFBC.Bundles = filterutil.InPlace(packageFBC.Bundles, filterutil.And(predicates...))
122123
cs.MatchedBundles = len(packageFBC.Bundles)
123124
if len(packageFBC.Bundles) == 0 {
124125
return nil

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

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
11
package filter
22

3-
import (
4-
"slices"
5-
)
3+
import "slices"
64

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

10-
// Filter filters a slice accordingly to
11-
func Filter[T any](in []T, test Predicate[T]) []T {
12-
return slices.DeleteFunc(in, Not(test))
13-
}
14-
158
func And[T any](predicates ...Predicate[T]) Predicate[T] {
169
return func(obj T) bool {
1710
for _, predicate := range predicates {
@@ -39,3 +32,21 @@ func Not[T any](predicate Predicate[T]) Predicate[T] {
3932
return !predicate(obj)
4033
}
4134
}
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 modifies s by removing any element for which test returns false.
48+
// InPlace zeroes the elements between the new length and the original length in s.
49+
// The returned slice is of the new length.
50+
func InPlace[T any](s []T, test Predicate[T]) []T {
51+
return slices.DeleteFunc(s, Not(test))
52+
}

0 commit comments

Comments
 (0)