block.go 1.44 KB
Newer Older
Łukasz Magiera's avatar
Łukasz Magiera committed
1 2
package options

Łukasz Magiera's avatar
Łukasz Magiera committed
3 4 5 6 7
import (
	//cid "gx/ipfs/QmeSrf6pzut73u6zLQkRFQ3ygt3k6XFT2kjdYP8Tnkwwyg/go-cid"
	"gx/ipfs/QmYeKnKpubCMRiq3PGZcTREErthbb5Q9cXsCoSkD9bjEBd/go-multihash"
)

Łukasz Magiera's avatar
Łukasz Magiera committed
8
type BlockPutSettings struct {
Łukasz Magiera's avatar
Łukasz Magiera committed
9
	Codec    string
Łukasz Magiera's avatar
Łukasz Magiera committed
10 11 12 13 14 15 16 17 18 19
	MhType   uint64
	MhLength int
}

type BlockRmSettings struct {
	Force bool
}

type BlockPutOption func(*BlockPutSettings) error
type BlockRmOption func(*BlockRmSettings) error
Łukasz Magiera's avatar
Łukasz Magiera committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73

func BlockPutOptions(opts ...BlockPutOption) (*BlockPutSettings, error) {
	options := &BlockPutSettings{
		Codec:    "v0",
		MhType:   multihash.SHA2_256,
		MhLength: -1,
	}

	for _, opt := range opts {
		err := opt(options)
		if err != nil {
			return nil, err
		}
	}
	return options, nil
}

func BlockRmOptions(opts ...BlockRmOption) (*BlockRmSettings, error) {
	options := &BlockRmSettings{
		Force: false,
	}

	for _, opt := range opts {
		err := opt(options)
		if err != nil {
			return nil, err
		}
	}
	return options, nil
}

type BlockOptions struct{}

func (api *BlockOptions) WithFormat(codec string) BlockPutOption {
	return func(settings *BlockPutSettings) error {
		settings.Codec = codec
		return nil
	}
}

func (api *BlockOptions) WithHash(mhType uint64, mhLen int) BlockPutOption {
	return func(settings *BlockPutSettings) error {
		settings.MhType = mhType
		settings.MhLength = mhLen
		return nil
	}
}

func (api *BlockOptions) WithForce(force bool) BlockRmOption {
	return func(settings *BlockRmSettings) error {
		settings.Force = force
		return nil
	}
}