utils/upload: init
This commit is contained in:
parent
618eb5b909
commit
c95f97689b
27
utils/upload/upload.go
Normal file
27
utils/upload/upload.go
Normal 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)})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
37
utils/upload/upload_test.go
Normal file
37
utils/upload/upload_test.go
Normal 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)
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user