store: improve interface
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
parent
263ea8910d
commit
286785491c
@ -32,3 +32,53 @@ func SetOption(k, v interface{}) Option {
|
|||||||
o.Context = context.WithValue(o.Context, k, v)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -29,7 +29,7 @@ func (n *noopStore) String() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Read reads store value by key
|
// 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
|
return ErrNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,6 +2,7 @@ package store
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"crypto/tls"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/unistack-org/micro/v3/codec"
|
"github.com/unistack-org/micro/v3/codec"
|
||||||
@ -29,6 +30,9 @@ type Options struct {
|
|||||||
Meter meter.Meter
|
Meter meter.Meter
|
||||||
// Tracer the tacer
|
// Tracer the tacer
|
||||||
Tracer tracer.Tracer
|
Tracer tracer.Tracer
|
||||||
|
// TLSConfig specifies tls.Config for secure
|
||||||
|
TLSConfig *tls.Config
|
||||||
|
|
||||||
// Context should contain all implementation specific options, using context.WithValue.
|
// Context should contain all implementation specific options, using context.WithValue.
|
||||||
Context context.Context
|
Context context.Context
|
||||||
}
|
}
|
||||||
@ -51,6 +55,13 @@ func NewOptions(opts ...Option) Options {
|
|||||||
// Option sets values in Options
|
// Option sets values in Options
|
||||||
type Option func(o *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
|
// Context pass context to store
|
||||||
func Context(ctx context.Context) Option {
|
func Context(ctx context.Context) Option {
|
||||||
return func(o *Options) {
|
return func(o *Options) {
|
||||||
@ -122,6 +133,8 @@ func NewReadOptions(opts ...ReadOption) ReadOptions {
|
|||||||
type ReadOptions struct {
|
type ReadOptions struct {
|
||||||
Database string
|
Database string
|
||||||
Table string
|
Table string
|
||||||
|
Namespace string
|
||||||
|
Context context.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReadOption sets values in ReadOptions
|
// ReadOption sets values in ReadOptions
|
||||||
@ -150,6 +163,8 @@ type WriteOptions struct {
|
|||||||
Table string
|
Table string
|
||||||
TTL time.Duration
|
TTL time.Duration
|
||||||
Metadata metadata.Metadata
|
Metadata metadata.Metadata
|
||||||
|
Namespace string
|
||||||
|
Context context.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
// WriteOption sets values in WriteOptions
|
// WriteOption sets values in WriteOptions
|
||||||
@ -188,7 +203,10 @@ func NewDeleteOptions(opts ...DeleteOption) DeleteOptions {
|
|||||||
|
|
||||||
// DeleteOptions configures an individual Delete operation
|
// DeleteOptions configures an individual Delete operation
|
||||||
type DeleteOptions struct {
|
type DeleteOptions struct {
|
||||||
Database, Table string
|
Database string
|
||||||
|
Table string
|
||||||
|
Namespace string
|
||||||
|
Context context.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteOption sets values in DeleteOptions
|
// DeleteOption sets values in DeleteOptions
|
||||||
@ -223,6 +241,8 @@ type ListOptions struct {
|
|||||||
Limit uint
|
Limit uint
|
||||||
// Offset when combined with Limit supports pagination
|
// Offset when combined with Limit supports pagination
|
||||||
Offset uint
|
Offset uint
|
||||||
|
Namespace string
|
||||||
|
Context context.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListOption sets values in ListOptions
|
// ListOption sets values in ListOptions
|
||||||
@ -263,3 +283,20 @@ func ListOffset(o uint) ListOption {
|
|||||||
l.Offset = o
|
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
|
||||||
|
}
|
||||||
|
@ -12,6 +12,8 @@ import (
|
|||||||
var (
|
var (
|
||||||
// ErrNotFound is returned when a key doesn't exist
|
// ErrNotFound is returned when a key doesn't exist
|
||||||
ErrNotFound = errors.New("not found")
|
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 is the global default store
|
||||||
DefaultStore Store = NewStore()
|
DefaultStore Store = NewStore()
|
||||||
)
|
)
|
||||||
@ -25,7 +27,7 @@ type Store interface {
|
|||||||
// Options allows you to view the current options.
|
// Options allows you to view the current options.
|
||||||
Options() Options
|
Options() Options
|
||||||
// Exists check that key exists in store
|
// 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 reads a single key name to provided value with optional ReadOptions
|
||||||
Read(ctx context.Context, key string, val interface{}, opts ...ReadOption) error
|
Read(ctx context.Context, key string, val interface{}, opts ...ReadOption) error
|
||||||
// Write writes a value to key name to the store with optional WriteOption
|
// Write writes a value to key name to the store with optional WriteOption
|
||||||
|
@ -96,7 +96,7 @@ func (c *syncStore) List(ctx context.Context, opts ...store.ListOption) ([]strin
|
|||||||
return c.syncOpts.Stores[0].List(ctx, opts...)
|
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)
|
return c.syncOpts.Stores[0].Exists(ctx, key)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user