Skip to content

Commit ff622bd

Browse files
authored
Merge pull request #2031 from erikgb/feat/not-predicate
✨ feat: add NOT predicate
2 parents c2f04bb + 02c1fae commit ff622bd

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

pkg/predicate/predicate.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ var _ Predicate = GenerationChangedPredicate{}
5050
var _ Predicate = AnnotationChangedPredicate{}
5151
var _ Predicate = or{}
5252
var _ Predicate = and{}
53+
var _ Predicate = not{}
5354

5455
// Funcs is a function that implements Predicate.
5556
type Funcs struct {
@@ -340,6 +341,35 @@ func (o or) Generic(e event.GenericEvent) bool {
340341
return false
341342
}
342343

344+
// Not returns a predicate that implements a logical NOT of the predicate passed to it.
345+
func Not(predicate Predicate) Predicate {
346+
return not{predicate}
347+
}
348+
349+
type not struct {
350+
predicate Predicate
351+
}
352+
353+
func (n not) InjectFunc(f inject.Func) error {
354+
return f(n.predicate)
355+
}
356+
357+
func (n not) Create(e event.CreateEvent) bool {
358+
return !n.predicate.Create(e)
359+
}
360+
361+
func (n not) Update(e event.UpdateEvent) bool {
362+
return !n.predicate.Update(e)
363+
}
364+
365+
func (n not) Delete(e event.DeleteEvent) bool {
366+
return !n.predicate.Delete(e)
367+
}
368+
369+
func (n not) Generic(e event.GenericEvent) bool {
370+
return !n.predicate.Generic(e)
371+
}
372+
343373
// LabelSelectorPredicate constructs a Predicate from a LabelSelector.
344374
// Only objects matching the LabelSelector will be admitted.
345375
func LabelSelectorPredicate(s metav1.LabelSelector) (Predicate, error) {

pkg/predicate/predicate_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,28 @@ var _ = Describe("Predicate", func() {
879879
Expect(prct.injected).To(BeTrue())
880880
})
881881
})
882+
Describe("When checking a Not predicate", func() {
883+
It("should return false when its predicate returns true", func() {
884+
n := predicate.Not(passFuncs)
885+
Expect(n.Create(event.CreateEvent{})).To(BeFalse())
886+
Expect(n.Update(event.UpdateEvent{})).To(BeFalse())
887+
Expect(n.Delete(event.DeleteEvent{})).To(BeFalse())
888+
Expect(n.Generic(event.GenericEvent{})).To(BeFalse())
889+
})
890+
It("should return true when its predicate returns false", func() {
891+
n := predicate.Not(failFuncs)
892+
Expect(n.Create(event.CreateEvent{})).To(BeTrue())
893+
Expect(n.Update(event.UpdateEvent{})).To(BeTrue())
894+
Expect(n.Delete(event.DeleteEvent{})).To(BeTrue())
895+
Expect(n.Generic(event.GenericEvent{})).To(BeTrue())
896+
})
897+
It("should inject into its predicate", func() {
898+
prct := &injectablePredicate{}
899+
n := predicate.Not(prct)
900+
Expect(injectFunc(n)).To(Succeed())
901+
Expect(prct.injected).To(BeTrue())
902+
})
903+
})
882904
})
883905

884906
Describe("NewPredicateFuncs with a namespace filter function", func() {

0 commit comments

Comments
 (0)