Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

coreapi: Refactor options #4807

Merged
merged 3 commits into from
Mar 29, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions core/coreapi/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ import (
blocks "gx/ipfs/Qmej7nf81hi2x2tvjRBF3mcp74sQyuDH4VMYDGd1YtXjb2/go-block-format"
)

type BlockAPI struct {
*CoreAPI
*caopts.BlockOptions
}
type BlockAPI CoreAPI

type BlockStat struct {
path coreiface.Path
Expand Down
10 changes: 6 additions & 4 deletions core/coreapi/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"strings"
"testing"

opt "github.com/ipfs/go-ipfs/core/coreapi/interface/options"

mh "gx/ipfs/QmZyZDi491cCNTLfAhwcaDii2Kg4pwKRkhqQzURGDvY6ua/go-multihash"
)

Expand Down Expand Up @@ -33,7 +35,7 @@ func TestBlockPutFormat(t *testing.T) {
t.Error(err)
}

res, err := api.Block().Put(ctx, strings.NewReader(`Hello`), api.Block().WithFormat("cbor"))
res, err := api.Block().Put(ctx, strings.NewReader(`Hello`), opt.Block.Format("cbor"))
if err != nil {
t.Error(err)
}
Expand All @@ -50,7 +52,7 @@ func TestBlockPutHash(t *testing.T) {
t.Error(err)
}

res, err := api.Block().Put(ctx, strings.NewReader(`Hello`), api.Block().WithHash(mh.KECCAK_512, -1))
res, err := api.Block().Put(ctx, strings.NewReader(`Hello`), opt.Block.Hash(mh.KECCAK_512, -1))
if err != nil {
t.Error(err)
}
Expand All @@ -67,7 +69,7 @@ func TestBlockGet(t *testing.T) {
t.Error(err)
}

res, err := api.Block().Put(ctx, strings.NewReader(`Hello`), api.Block().WithHash(mh.KECCAK_512, -1))
res, err := api.Block().Put(ctx, strings.NewReader(`Hello`), opt.Block.Hash(mh.KECCAK_512, -1))
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -134,7 +136,7 @@ func TestBlockRm(t *testing.T) {
t.Errorf("unexpected error; %s", err.Error())
}

err = api.Block().Rm(ctx, res, api.Block().WithForce(true))
err = api.Block().Rm(ctx, res, opt.Block.Force(true))
if err != nil {
t.Error(err)
}
Expand Down
12 changes: 6 additions & 6 deletions core/coreapi/coreapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,32 +31,32 @@ func (api *CoreAPI) Unixfs() coreiface.UnixfsAPI {

// Block returns the BlockAPI interface implementation backed by the go-ipfs node
func (api *CoreAPI) Block() coreiface.BlockAPI {
return &BlockAPI{api, nil}
return (*BlockAPI)(api)
}

// Dag returns the DagAPI interface implementation backed by the go-ipfs node
func (api *CoreAPI) Dag() coreiface.DagAPI {
return &DagAPI{api, nil}
return (*DagAPI)(api)
}

// Name returns the NameAPI interface implementation backed by the go-ipfs node
func (api *CoreAPI) Name() coreiface.NameAPI {
return &NameAPI{api, nil}
return (*NameAPI)(api)
}

// Key returns the KeyAPI interface implementation backed by the go-ipfs node
func (api *CoreAPI) Key() coreiface.KeyAPI {
return &KeyAPI{api, nil}
return (*KeyAPI)(api)
}

//Object returns the ObjectAPI interface implementation backed by the go-ipfs node
func (api *CoreAPI) Object() coreiface.ObjectAPI {
return &ObjectAPI{api, nil}
return (*ObjectAPI)(api)
}

// Pin returns the PinAPI interface implementation backed by the go-ipfs node
func (api *CoreAPI) Pin() coreiface.PinAPI {
return &PinAPI{api, nil}
return (*PinAPI)(api)
}

// ResolveNode resolves the path `p` using Unixfx resolver, gets and returns the
Expand Down
7 changes: 2 additions & 5 deletions core/coreapi/dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ import (
ipld "gx/ipfs/Qme5bWv7wtjUNGsK2BNGVUFPKiuxWrsqrtvYwCLRw8YFES/go-ipld-format"
)

type DagAPI struct {
*CoreAPI
*caopts.DagOptions
}
type DagAPI CoreAPI

// Put inserts data using specified format and input encoding. Unless used with
// `WithCodes` or `WithHash`, the defaults "dag-cbor" and "sha256" are used.
Expand Down Expand Up @@ -79,5 +76,5 @@ func (api *DagAPI) Tree(ctx context.Context, p coreiface.Path, opts ...caopts.Da
}

func (api *DagAPI) core() coreiface.CoreAPI {
return api.CoreAPI
return (*CoreAPI)(api)
}
4 changes: 3 additions & 1 deletion core/coreapi/dag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
coreapi "github.com/ipfs/go-ipfs/core/coreapi"

mh "gx/ipfs/QmZyZDi491cCNTLfAhwcaDii2Kg4pwKRkhqQzURGDvY6ua/go-multihash"

opt "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
)

var (
Expand Down Expand Up @@ -45,7 +47,7 @@ func TestPutWithHash(t *testing.T) {
t.Error(err)
}

res, err := api.Dag().Put(ctx, strings.NewReader(`"Hello"`), api.Dag().WithHash(mh.ID, -1))
res, err := api.Dag().Put(ctx, strings.NewReader(`"Hello"`), opt.Dag.Hash(mh.ID, -1))
if err != nil {
t.Error(err)
}
Expand Down
13 changes: 0 additions & 13 deletions core/coreapi/interface/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,6 @@ type BlockAPI interface {
// Put imports raw block data, hashing it using specified settings.
Put(context.Context, io.Reader, ...options.BlockPutOption) (Path, error)

// WithFormat is an option for Put which specifies the multicodec to use to
// serialize the object. Default is "v0"
WithFormat(codec string) options.BlockPutOption

// WithHash is an option for Put which specifies the multihash settings to use
// when hashing the object. Default is mh.SHA2_256 (0x12).
// If mhLen is set to -1, default length for the hash will be used
WithHash(mhType uint64, mhLen int) options.BlockPutOption

// Get attempts to resolve the path and return a reader for data in the block
Get(context.Context, Path) (io.Reader, error)

Expand All @@ -40,10 +31,6 @@ type BlockAPI interface {
// will be returned
Rm(context.Context, Path, ...options.BlockRmOption) error

// WithForce is an option for Rm which, when set to true, will ignore
// non-existing blocks
WithForce(force bool) options.BlockRmOption

// Stat returns information on
Stat(context.Context, Path) (BlockStat, error)
}
18 changes: 0 additions & 18 deletions core/coreapi/interface/dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,9 @@ type DagAPI interface {
// "sha256" are used.
Put(ctx context.Context, src io.Reader, opts ...options.DagPutOption) (Path, error)

// WithInputEnc is an option for Put which specifies the input encoding of the
// data. Default is "json", most formats/codecs support "raw"
WithInputEnc(enc string) options.DagPutOption

// WithCodec is an option for Put which specifies the multicodec to use to
// serialize the object. Default is cid.DagCBOR (0x71)
WithCodec(codec uint64) options.DagPutOption

// WithHash is an option for Put which specifies the multihash settings to use
// when hashing the object. Default is based on the codec used
// (mh.SHA2_256 (0x12) for DagCBOR). If mhLen is set to -1, default length for
// the hash will be used
WithHash(mhType uint64, mhLen int) options.DagPutOption

// Get attempts to resolve and get the node specified by the path
Get(ctx context.Context, path Path) (ipld.Node, error)

// Tree returns list of paths within a node specified by the path.
Tree(ctx context.Context, path Path, opts ...options.DagTreeOption) ([]Path, error)

// WithDepth is an option for Tree which specifies maximum depth of the
// returned tree. Default is -1 (no depth limit)
WithDepth(depth int) options.DagTreeOption
}
19 changes: 0 additions & 19 deletions core/coreapi/interface/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,10 @@ type KeyAPI interface {
// name and returns a base58 encoded multihash of it's public key
Generate(ctx context.Context, name string, opts ...options.KeyGenerateOption) (Key, error)

// WithType is an option for Generate which specifies which algorithm
// should be used for the key. Default is options.RSAKey
//
// Supported key types:
// * options.RSAKey
// * options.Ed25519Key
WithType(algorithm string) options.KeyGenerateOption

// WithSize is an option for Generate which specifies the size of the key to
// generated. Default is -1
//
// value of -1 means 'use default size for key type':
// * 2048 for RSA
WithSize(size int) options.KeyGenerateOption

// Rename renames oldName key to newName. Returns the key and whether another
// key was overwritten, or an error
Rename(ctx context.Context, oldName string, newName string, opts ...options.KeyRenameOption) (Key, bool, error)

// WithForce is an option for Rename which specifies whether to allow to
// replace existing keys.
WithForce(force bool) options.KeyRenameOption

// List lists keys stored in keystore
List(ctx context.Context) ([]Key, error)

Expand Down
24 changes: 0 additions & 24 deletions core/coreapi/interface/name.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package iface

import (
"context"
"time"

options "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
)
Expand All @@ -27,29 +26,6 @@ type NameAPI interface {
// Publish announces new IPNS name
Publish(ctx context.Context, path Path, opts ...options.NamePublishOption) (IpnsEntry, error)

// WithValidTime is an option for Publish which specifies for how long the
// entry will remain valid. Default value is 24h
WithValidTime(validTime time.Duration) options.NamePublishOption

// WithKey is an option for Publish which specifies the key to use for
// publishing. Default value is "self" which is the node's own PeerID.
// The key parameter must be either PeerID or keystore key alias.
//
// You can use KeyAPI to list and generate more names and their respective keys.
WithKey(key string) options.NamePublishOption

// Resolve attempts to resolve the newest version of the specified name
Resolve(ctx context.Context, name string, opts ...options.NameResolveOption) (Path, error)

// WithRecursive is an option for Resolve which specifies whether to perform a
// recursive lookup. Default value is false
WithRecursive(recursive bool) options.NameResolveOption

// WithLocal is an option for Resolve which specifies if the lookup should be
// offline. Default value is false
WithLocal(local bool) options.NameResolveOption

// WithCache is an option for Resolve which specifies if cache should be used.
// Default value is true
WithCache(cache bool) options.NameResolveOption
}
28 changes: 0 additions & 28 deletions core/coreapi/interface/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,33 +37,9 @@ type ObjectAPI interface {
// New creates new, empty (by default) dag-node.
New(context.Context, ...options.ObjectNewOption) (ipld.Node, error)

// WithType is an option for New which allows to change the type of created
// dag node.
//
// Supported types:
// * 'empty' - Empty node
// * 'unixfs-dir' - Empty UnixFS directory
WithType(string) options.ObjectNewOption

// Put imports the data into merkledag
Put(context.Context, io.Reader, ...options.ObjectPutOption) (Path, error)

// WithInputEnc is an option for Put which specifies the input encoding of the
// data. Default is "json".
//
// Supported encodings:
// * "protobuf"
// * "json"
WithInputEnc(e string) options.ObjectPutOption

// WithDataType specifies the encoding of data field when using Josn or XML
// input encoding.
//
// Supported types:
// * "text" (default)
// * "base64"
WithDataType(t string) options.ObjectPutOption

// Get returns the node for the path
Get(context.Context, Path) (ipld.Node, error)

Expand All @@ -81,10 +57,6 @@ type ObjectAPI interface {
// with WithCreate option).
AddLink(ctx context.Context, base Path, name string, child Path, opts ...options.ObjectAddLinkOption) (Path, error)

// WithCreate is an option for AddLink which specifies whether create required
// directories for the child
WithCreate(create bool) options.ObjectAddLinkOption

// RmLink removes a link from the node
RmLink(ctx context.Context, base Path, link string) (Path, error)

Expand Down
17 changes: 13 additions & 4 deletions core/coreapi/interface/options/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,33 @@ func BlockRmOptions(opts ...BlockRmOption) (*BlockRmSettings, error) {
return options, nil
}

type BlockOptions struct{}
type blockOpts struct{}

func (api *BlockOptions) WithFormat(codec string) BlockPutOption {
var Block blockOpts

// Format is an option for Block.Put which specifies the multicodec to use to
// serialize the object. Default is "v0"
func (_ blockOpts) Format(codec string) BlockPutOption {
return func(settings *BlockPutSettings) error {
settings.Codec = codec
return nil
}
}

func (api *BlockOptions) WithHash(mhType uint64, mhLen int) BlockPutOption {
// Hash is an option for Block.Put which specifies the multihash settings to use
// when hashing the object. Default is mh.SHA2_256 (0x12).
// If mhLen is set to -1, default length for the hash will be used
func (_ blockOpts) Hash(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 {
// Force is an option for Block.Rm which, when set to true, will ignore
// non-existing blocks
func (_ blockOpts) Force(force bool) BlockRmOption {
return func(settings *BlockRmSettings) error {
settings.Force = force
return nil
Expand Down
22 changes: 17 additions & 5 deletions core/coreapi/interface/options/dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,31 +51,43 @@ func DagTreeOptions(opts ...DagTreeOption) (*DagTreeSettings, error) {
return options, nil
}

type DagOptions struct{}
type dagOpts struct{}

func (api *DagOptions) WithInputEnc(enc string) DagPutOption {
var Dag dagOpts

// InputEnc is an option for Dag.Put which specifies the input encoding of the
// data. Default is "json", most formats/codecs support "raw"
func (_ dagOpts) InputEnc(enc string) DagPutOption {
return func(settings *DagPutSettings) error {
settings.InputEnc = enc
return nil
}
}

func (api *DagOptions) WithCodec(codec uint64) DagPutOption {
// Codec is an option for Dag.Put which specifies the multicodec to use to
// serialize the object. Default is cid.DagCBOR (0x71)
func (_ dagOpts) Codec(codec uint64) DagPutOption {
return func(settings *DagPutSettings) error {
settings.Codec = codec
return nil
}
}

func (api *DagOptions) WithHash(mhType uint64, mhLen int) DagPutOption {
// Hash is an option for Dag.Put which specifies the multihash settings to use
// when hashing the object. Default is based on the codec used
// (mh.SHA2_256 (0x12) for DagCBOR). If mhLen is set to -1, default length for
// the hash will be used
func (_ dagOpts) Hash(mhType uint64, mhLen int) DagPutOption {
return func(settings *DagPutSettings) error {
settings.MhType = mhType
settings.MhLength = mhLen
return nil
}
}

func (api *DagOptions) WithDepth(depth int) DagTreeOption {
// Depth is an option for Dag.Tree which specifies maximum depth of the
// returned tree. Default is -1 (no depth limit)
func (_ dagOpts) Depth(depth int) DagTreeOption {
return func(settings *DagTreeSettings) error {
settings.Depth = depth
return nil
Expand Down
Loading