coreapi: implement block API

License: MIT
Signed-off-by: Łukasz Magiera <magik6k@gmail.com>


This commit was moved from ipfs/interface-go-ipfs-core@a7509ebfca

This commit was moved from ipfs/boxo@bfcc5b4d08
This commit is contained in:
Łukasz Magiera 2018-01-06 16:57:41 +01:00
parent c561185676
commit 6b6fdcde60
2 changed files with 67 additions and 6 deletions

View File

@ -62,6 +62,8 @@ type BlockStat interface {
type CoreAPI interface {
// Unixfs returns an implementation of Unixfs API.
Unixfs() UnixfsAPI
// Block returns an implementation of Block API.
Block() BlockAPI
// Dag returns an implementation of Dag API.
Dag() DagAPI
// Name returns an implementation of Name API.
@ -93,16 +95,16 @@ type UnixfsAPI interface {
}
type BlockAPI interface {
Put(context.Context, io.Reader) (Path, error)
WithCodec(codec uint64) options.BlockPutOption
Put(context.Context, io.Reader, ...options.BlockPutOption) (Path, error)
WithFormat(codec string) 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
Stat(context.Context) (BlockStat, error)
Stat(context.Context, Path) (BlockStat, error)
}
// DagAPI specifies the interface to IPLD

View File

@ -1,7 +1,12 @@
package options
import (
//cid "gx/ipfs/QmeSrf6pzut73u6zLQkRFQ3ygt3k6XFT2kjdYP8Tnkwwyg/go-cid"
"gx/ipfs/QmYeKnKpubCMRiq3PGZcTREErthbb5Q9cXsCoSkD9bjEBd/go-multihash"
)
type BlockPutSettings struct {
Codec uint64
Codec string
MhType uint64
MhLength int
}
@ -12,3 +17,57 @@ type BlockRmSettings struct {
type BlockPutOption func(*BlockPutSettings) 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
}
}