coreapi: block: move option logic to options package
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
Łukasz Magiera committed
Aug 15, 2018 at 14:01 UTC
1f98f4b99ce3b8bfd291858cf3d12ef0dc48381f
2 files changed
+40
-39
core/coreapi/block.go
+1
-34
@@ -4,7 +4,6 @@ import (
4
"bytes"
5
"context"
6
"errors"
7
- "fmt"
7
"io"
8
"io/ioutil"
9
@@ -12,7 +11,6 @@ import (
11
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
12
caopts "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
13
15
- mh "gx/ipfs/QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8/go-multihash"
14
blocks "gx/ipfs/QmWAzSEoqZ6xU6pu8yL8e5WaMb7wtbfbhhN4p1DknUPtr3/go-block-format"
15
cid "gx/ipfs/QmZFbDTY9jfSBms2MchvYM9oYRbAF19K7Pby47yDBfpPrb/go-cid"
16
)
@@ -25,7 +23,7 @@ type BlockStat struct {
23
}
24
25
func (api *BlockAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.BlockPutOption) (coreiface.BlockStat, error) {
28
- settings, err := caopts.BlockPutOptions(opts...)
26
+ _, pref, err := caopts.BlockPutOptions(opts...)
27
if err != nil {
28
return nil, err
29
}
@@ -35,37 +33,6 @@ func (api *BlockAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.Bloc
33
return nil, err
34
}
35
38
- var pref cid.Prefix
39
- pref.Version = 1
40
-
41
- if settings.Codec == "" {
42
- if settings.MhType != mh.SHA2_256 || (settings.MhLength != -1 && settings.MhLength != 32) {
43
- settings.Codec = "protobuf"
44
- } else {
45
- settings.Codec = "v0"
46
- }
47
- }
48
-
49
- formatval, ok := cid.Codecs[settings.Codec]
50
- if !ok {
51
- return nil, fmt.Errorf("unrecognized format: %s", settings.Codec)
52
- }
53
-
54
- if settings.Codec == "v0" && settings.MhType == mh.SHA2_256 {
55
- pref.Version = 0
56
- }
57
-
58
- if settings.Codec == "v0" {
59
- if settings.MhType != mh.SHA2_256 || (settings.MhLength != -1 && settings.MhLength != 32) {
60
- return nil, fmt.Errorf("only sha2-255-32 is allowed with CIDv0")
61
- }
62
- }
63
-
64
- pref.Codec = formatval
65
-
66
- pref.MhType = settings.MhType
67
- pref.MhLength = settings.MhLength
68
-
36
bcid, err := pref.Sum(data)
37
if err != nil {
38
return nil, err
core/coreapi/interface/options/block.go
+39
-5
@@ -1,7 +1,9 @@
1
package options
2
3
import (
4
- "gx/ipfs/QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8/go-multihash"
4
+ "fmt"
5
+ mh "gx/ipfs/QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8/go-multihash"
6
+ cid "gx/ipfs/QmZFbDTY9jfSBms2MchvYM9oYRbAF19K7Pby47yDBfpPrb/go-cid"
7
)
8
9
type BlockPutSettings struct {
@@ -17,20 +19,52 @@ type BlockRmSettings struct {
19
type BlockPutOption func(*BlockPutSettings) error
20
type BlockRmOption func(*BlockRmSettings) error
21
20
-func BlockPutOptions(opts ...BlockPutOption) (*BlockPutSettings, error) {
22
+func BlockPutOptions(opts ...BlockPutOption) (*BlockPutSettings, cid.Prefix, error) {
23
options := &BlockPutSettings{
24
Codec: "",
23
- MhType: multihash.SHA2_256,
25
+ MhType: mh.SHA2_256,
26
MhLength: -1,
27
}
28
29
for _, opt := range opts {
30
err := opt(options)
31
if err != nil {
30
- return nil, err
32
+ return nil, cid.Prefix{}, err
33
}
34
}
33
- return options, nil
35
+
36
+ var pref cid.Prefix
37
+ pref.Version = 1
38
+
39
+ if options.Codec == "" {
40
+ if options.MhType != mh.SHA2_256 || (options.MhLength != -1 && options.MhLength != 32) {
41
+ options.Codec = "protobuf"
42
+ } else {
43
+ options.Codec = "v0"
44
+ }
45
+ }
46
+
47
+ if options.Codec == "v0" && options.MhType == mh.SHA2_256 {
48
+ pref.Version = 0
49
+ }
50
+
51
+ formatval, ok := cid.Codecs[options.Codec]
52
+ if !ok {
53
+ return nil, cid.Prefix{}, fmt.Errorf("unrecognized format: %s", options.Codec)
54
+ }
55
+
56
+ if options.Codec == "v0" {
57
+ if options.MhType != mh.SHA2_256 || (options.MhLength != -1 && options.MhLength != 32) {
58
+ return nil, cid.Prefix{}, fmt.Errorf("only sha2-255-32 is allowed with CIDv0")
59
+ }
60
+ }
61
+
62
+ pref.Codec = formatval
63
+
64
+ pref.MhType = options.MhType
65
+ pref.MhLength = options.MhLength
66
+
67
+ return options, pref, nil
68
}
69
70
func BlockRmOptions(opts ...BlockRmOption) (*BlockRmSettings, error) {