block_generator.go 952 Bytes
Newer Older
1 2
// Package blocksutil provides utility functions for working
// with Blocks.
3 4
package blocksutil

tavit ohanian's avatar
tavit ohanian committed
5 6 7 8 9
import (
	"fmt"

	blocks "gitlab.dms3.io/dms3/public/go-block-format"
)
10

11 12
// NewBlockGenerator returns an object capable of
// producing blocks.
13 14 15 16
func NewBlockGenerator() BlockGenerator {
	return BlockGenerator{}
}

17 18 19 20
// BlockGenerator generates BasicBlocks on demand.
// For each instace of BlockGenerator,
// each new block is different from the previous,
// although two different instances will produce the same.
21 22 23 24
type BlockGenerator struct {
	seq int
}

25
// Next generates a new BasicBlock.
26
func (bg *BlockGenerator) Next() *blocks.BasicBlock {
27
	bg.seq++
tavit ohanian's avatar
tavit ohanian committed
28
	return blocks.NewBlock([]byte(fmt.Sprint(bg.seq)))
29 30
}

31
// Blocks generates as many BasicBlocks as specified by n.
Jeromy's avatar
Jeromy committed
32 33
func (bg *BlockGenerator) Blocks(n int) []blocks.Block {
	blocks := make([]blocks.Block, 0, n)
34 35 36 37 38 39
	for i := 0; i < n; i++ {
		b := bg.Next()
		blocks = append(blocks, b)
	}
	return blocks
}