Commit a7509ebf authored by Łukasz Magiera's avatar Łukasz Magiera

coreapi: implement block API

License: MIT
Signed-off-by: default avatarŁukasz Magiera <magik6k@gmail.com>
parent 3cd2995b
...@@ -62,6 +62,8 @@ type BlockStat interface { ...@@ -62,6 +62,8 @@ type BlockStat interface {
type CoreAPI interface { type CoreAPI interface {
// Unixfs returns an implementation of Unixfs API. // Unixfs returns an implementation of Unixfs API.
Unixfs() UnixfsAPI Unixfs() UnixfsAPI
// Block returns an implementation of Block API.
Block() BlockAPI
// Dag returns an implementation of Dag API. // Dag returns an implementation of Dag API.
Dag() DagAPI Dag() DagAPI
// Name returns an implementation of Name API. // Name returns an implementation of Name API.
...@@ -93,16 +95,16 @@ type UnixfsAPI interface { ...@@ -93,16 +95,16 @@ type UnixfsAPI interface {
} }
type BlockAPI interface { type BlockAPI interface {
Put(context.Context, io.Reader) (Path, error) Put(context.Context, io.Reader, ...options.BlockPutOption) (Path, error)
WithCodec(codec uint64) options.BlockPutOption WithFormat(codec string) options.BlockPutOption
WithHash(mhType uint64, mhLen int) options.BlockPutOption WithHash(mhType uint64, mhLen int) options.BlockPutOption
Get(context.Context) (io.Reader, error) Get(context.Context, Path) (io.Reader, error)
Rm(context.Context) error Rm(context.Context, Path, ...options.BlockRmOption) error
WithForce(force bool) options.BlockRmOption WithForce(force bool) options.BlockRmOption
Stat(context.Context) (BlockStat, error) Stat(context.Context, Path) (BlockStat, error)
} }
// DagAPI specifies the interface to IPLD // DagAPI specifies the interface to IPLD
......
package options package options
import (
//cid "gx/ipfs/QmeSrf6pzut73u6zLQkRFQ3ygt3k6XFT2kjdYP8Tnkwwyg/go-cid"
"gx/ipfs/QmYeKnKpubCMRiq3PGZcTREErthbb5Q9cXsCoSkD9bjEBd/go-multihash"
)
type BlockPutSettings struct { type BlockPutSettings struct {
Codec uint64 Codec string
MhType uint64 MhType uint64
MhLength int MhLength int
} }
...@@ -12,3 +17,57 @@ type BlockRmSettings struct { ...@@ -12,3 +17,57 @@ type BlockRmSettings struct {
type BlockPutOption func(*BlockPutSettings) error type BlockPutOption func(*BlockPutSettings) error
type BlockRmOption func(*BlockRmSettings) error type BlockRmOption func(*BlockRmSettings) error
func BlockPutOptions(opts ...BlockPutOption) (*BlockPutSettings, error) {
options := &BlockPutSettings{
Codec: "v0",
MhType: multihash.SHA2_256,
MhLength: -1,
}
for _, opt := range opts {
err := opt(options)
if err != nil {
return nil, err
}
}
return options, nil
}
func BlockRmOptions(opts ...BlockRmOption) (*BlockRmSettings, error) {
options := &BlockRmSettings{
Force: false,
}
for _, opt := range opts {
err := opt(options)
if err != nil {
return nil, err
}
}
return options, nil
}
type BlockOptions struct{}
func (api *BlockOptions) WithFormat(codec string) BlockPutOption {
return func(settings *BlockPutSettings) error {
settings.Codec = codec
return nil
}
}
func (api *BlockOptions) WithHash(mhType uint64, mhLen int) BlockPutOption {
return func(settings *BlockPutSettings) error {
settings.MhType = mhType
settings.MhLength = mhLen
return nil
}
}
func (api *BlockOptions) WithForce(force bool) BlockRmOption {
return func(settings *BlockRmSettings) error {
settings.Force = force
return nil
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment