store: improve interface

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2021-01-26 02:08:22 +03:00
parent 263ea8910d
commit 286785491c
5 changed files with 100 additions and 11 deletions

View File

@ -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)
}
}

View File

@ -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
}

View File

@ -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) {
@ -122,6 +133,8 @@ func NewReadOptions(opts ...ReadOption) ReadOptions {
type ReadOptions struct {
Database string
Table string
Namespace string
Context context.Context
}
// ReadOption sets values in ReadOptions
@ -150,6 +163,8 @@ type WriteOptions struct {
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
@ -223,6 +241,8 @@ type ListOptions struct {
Limit uint
// Offset when combined with Limit supports pagination
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
}

View File

@ -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

View File

@ -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)
}