@cryptotaxi247 / kubo / commits / e6a16b57e

coreapi: functional options for DagAPI

License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com> This commit was moved from ipfs/interface-go-ipfs-core@39f8afada852b5189996725ccde10b6d64f1b5ab This commit was moved from ipfs/boxo@9901857d2d284da55025a64aacf72d6ac9c89b1f

Łukasz Magiera committed Dec 17, 2017 at 03:23 UTC e6a16b57e63b33e1d8ed7f2edb079e1c96527873
2 files changed +105 -3
core/coreiface/interface.go
+22 -3
@@ -7,6 +7,8 @@ import (
7 "errors"
8 "io"
9
10 + options "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
11 +
12 ipld "gx/ipfs/QmNwUEK7QbwSqyKBu3mMtToo8SUc6wQJ7gdZq4gGGJqfnf/go-ipld-format"
13 cid "gx/ipfs/QmeSrf6pzut73u6zLQkRFQ3ygt3k6XFT2kjdYP8Tnkwwyg/go-cid"
14 )
@@ -60,14 +62,31 @@ type UnixfsAPI interface {
62 type DagAPI interface {
63 // Put inserts data using specified format and input encoding.
64 // If format is not specified (nil), default dag-cbor/sha256 is used
63 - Put(ctx context.Context, src io.Reader, inputEnc string, format *cid.Prefix) ([]Node, error) //TODO: make format optional
65 + Put(ctx context.Context, src io.Reader, opts ...options.DagPutOption) ([]Node, error)
66 +
67 + // WithInputEnc is an option for Put which specifies the input encoding of the
68 + // data. Default is "json", most formats/codecs support "raw"
69 + WithInputEnc(enc string) options.DagPutOption
70 +
71 + // WithCodec is an option for Put which specifies the multicodec to use to
72 + // serialize the object. Default is cid.DagCBOR (0x71)
73 + WithCodec(codec uint64) options.DagPutOption
74 +
75 + // WithHash is an option for Put which specifies the multihash settings to use
76 + // when hashing the object. Default is based on the codec used
77 + // (mh.SHA2_256 (0x12) for DagCBOR). If mhLen is set to -1, default length for
78 + // the hash will be used
79 + WithHash(mhType uint64, mhLen int) options.DagPutOption
80
81 // Get attempts to resolve and get the node specified by the path
82 Get(ctx context.Context, path Path) (Node, error)
83
84 // Tree returns list of paths within a node specified by the path.
69 - // To get all paths in a tree, set depth to -1
70 - Tree(ctx context.Context, path Path, depth int) ([]Path, error)
85 + Tree(ctx context.Context, path Path, opts ...options.DagTreeOption) ([]Path, error)
86 +
87 + // WithDepth is an option for Tree which specifies maximum depth of the
88 + // returned tree. Default is -1 (no depth limit)
89 + WithDepth(depth int) options.DagTreeOption
90 }
91
92 // type ObjectAPI interface {
core/coreiface/options/dag.go new
+83
@@ -0,0 +1,83 @@
1 +package options
2 +
3 +import (
4 + "math"
5 +
6 + cid "gx/ipfs/QmeSrf6pzut73u6zLQkRFQ3ygt3k6XFT2kjdYP8Tnkwwyg/go-cid"
7 +)
8 +
9 +type DagPutSettings struct {
10 + InputEnc string
11 + Codec uint64
12 + MhType uint64
13 + MhLength int
14 +}
15 +
16 +type DagTreeSettings struct {
17 + Depth int
18 +}
19 +
20 +type DagPutOption func(*DagPutSettings) error
21 +type DagTreeOption func(*DagTreeSettings) error
22 +
23 +func DagPutOptions(opts ...DagPutOption) (*DagPutSettings, error) {
24 + options := &DagPutSettings{
25 + InputEnc: "json",
26 + Codec: cid.DagCBOR,
27 + MhType: math.MaxUint64,
28 + MhLength: -1,
29 + }
30 +
31 + for _, opt := range opts {
32 + err := opt(options)
33 + if err != nil {
34 + return nil, err
35 + }
36 + }
37 + return options, nil
38 +}
39 +
40 +func DagTreeOptions(opts ...DagTreeOption) (*DagTreeSettings, error) {
41 + options := &DagTreeSettings{
42 + Depth: -1,
43 + }
44 +
45 + for _, opt := range opts {
46 + err := opt(options)
47 + if err != nil {
48 + return nil, err
49 + }
50 + }
51 + return options, nil
52 +}
53 +
54 +type DagOptions struct{}
55 +
56 +func (api *DagOptions) WithInputEnc(enc string) DagPutOption {
57 + return func(settings *DagPutSettings) error {
58 + settings.InputEnc = enc
59 + return nil
60 + }
61 +}
62 +
63 +func (api *DagOptions) WithCodec(codec uint64) DagPutOption {
64 + return func(settings *DagPutSettings) error {
65 + settings.Codec = codec
66 + return nil
67 + }
68 +}
69 +
70 +func (api *DagOptions) WithHash(mhType uint64, mhLen int) DagPutOption {
71 + return func(settings *DagPutSettings) error {
72 + settings.MhType = mhType
73 + settings.MhLength = mhLen
74 + return nil
75 + }
76 +}
77 +
78 +func (api *DagOptions) WithDepth(depth int) DagTreeOption {
79 + return func(settings *DagTreeSettings) error {
80 + settings.Depth = depth
81 + return nil
82 + }
83 +}