Skip to content

Commit bf04273

Browse files
committed
add 'Not' in addition to 'WhereNot'
1 parent 4c639f3 commit bf04273

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

go/fn/object.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -793,7 +793,7 @@ func (o KubeObjects) String() string {
793793
}
794794

795795
// Where will return the subset of objects in KubeObjects such that f(object) returns 'true'.
796-
func (o KubeObjects) Where(f func(o *KubeObject) bool) KubeObjects {
796+
func (o KubeObjects) Where(f func(*KubeObject) bool) KubeObjects {
797797
var result KubeObjects
798798
for _, obj := range o {
799799
if f(obj) {
@@ -803,15 +803,17 @@ func (o KubeObjects) Where(f func(o *KubeObject) bool) KubeObjects {
803803
return result
804804
}
805805

806+
// Not returns will return a function that returns the opposite of f(object), i.e. !f(object)
807+
func Not(f func(*KubeObject) bool) func(o *KubeObject) bool {
808+
return func(o *KubeObject) bool {
809+
return !f(o)
810+
}
811+
}
812+
806813
// WhereNot will return the subset of objects in KubeObjects such that f(object) returns 'false'.
814+
// This is a shortcut for Where(Not(f)).
807815
func (o KubeObjects) WhereNot(f func(o *KubeObject) bool) KubeObjects {
808-
var result KubeObjects
809-
for _, obj := range o {
810-
if !f(obj) {
811-
result = append(result, obj)
812-
}
813-
}
814-
return result
816+
return o.Where(Not(f))
815817
}
816818

817819
// IsGVK returns a function that checks if a KubeObject has a certain GVK.

0 commit comments

Comments
 (0)