@cryptotaxi247 / kubo / commits / 6b6fdcde6

coreapi: implement block API

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

Łukasz Magiera committed Jan 6, 2018 at 16:57 UTC 6b6fdcde60d8198b343e0449533a3fa3e306cff0
2 files changed +67 -6
core/coreiface/interface.go
+7 -5
@@ -62,6 +62,8 @@ type BlockStat interface {
62 type CoreAPI interface {
63 // Unixfs returns an implementation of Unixfs API.
64 Unixfs() UnixfsAPI
65 + // Block returns an implementation of Block API.
66 + Block() BlockAPI
67 // Dag returns an implementation of Dag API.
68 Dag() DagAPI
69 // Name returns an implementation of Name API.
@@ -93,16 +95,16 @@ type UnixfsAPI interface {
95 }
96
97 type BlockAPI interface {
96 - Put(context.Context, io.Reader) (Path, error)
97 - WithCodec(codec uint64) options.BlockPutOption
98 + Put(context.Context, io.Reader, ...options.BlockPutOption) (Path, error)
99 + WithFormat(codec string) options.BlockPutOption
100 WithHash(mhType uint64, mhLen int) options.BlockPutOption
101
100 - Get(context.Context) (io.Reader, error)
102 + Get(context.Context, Path) (io.Reader, error)
103
102 - Rm(context.Context) error
104 + Rm(context.Context, Path, ...options.BlockRmOption) error
105 WithForce(force bool) options.BlockRmOption
106
105 - Stat(context.Context) (BlockStat, error)
107 + Stat(context.Context, Path) (BlockStat, error)
108 }
109
110 // DagAPI specifies the interface to IPLD
core/coreiface/options/block.go
+60 -1
@@ -1,7 +1,12 @@
1 package options
2
3 +import (
4 + //cid "gx/ipfs/QmeSrf6pzut73u6zLQkRFQ3ygt3k6XFT2kjdYP8Tnkwwyg/go-cid"
5 + "gx/ipfs/QmYeKnKpubCMRiq3PGZcTREErthbb5Q9cXsCoSkD9bjEBd/go-multihash"
6 +)
7 +
8 type BlockPutSettings struct {
4 - Codec uint64
9 + Codec string
10 MhType uint64
11 MhLength int
12 }
@@ -12,3 +17,57 @@ type BlockRmSettings struct {
17
18 type BlockPutOption func(*BlockPutSettings) error
19 type BlockRmOption func(*BlockRmSettings) error
20 +
21 +func BlockPutOptions(opts ...BlockPutOption) (*BlockPutSettings, error) {
22 + options := &BlockPutSettings{
23 + Codec: "v0",
24 + MhType: multihash.SHA2_256,
25 + MhLength: -1,
26 + }
27 +
28 + for _, opt := range opts {
29 + err := opt(options)
30 + if err != nil {
31 + return nil, err
32 + }
33 + }
34 + return options, nil
35 +}
36 +
37 +func BlockRmOptions(opts ...BlockRmOption) (*BlockRmSettings, error) {
38 + options := &BlockRmSettings{
39 + Force: false,
40 + }
41 +
42 + for _, opt := range opts {
43 + err := opt(options)
44 + if err != nil {
45 + return nil, err
46 + }
47 + }
48 + return options, nil
49 +}
50 +
51 +type BlockOptions struct{}
52 +
53 +func (api *BlockOptions) WithFormat(codec string) BlockPutOption {
54 + return func(settings *BlockPutSettings) error {
55 + settings.Codec = codec
56 + return nil
57 + }
58 +}
59 +
60 +func (api *BlockOptions) WithHash(mhType uint64, mhLen int) BlockPutOption {
61 + return func(settings *BlockPutSettings) error {
62 + settings.MhType = mhType
63 + settings.MhLength = mhLen
64 + return nil
65 + }
66 +}
67 +
68 +func (api *BlockOptions) WithForce(force bool) BlockRmOption {
69 + return func(settings *BlockRmSettings) error {
70 + settings.Force = force
71 + return nil
72 + }
73 +}