-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[kafka] Return slice instead of iterator from
Batched
(#336)
- Loading branch information
1 parent
ec558fe
commit 57a7bda
Showing
4 changed files
with
24 additions
and
53 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package kafkalib | ||
|
||
// batched splits a slice of items into a slice of step-sized slices. | ||
func batched[T any](items []T, step int) [][]T { | ||
step = max(step, 1) | ||
var result [][]T | ||
for index := 0; index < len(items); { | ||
end := min(index+step, len(items)) | ||
result = append(result, items[index:end]) | ||
index = end | ||
} | ||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,35 @@ | ||
package iterator | ||
package kafkalib | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestBatchIterator(t *testing.T) { | ||
func TestBatched(t *testing.T) { | ||
// length of items is 0 | ||
{ | ||
batches, err := Collect(Batched([]int{}, 2)) | ||
assert.NoError(t, err) | ||
batches := batched([]int{}, 2) | ||
assert.Empty(t, batches) | ||
} | ||
// length of items is 1 | ||
{ | ||
batches, err := Collect(Batched([]int{1}, 2)) | ||
assert.NoError(t, err) | ||
batches := batched([]int{1}, 2) | ||
assert.Equal(t, [][]int{{1}}, batches) | ||
} | ||
// n is 0 | ||
{ | ||
batches, err := Collect(Batched([]int{1, 2}, 0)) | ||
assert.NoError(t, err) | ||
batches := batched([]int{1, 2}, 0) | ||
assert.Equal(t, [][]int{{1}, {2}}, batches) | ||
} | ||
// length of items is a multiple of n | ||
{ | ||
batches, err := Collect(Batched([]int{1, 2, 3, 4}, 2)) | ||
assert.NoError(t, err) | ||
batches := batched([]int{1, 2, 3, 4}, 2) | ||
assert.Equal(t, [][]int{{1, 2}, {3, 4}}, batches) | ||
} | ||
// length of items is not a multiple of n | ||
{ | ||
batches, err := Collect(Batched([]int{1, 2, 3, 4, 5}, 2)) | ||
assert.NoError(t, err) | ||
batches := batched([]int{1, 2, 3, 4, 5}, 2) | ||
assert.Equal(t, [][]int{{1, 2}, {3, 4}, {5}}, batches) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters