utils/upload: init

This commit is contained in:
Blake Mizerany 2024-04-02 14:15:21 -07:00
parent 618eb5b909
commit c95f97689b
2 changed files with 64 additions and 0 deletions

27
utils/upload/upload.go Normal file
View File

@ -0,0 +1,27 @@
package upload
import (
"iter"
"golang.org/x/exp/constraints"
)
type Chunk[I constraints.Integer] struct {
Offset I
Size I
}
// Chunks yields a sequence of a part number and a Chunk. The Chunk is the offset
// and size of the chunk. The last chunk may be smaller than chunkSize if size is
// not a multiple of chunkSize.
//
// The first part number is 1 and increases monotonically.
func Chunks[I constraints.Integer](size, chunkSize I) iter.Seq2[int, Chunk[I]] {
return func(yield func(int, Chunk[I]) bool) {
var n int
for off := I(0); off < size; off += chunkSize {
n++
yield(n, Chunk[I]{off, min(chunkSize, size-off)})
}
}
}

View File

@ -0,0 +1,37 @@
package upload
import (
"testing"
"kr.dev/diff"
)
func TestChunks(t *testing.T) {
const size = 101
const chunkSize = 10
var got []Chunk[int]
var lastN int
for n, c := range Chunks(size, chunkSize) {
if n != lastN+1 {
t.Errorf("n = %d; want %d", n, lastN+1)
}
got = append(got, c)
lastN = n
}
want := []Chunk[int]{
{0, 10},
{10, 10},
{20, 10},
{30, 10},
{40, 10},
{50, 10},
{60, 10},
{70, 10},
{80, 10},
{90, 10},
{100, 1},
}
diff.Test(t, t.Errorf, got, want)
}