Skip to content

Commit

Permalink
Use the new upcoming iterator style instead of our custom Range()
Browse files Browse the repository at this point in the history
  • Loading branch information
RichieSams committed Feb 14, 2024
1 parent 0bfcac2 commit 18aa7c3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
6 changes: 3 additions & 3 deletions internal/exp/metrics/staleness/staleness.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ type Map[T any] interface {
LoadOrStore(key identity.Stream, value T) (T, bool)
// Remove the value at key from the map
Delete(key identity.Stream)
// Range calls f sequentially for each key and value present in the map.
// If f returns false, range stops the iteration.
Range(f func(key identity.Stream, value T) bool)
// Items returns an iterator function that in future go version can be used with range
// See: https://go.dev/wiki/RangefuncExperiment
Items() func(yield func(identity.Stream, T) bool) bool
}

type Staleness[T any] struct {
Expand Down
14 changes: 8 additions & 6 deletions internal/exp/metrics/staleness/staleness_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,14 @@ func (rm *RawMap[K, V]) Delete(key K) {
delete(*rm, key)
}

func (rm *RawMap[K, V]) Range(f func(key K, value V) bool) {
for key, value := range *rm {
if !f(key, value) {
return
func (rm *RawMap[K, V]) Items() func(yield func(K, V) bool) bool {
return func(yield func(K, V) bool) bool {
for k, v := range *rm {
if !yield(k, v) {
break
}
}
return false
}
}

Expand Down Expand Up @@ -122,10 +125,9 @@ func TestStaleness(t *testing.T) {
func validateStalenessMapEntries(t *testing.T, expected map[identity.Stream]int, sm *Staleness[int]) {
actual := map[identity.Stream]int{}

sm.Range(func(key identity.Stream, value int) bool {
sm.Items()(func(key identity.Stream, value int) bool {
actual[key] = value
return true
})

require.Equal(t, expected, actual)
}

0 comments on commit 18aa7c3

Please sign in to comment.