Skip to content

Commit dc5d66c

Browse files
committed
coreapi: block: move option logic to options package
License: MIT Signed-off-by: Łukasz Magiera <[email protected]>
1 parent 301fcf8 commit dc5d66c

File tree

2 files changed

+40
-39
lines changed

2 files changed

+40
-39
lines changed

core/coreapi/block.go

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@ import (
44
"bytes"
55
"context"
66
"errors"
7-
"fmt"
87
"io"
98
"io/ioutil"
109

1110
util "github.com/ipfs/go-ipfs/blocks/blockstoreutil"
1211
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
1312
caopts "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
1413

15-
mh "gx/ipfs/QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8/go-multihash"
1614
blocks "gx/ipfs/QmZXvzTJTiN6p469osBUtEwm4WwhXXoWcHC8aTS1cAJkjy/go-block-format"
1715
cid "gx/ipfs/Qmdu2AYUV7yMoVBQPxXNfe7FJcdx16kYtsx6jAPKWQYF1y/go-cid"
1816
)
@@ -25,7 +23,7 @@ type BlockStat struct {
2523
}
2624

2725
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...)
2927
if err != nil {
3028
return nil, err
3129
}
@@ -35,37 +33,6 @@ func (api *BlockAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.Bloc
3533
return nil, err
3634
}
3735

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-
6936
bcid, err := pref.Sum(data)
7037
if err != nil {
7138
return nil, err

core/coreapi/interface/options/block.go

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package options
22

33
import (
4-
"gx/ipfs/QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8/go-multihash"
4+
"fmt"
5+
mh "gx/ipfs/QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8/go-multihash"
6+
cid "gx/ipfs/Qmdu2AYUV7yMoVBQPxXNfe7FJcdx16kYtsx6jAPKWQYF1y/go-cid"
57
)
68

79
type BlockPutSettings struct {
@@ -17,20 +19,52 @@ type BlockRmSettings struct {
1719
type BlockPutOption func(*BlockPutSettings) error
1820
type BlockRmOption func(*BlockRmSettings) error
1921

20-
func BlockPutOptions(opts ...BlockPutOption) (*BlockPutSettings, error) {
22+
func BlockPutOptions(opts ...BlockPutOption) (*BlockPutSettings, cid.Prefix, error) {
2123
options := &BlockPutSettings{
2224
Codec: "",
23-
MhType: multihash.SHA2_256,
25+
MhType: mh.SHA2_256,
2426
MhLength: -1,
2527
}
2628

2729
for _, opt := range opts {
2830
err := opt(options)
2931
if err != nil {
30-
return nil, err
32+
return nil, cid.Prefix{}, err
3133
}
3234
}
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
3468
}
3569

3670
func BlockRmOptions(opts ...BlockRmOption) (*BlockRmSettings, error) {

0 commit comments

Comments
 (0)