From 8616936ac8e6749be2eaec1764911b9ad88ad79b Mon Sep 17 00:00:00 2001 From: Laurent Senta Date: Wed, 12 Apr 2023 10:29:37 +0200 Subject: [PATCH] feat(routing): allow-offline with routing put --- coreiface/options/routing.go | 35 +++++++++++++++++++++++++++++++++++ coreiface/routing.go | 4 +++- 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 coreiface/options/routing.go diff --git a/coreiface/options/routing.go b/coreiface/options/routing.go new file mode 100644 index 0000000000..d66d44a0db --- /dev/null +++ b/coreiface/options/routing.go @@ -0,0 +1,35 @@ +package options + +type RoutingPutSettings struct { + AllowOffline bool +} + +type RoutingPutOption func(*RoutingPutSettings) error + +func RoutingPutOptions(opts ...RoutingPutOption) (*RoutingPutSettings, error) { + options := &RoutingPutSettings{ + AllowOffline: false, + } + + for _, opt := range opts { + err := opt(options) + if err != nil { + return nil, err + } + } + + return options, nil +} + +type putOpts struct{} + +var Put putOpts + +// AllowOffline is an option for Routing.Put which specifies whether to allow +// publishing when the node is offline. Default value is false +func (putOpts) AllowOffline(allow bool) RoutingPutOption { + return func(settings *RoutingPutSettings) error { + settings.AllowOffline = allow + return nil + } +} diff --git a/coreiface/routing.go b/coreiface/routing.go index a28ceb9e77..5099c3de07 100644 --- a/coreiface/routing.go +++ b/coreiface/routing.go @@ -2,6 +2,8 @@ package iface import ( "context" + + "github.com/ipfs/boxo/coreiface/options" ) // RoutingAPI specifies the interface to the routing layer. @@ -10,5 +12,5 @@ type RoutingAPI interface { Get(context.Context, string) ([]byte, error) // Put sets a value for a given key - Put(ctx context.Context, key string, value []byte) error + Put(ctx context.Context, key string, value []byte, opts ...options.RoutingPutOption) error }