From 5f75628c244056bac464d6440f8c93f3658d3308 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Fri, 30 Mar 2018 09:34:49 +0200 Subject: [PATCH] coreapi/dht: refactor options MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit License: MIT Signed-off-by: Ɓukasz Magiera --- core/coreapi/coreapi.go | 2 +- core/coreapi/dht.go | 11 ++++++----- core/coreapi/dht_test.go | 7 ++++--- core/coreapi/interface/dht.go | 8 -------- core/coreapi/interface/options/dht.go | 12 +++++++++--- 5 files changed, 20 insertions(+), 20 deletions(-) diff --git a/core/coreapi/coreapi.go b/core/coreapi/coreapi.go index 344e0659015..c03342efd51 100644 --- a/core/coreapi/coreapi.go +++ b/core/coreapi/coreapi.go @@ -61,7 +61,7 @@ func (api *CoreAPI) Pin() coreiface.PinAPI { // Dht returns the DhtAPI interface implementation backed by the go-ipfs node func (api *CoreAPI) Dht() coreiface.DhtAPI { - return &DhtAPI{api, nil} + return (*DhtAPI)(api) } // ResolveNode resolves the path `p` using Unixfx resolver, gets and returns the diff --git a/core/coreapi/dht.go b/core/coreapi/dht.go index e7900569d5f..a27628b4229 100644 --- a/core/coreapi/dht.go +++ b/core/coreapi/dht.go @@ -21,10 +21,7 @@ import ( var ErrNotDHT = errors.New("routing service is not a DHT") -type DhtAPI struct { - *CoreAPI - *caopts.DhtOptions -} +type DhtAPI CoreAPI func (api *DhtAPI) FindPeer(ctx context.Context, p peer.ID) (<-chan ma.Multiaddr, error) { dht, ok := api.node.Routing.(*ipdht.IpfsDHT) @@ -93,7 +90,7 @@ func (api *DhtAPI) FindProviders(ctx context.Context, p coreiface.Path, opts ... return nil, ErrNotDHT } - p, err = api.ResolvePath(ctx, p) + p, err = api.core().ResolvePath(ctx, p) if err != nil { return nil, err } @@ -238,3 +235,7 @@ func provideKeysRec(ctx context.Context, r routing.IpfsRouting, dserv ipld.DAGSe return nil } + +func (api *DhtAPI) core() coreiface.CoreAPI { + return (*CoreAPI)(api) +} diff --git a/core/coreapi/dht_test.go b/core/coreapi/dht_test.go index ed2ee542f08..6a9c96649c4 100644 --- a/core/coreapi/dht_test.go +++ b/core/coreapi/dht_test.go @@ -7,6 +7,7 @@ import ( "testing" coreapi "github.com/ipfs/go-ipfs/core/coreapi" + options "github.com/ipfs/go-ipfs/core/coreapi/interface/options" peer "gx/ipfs/QmZoWKhxUmZ2seW4BzX6fJkNR8hh9PsGModr7q171yq2SS/go-libp2p-peer" blocks "gx/ipfs/Qmej7nf81hi2x2tvjRBF3mcp74sQyuDH4VMYDGd1YtXjb2/go-block-format" @@ -54,7 +55,7 @@ func TestDhtFindProviders(t *testing.T) { t.Fatal(err) } - out, err := apis[2].Dht().FindProviders(ctx, p, apis[2].Dht().WithNumProviders(1)) + out, err := apis[2].Dht().FindProviders(ctx, p, options.Dht.NumProviders(1)) if err != nil { t.Fatal(err) } @@ -83,7 +84,7 @@ func TestDhtProvide(t *testing.T) { nds[0].Blockstore.Put(b) p := coreapi.ParseCid(b.Cid()) - out, err := apis[2].Dht().FindProviders(ctx, p, apis[2].Dht().WithNumProviders(1)) + out, err := apis[2].Dht().FindProviders(ctx, p, options.Dht.NumProviders(1)) if err != nil { t.Fatal(err) } @@ -99,7 +100,7 @@ func TestDhtProvide(t *testing.T) { t.Fatal(err) } - out, err = apis[2].Dht().FindProviders(ctx, p, apis[2].Dht().WithNumProviders(1)) + out, err = apis[2].Dht().FindProviders(ctx, p, options.Dht.NumProviders(1)) if err != nil { t.Fatal(err) } diff --git a/core/coreapi/interface/dht.go b/core/coreapi/interface/dht.go index ce8509e01a8..c49903bee76 100644 --- a/core/coreapi/interface/dht.go +++ b/core/coreapi/interface/dht.go @@ -19,14 +19,6 @@ type DhtAPI interface { // given a key. FindProviders(context.Context, Path, ...options.DhtFindProvidersOption) (<-chan peer.ID, error) //TODO: is path the right choice here? - // WithNumProviders is an option for FindProviders which specifies the - // number of peers to look for. Default is 20 - WithNumProviders(numProviders int) options.DhtFindProvidersOption - // Provide announces to the network that you are providing given values Provide(context.Context, Path, ...options.DhtProvideOption) error - - // WithRecursive is an option for Provide which specifies whether to provide - // the given path recursively - WithRecursive(recursive bool) options.DhtProvideOption } diff --git a/core/coreapi/interface/options/dht.go b/core/coreapi/interface/options/dht.go index 3867e32c075..6674a30a583 100644 --- a/core/coreapi/interface/options/dht.go +++ b/core/coreapi/interface/options/dht.go @@ -39,16 +39,22 @@ func DhtFindProvidersOptions(opts ...DhtFindProvidersOption) (*DhtFindProvidersS return options, nil } -type DhtOptions struct{} +type dhtOpts struct{} -func (api *DhtOptions) WithRecursive(recursive bool) DhtProvideOption { +var Dht dhtOpts + +// Recursive is an option for Provide which specifies whether to provide +// the given path recursively +func (dhtOpts) Recursive(recursive bool) DhtProvideOption { return func(settings *DhtProvideSettings) error { settings.Recursive = recursive return nil } } -func (api *DhtOptions) WithNumProviders(numProviders int) DhtFindProvidersOption { +// Providers is an option for Dht.FindProviders which specifies the +// number of peers to look for. Default is 20 +func (dhtOpts) NumProviders(numProviders int) DhtFindProvidersOption { return func(settings *DhtFindProvidersSettings) error { settings.NumProviders = numProviders return nil