Skip to content

Commit 2edf415

Browse files
committed
add Get
1 parent eb0dcc9 commit 2edf415

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

contrib/pkg/sets/v2/sets.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,12 @@ type ResourceSet[T client.Object] interface {
4949
Difference(set ResourceSet[T]) ResourceSet[T]
5050
// Return the intersection with the provided set
5151
Intersection(set ResourceSet[T]) ResourceSet[T]
52-
// Find the resource with the given ID
52+
// Find the resource with the given ID.
53+
// Returns a NotFoundErr error if the resource is not found.
5354
Find(resource ezkube.ResourceId) (T, error)
55+
// Find the resource with the given ID.
56+
// Returns nil if the resource is not found.
57+
Get(resource ezkube.ResourceId) T
5458
// Get the length of the set
5559
Len() int
5660
Length() int
@@ -274,6 +278,22 @@ func (s *resourceSet[T]) Intersection(set ResourceSet[T]) ResourceSet[T] {
274278
return result
275279
}
276280

281+
func (s *resourceSet[T]) Get(resource ezkube.ResourceId) T {
282+
s.lock.RLock()
283+
defer s.lock.RUnlock()
284+
285+
insertIndex, found := slices.BinarySearchFunc(
286+
s.set,
287+
resource,
288+
func(a T, b ezkube.ResourceId) int { return s.compareFunc(a, b) },
289+
)
290+
if found {
291+
return s.set[insertIndex]
292+
}
293+
var r T
294+
return r
295+
}
296+
277297
func (s *resourceSet[T]) Find(resource ezkube.ResourceId) (T, error) {
278298
s.lock.RLock()
279299
defer s.lock.RUnlock()

0 commit comments

Comments
 (0)