Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Filter to set #389

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions changelog/v0.23.10/set-filter.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
changelog:
- type: NEW_FEATURE
description: Add a `Filter` method to `v2sets.ResourceSet` to allow for efficient filtering on sets.
issueLink: https://github.com/solo-io/skv2/issues/388


24 changes: 24 additions & 0 deletions contrib/pkg/sets/v2/sets.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ type ResourceSet[T client.Object] interface {
Has(resource T) bool
// Delete the key matching the resource
Delete(resource T)
// Return a set with the matching objects filtered out
Filter(filterResource ...func(T) bool) ResourceSet[T]
// Return the union with the provided set
Union(set ResourceSet[T]) ResourceSet[T]
// Return the difference with the provided set
Expand Down Expand Up @@ -122,6 +124,28 @@ func (s *resourceSet[T]) UnsortedList(filterResource ...func(T) bool) []T {
return resources
}

func (s *resourceSet[T]) Filter(filterResource ...func(T) bool) ResourceSet[T] {
s.lock.RLock()
defer s.lock.RUnlock()

keys := s.set.UnsortedList()
resources := NewResourceSet[T]()

for _, key := range keys {
var filtered bool
for _, filter := range filterResource {
if filter(s.mapping[key]) {
filtered = true
break
}
}
if !filtered {
resources.Insert(s.mapping[key])
}
}
return resources
}

func (s *resourceSet[T]) Map() map[string]T {
s.lock.RLock()
defer s.lock.RUnlock()
Expand Down