coreapi: block: move option logic to options package
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com> This commit was moved from ipfs/interface-go-ipfs-core@68340710253f13b5c5376a9f882608cb0ab17f8c This commit was moved from ipfs/boxo@571c6def44851832a3617c699f1b166d1c9dc959
Łukasz Magiera committed
Aug 15, 2018 at 14:01 UTC
b089e7cadcdb8cdf85874ea606bb64e7a1023487
1 file changed
+39
-5
core/coreiface/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) {