Skip to content

Commit

Permalink
use 'Not' instead of 'WhereNot'
Browse files Browse the repository at this point in the history
  • Loading branch information
natasha41575 committed Jun 7, 2022
1 parent 4c639f3 commit 1759fa5
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
2 changes: 1 addition & 1 deletion go/fn/examples/example_select_exclude_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ func Example_selectExclude() {
// NOT have the label foo=bar, and removes the rest.
func selectResources(rl *fn.ResourceList) (bool, error) {
rl.Items = rl.Items.Where(fn.IsGVK("apps/v1", "Deployment")).
WhereNot(fn.HasLabels(map[string]string{"foo": "bar"}))
Where(fn.Not(fn.HasLabels(map[string]string{"foo": "bar"})))
return true, nil
}
9 changes: 8 additions & 1 deletion go/fn/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,7 @@ func (o KubeObjects) String() string {
}

// Where will return the subset of objects in KubeObjects such that f(object) returns 'true'.
func (o KubeObjects) Where(f func(o *KubeObject) bool) KubeObjects {
func (o KubeObjects) Where(f func(*KubeObject) bool) KubeObjects {
var result KubeObjects
for _, obj := range o {
if f(obj) {
Expand All @@ -803,6 +803,13 @@ func (o KubeObjects) Where(f func(o *KubeObject) bool) KubeObjects {
return result
}

// Not returns will return a function that returns the opposite of f(object), i.e. !f(object)
func Not(f func(*KubeObject) bool) func(o *KubeObject) bool {
return func(o *KubeObject) bool {
return !f(o)
}
}

// WhereNot will return the subset of objects in KubeObjects such that f(object) returns 'false'.
func (o KubeObjects) WhereNot(f func(o *KubeObject) bool) KubeObjects {
var result KubeObjects
Expand Down
4 changes: 2 additions & 2 deletions go/fn/object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ metadata:
bar: foo`)

// exclude all services and meta resources
items = input.WhereNot(IsGVK("apps/v1", "Service")).WhereNot(IsMetaResource())
items = input.Where(Not(IsGVK("apps/v1", "Service"))).WhereNot(IsMetaResource())
assert.Equal(t, items.String(), `apiVersion: apps/v1
kind: Deployment
metadata:
Expand All @@ -389,7 +389,7 @@ metadata:
bar: foo`)

// exclude all resources with the annotation foo=bar
items = input.WhereNot(HasAnnotations(map[string]string{"foo": "bar"}))
items = input.Where(Not(HasAnnotations(map[string]string{"foo": "bar"})))
assert.Equal(t, items.String(), `apiVersion: apps/v1
kind: Deployment
metadata:
Expand Down

0 comments on commit 1759fa5

Please sign in to comment.