From 286785491c29db6a373b6ffccade7692a97f1cba Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Tue, 26 Jan 2021 02:08:22 +0300 Subject: [PATCH] store: improve interface Signed-off-by: Vasiliy Tolstov --- store/context.go | 50 ++++++++++++++++++++++++++++++++++++++++++++ store/noop.go | 2 +- store/options.go | 53 ++++++++++++++++++++++++++++++++++++++++------- store/store.go | 4 +++- util/sync/sync.go | 2 +- 5 files changed, 100 insertions(+), 11 deletions(-) diff --git a/store/context.go b/store/context.go index d048cad7..2cfcb62e 100644 --- a/store/context.go +++ b/store/context.go @@ -32,3 +32,53 @@ func SetOption(k, v interface{}) Option { o.Context = context.WithValue(o.Context, k, v) } } + +// SetReadOption returns a function to setup a context with given value +func SetReadOption(k, v interface{}) ReadOption { + return func(o *ReadOptions) { + if o.Context == nil { + o.Context = context.Background() + } + o.Context = context.WithValue(o.Context, k, v) + } +} + +// SetWriteOption returns a function to setup a context with given value +func SetWriteOption(k, v interface{}) WriteOption { + return func(o *WriteOptions) { + if o.Context == nil { + o.Context = context.Background() + } + o.Context = context.WithValue(o.Context, k, v) + } +} + +// SetListOption returns a function to setup a context with given value +func SetListOption(k, v interface{}) ListOption { + return func(o *ListOptions) { + if o.Context == nil { + o.Context = context.Background() + } + o.Context = context.WithValue(o.Context, k, v) + } +} + +// SetDeleteOption returns a function to setup a context with given value +func SetDeleteOption(k, v interface{}) DeleteOption { + return func(o *DeleteOptions) { + if o.Context == nil { + o.Context = context.Background() + } + o.Context = context.WithValue(o.Context, k, v) + } +} + +// SetExistsOption returns a function to setup a context with given value +func SetExistsOption(k, v interface{}) ExistsOption { + return func(o *ExistsOptions) { + if o.Context == nil { + o.Context = context.Background() + } + o.Context = context.WithValue(o.Context, k, v) + } +} diff --git a/store/noop.go b/store/noop.go index 711f3564..59767ae1 100644 --- a/store/noop.go +++ b/store/noop.go @@ -29,7 +29,7 @@ func (n *noopStore) String() string { } // Read reads store value by key -func (n *noopStore) Exists(ctx context.Context, key string) error { +func (n *noopStore) Exists(ctx context.Context, key string, opts ...ExistsOption) error { return ErrNotFound } diff --git a/store/options.go b/store/options.go index d40b554d..3cdf0f49 100644 --- a/store/options.go +++ b/store/options.go @@ -2,6 +2,7 @@ package store import ( "context" + "crypto/tls" "time" "github.com/unistack-org/micro/v3/codec" @@ -29,6 +30,9 @@ type Options struct { Meter meter.Meter // Tracer the tacer Tracer tracer.Tracer + // TLSConfig specifies tls.Config for secure + TLSConfig *tls.Config + // Context should contain all implementation specific options, using context.WithValue. Context context.Context } @@ -51,6 +55,13 @@ func NewOptions(opts ...Option) Options { // Option sets values in Options type Option func(o *Options) +// TLSConfig specifies a *tls.Config +func TLSConfig(t *tls.Config) Option { + return func(o *Options) { + o.TLSConfig = t + } +} + // Context pass context to store func Context(ctx context.Context) Option { return func(o *Options) { @@ -120,8 +131,10 @@ func NewReadOptions(opts ...ReadOption) ReadOptions { // ReadOptions configures an individual Read operation type ReadOptions struct { - Database string - Table string + Database string + Table string + Namespace string + Context context.Context } // ReadOption sets values in ReadOptions @@ -146,10 +159,12 @@ func NewWriteOptions(opts ...WriteOption) WriteOptions { // WriteOptions configures an individual Write operation type WriteOptions struct { - Database string - Table string - TTL time.Duration - Metadata metadata.Metadata + Database string + Table string + TTL time.Duration + Metadata metadata.Metadata + Namespace string + Context context.Context } // WriteOption sets values in WriteOptions @@ -188,7 +203,10 @@ func NewDeleteOptions(opts ...DeleteOption) DeleteOptions { // DeleteOptions configures an individual Delete operation type DeleteOptions struct { - Database, Table string + Database string + Table string + Namespace string + Context context.Context } // DeleteOption sets values in DeleteOptions @@ -222,7 +240,9 @@ type ListOptions struct { // Limit limits the number of returned keys Limit uint // Offset when combined with Limit supports pagination - Offset uint + Offset uint + Namespace string + Context context.Context } // ListOption sets values in ListOptions @@ -263,3 +283,20 @@ func ListOffset(o uint) ListOption { l.Offset = o } } + +type ExistsOption func(*ExistsOptions) + +type ExistsOptions struct { + Namespace string + Context context.Context +} + +func NewExistsOptions(opts ...ExistsOption) ExistsOptions { + options := ExistsOptions{ + Context: context.Background(), + } + for _, o := range opts { + o(&options) + } + return options +} diff --git a/store/store.go b/store/store.go index 91a984f8..ad526974 100644 --- a/store/store.go +++ b/store/store.go @@ -12,6 +12,8 @@ import ( var ( // ErrNotFound is returned when a key doesn't exist ErrNotFound = errors.New("not found") + // ErrInvalidKey is returned when a key has empty or have invalid format + ErrInvalidKey = errors.New("invalid key") // DefaultStore is the global default store DefaultStore Store = NewStore() ) @@ -25,7 +27,7 @@ type Store interface { // Options allows you to view the current options. Options() Options // Exists check that key exists in store - Exists(ctx context.Context, key string) error + Exists(ctx context.Context, key string, opts ...ExistsOption) error // Read reads a single key name to provided value with optional ReadOptions Read(ctx context.Context, key string, val interface{}, opts ...ReadOption) error // Write writes a value to key name to the store with optional WriteOption diff --git a/util/sync/sync.go b/util/sync/sync.go index ec20b95c..25779a8e 100644 --- a/util/sync/sync.go +++ b/util/sync/sync.go @@ -96,7 +96,7 @@ func (c *syncStore) List(ctx context.Context, opts ...store.ListOption) ([]strin return c.syncOpts.Stores[0].List(ctx, opts...) } -func (c *syncStore) Exists(ctx context.Context, key string) error { +func (c *syncStore) Exists(ctx context.Context, key string, opts ...store.ExistsOption) error { return c.syncOpts.Stores[0].Exists(ctx, key) }