Skip to content

Commit 82d8ae6

Browse files
committed
Implement metautils.FilterList
1 parent 745225e commit 82d8ae6

File tree

5 files changed

+44
-3
lines changed

5 files changed

+44
-3
lines changed

.github/workflows/golangci-lint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ jobs:
1717
- name: golangci-lint
1818
uses: golangci/golangci-lint-action@v3
1919
with:
20-
version: v1.43.0
20+
version: v1.47.2

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ require (
3838
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
3939
github.com/golang/protobuf v1.5.2 // indirect
4040
github.com/google/gnostic v0.5.7-v3refs // indirect
41-
github.com/google/go-cmp v0.5.5 // indirect
41+
github.com/google/go-cmp v0.5.8 // indirect
4242
github.com/google/gofuzz v1.1.0 // indirect
4343
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
4444
github.com/google/uuid v1.1.2 // indirect

go.sum

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,9 @@ github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
229229
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
230230
github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
231231
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
232-
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
233232
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
233+
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
234+
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
234235
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
235236
github.com/google/gofuzz v1.1.0 h1:Hsa8mG0dQ46ij8Sl2AYJDUv1oA9/d6Vk+3LG99Oe02g=
236237
github.com/google/gofuzz v1.1.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=

metautils/metautils.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,21 @@ func EachListItem(list client.ObjectList, f func(obj client.Object) error) error
338338
})
339339
}
340340

341+
// FilterList filters the list with the given function, mutating it in-place with the filtered objects.
342+
func FilterList(list client.ObjectList, f func(obj client.Object) bool) error {
343+
var filtered []client.Object
344+
if err := EachListItem(list, func(obj client.Object) error {
345+
if f(obj) {
346+
filtered = append(filtered, obj)
347+
}
348+
return nil
349+
}); err != nil {
350+
return fmt.Errorf("error filtering list: %w", err)
351+
}
352+
353+
return SetList(list, filtered)
354+
}
355+
341356
// SetLabel sets the given label on the object.
342357
func SetLabel(obj metav1.Object, key, value string) {
343358
labels := obj.GetLabels()

metautils/metautils_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package metautils_test
1616

1717
import (
1818
"reflect"
19+
"strings"
1920

2021
"github.com/golang/mock/gomock"
2122
. "github.com/onmetal/controller-utils/metautils"
@@ -555,4 +556,28 @@ var _ = Describe("Metautils", func() {
555556
Entry("other keys present", map[string]string{"bar": "baz"}, map[string]string{"foo": "bar"}, map[string]string{"bar": "baz", "foo": "bar"}),
556557
Entry("partial other keys, same key", map[string]string{"foo": "baz", "bar": "baz"}, map[string]string{"foo": "bar"}, map[string]string{"foo": "bar", "bar": "baz"}),
557558
)
559+
560+
Describe("FilterList", func() {
561+
It("should filter the list with the given function", func() {
562+
list := &corev1.SecretList{
563+
Items: []corev1.Secret{
564+
{ObjectMeta: metav1.ObjectMeta{Namespace: "foo", Name: "bar"}},
565+
{ObjectMeta: metav1.ObjectMeta{Namespace: "foo", Name: "baz"}},
566+
{ObjectMeta: metav1.ObjectMeta{Namespace: "foo", Name: "qux"}},
567+
{ObjectMeta: metav1.ObjectMeta{Namespace: "bar", Name: "bar"}},
568+
},
569+
}
570+
571+
Expect(FilterList(list, func(obj client.Object) bool {
572+
return obj.GetNamespace() == "foo" && strings.HasPrefix(obj.GetName(), "b")
573+
})).To(Succeed())
574+
575+
Expect(list).To(Equal(&corev1.SecretList{
576+
Items: []corev1.Secret{
577+
{ObjectMeta: metav1.ObjectMeta{Namespace: "foo", Name: "bar"}},
578+
{ObjectMeta: metav1.ObjectMeta{Namespace: "foo", Name: "baz"}},
579+
},
580+
}))
581+
})
582+
})
558583
})

0 commit comments

Comments
 (0)