dag.go 2.12 KB
Newer Older
1 2 3 4 5
package options

import (
	"math"

Steven Allen's avatar
Steven Allen committed
6
	cid "gx/ipfs/QmR8BauakNcBa3RbE4nbQu76PDiJgoQgz8AJdhJuiU4TAw/go-cid"
7 8 9 10 11 12 13 14 15 16 17 18 19 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
)

type DagPutSettings struct {
	InputEnc string
	Codec    uint64
	MhType   uint64
	MhLength int
}

type DagTreeSettings struct {
	Depth int
}

type DagPutOption func(*DagPutSettings) error
type DagTreeOption func(*DagTreeSettings) error

func DagPutOptions(opts ...DagPutOption) (*DagPutSettings, error) {
	options := &DagPutSettings{
		InputEnc: "json",
		Codec:    cid.DagCBOR,
		MhType:   math.MaxUint64,
		MhLength: -1,
	}

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

func DagTreeOptions(opts ...DagTreeOption) (*DagTreeSettings, error) {
	options := &DagTreeSettings{
		Depth: -1,
	}

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

54
type dagOpts struct{}
55

56 57 58 59
var Dag dagOpts

// InputEnc is an option for Dag.Put which specifies the input encoding of the
// data. Default is "json", most formats/codecs support "raw"
60
func (dagOpts) InputEnc(enc string) DagPutOption {
61 62 63 64 65 66
	return func(settings *DagPutSettings) error {
		settings.InputEnc = enc
		return nil
	}
}

67 68
// Codec is an option for Dag.Put which specifies the multicodec to use to
// serialize the object. Default is cid.DagCBOR (0x71)
69
func (dagOpts) Codec(codec uint64) DagPutOption {
70 71 72 73 74 75
	return func(settings *DagPutSettings) error {
		settings.Codec = codec
		return nil
	}
}

76 77 78 79
// Hash is an option for Dag.Put which specifies the multihash settings to use
// when hashing the object. Default is based on the codec used
// (mh.SHA2_256 (0x12) for DagCBOR). If mhLen is set to -1, default length for
// the hash will be used
80
func (dagOpts) Hash(mhType uint64, mhLen int) DagPutOption {
81 82 83 84 85 86 87
	return func(settings *DagPutSettings) error {
		settings.MhType = mhType
		settings.MhLength = mhLen
		return nil
	}
}

88 89
// Depth is an option for Dag.Tree which specifies maximum depth of the
// returned tree. Default is -1 (no depth limit)
90
func (dagOpts) Depth(depth int) DagTreeOption {
91 92 93 94 95
	return func(settings *DagTreeSettings) error {
		settings.Depth = depth
		return nil
	}
}