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

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

Łukasz Magiera's avatar
Łukasz Magiera committed
7
type BlockPutSettings struct {
Łukasz Magiera's avatar
Łukasz Magiera committed
8
	Codec    string
Łukasz Magiera's avatar
Łukasz Magiera committed
9 10 11 12 13 14 15 16 17 18
	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
19 20 21

func BlockPutOptions(opts ...BlockPutOption) (*BlockPutSettings, error) {
	options := &BlockPutSettings{
22
		Codec:    "",
Łukasz Magiera's avatar
Łukasz Magiera committed
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
		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
}

50
type blockOpts struct{}
Łukasz Magiera's avatar
Łukasz Magiera committed
51

52 53 54 55
var Block blockOpts

// Format is an option for Block.Put which specifies the multicodec to use to
// serialize the object. Default is "v0"
56
func (blockOpts) Format(codec string) BlockPutOption {
Łukasz Magiera's avatar
Łukasz Magiera committed
57 58 59 60 61 62
	return func(settings *BlockPutSettings) error {
		settings.Codec = codec
		return nil
	}
}

63 64 65
// Hash is an option for Block.Put which specifies the multihash settings to use
// when hashing the object. Default is mh.SHA2_256 (0x12).
// If mhLen is set to -1, default length for the hash will be used
66
func (blockOpts) Hash(mhType uint64, mhLen int) BlockPutOption {
Łukasz Magiera's avatar
Łukasz Magiera committed
67 68 69 70 71 72 73
	return func(settings *BlockPutSettings) error {
		settings.MhType = mhType
		settings.MhLength = mhLen
		return nil
	}
}

74 75
// Force is an option for Block.Rm which, when set to true, will ignore
// non-existing blocks
76
func (blockOpts) Force(force bool) BlockRmOption {
Łukasz Magiera's avatar
Łukasz Magiera committed
77 78 79 80 81
	return func(settings *BlockRmSettings) error {
		settings.Force = force
		return nil
	}
}