forked from ipfs/kubo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dag.go
95 lines (80 loc) · 2.12 KB
/
dag.go
1
2
3
4
5
6
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package options
import (
"math"
cid "gx/ipfs/QmcZfnkapfECQGcLZaf9B79NRg7cRa9EnZh4LSbkCzwNvY/go-cid"
)
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
}
type dagOpts struct{}
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"
func (dagOpts) InputEnc(enc string) DagPutOption {
return func(settings *DagPutSettings) error {
settings.InputEnc = enc
return nil
}
}
// Codec is an option for Dag.Put which specifies the multicodec to use to
// serialize the object. Default is cid.DagCBOR (0x71)
func (dagOpts) Codec(codec uint64) DagPutOption {
return func(settings *DagPutSettings) error {
settings.Codec = codec
return nil
}
}
// 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
func (dagOpts) Hash(mhType uint64, mhLen int) DagPutOption {
return func(settings *DagPutSettings) error {
settings.MhType = mhType
settings.MhLength = mhLen
return nil
}
}
// Depth is an option for Dag.Tree which specifies maximum depth of the
// returned tree. Default is -1 (no depth limit)
func (dagOpts) Depth(depth int) DagTreeOption {
return func(settings *DagTreeSettings) error {
settings.Depth = depth
return nil
}
}