many improvements with options and noop stuff

* add many options helpers
* fix noop client to allow publish messages to topic in broker
* fix noop server to allow registering in registry
* fix noop server to allow subscribe to topic in broker
* fix new service initialization

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
2020-10-16 09:38:57 +03:00
parent a59aae760f
commit 14c97d59c1
39 changed files with 1384 additions and 432 deletions

View File

@@ -2,49 +2,45 @@ package store
import "context"
type noopStore struct {
type NoopStore struct {
opts Options
}
func newStore(opts ...Option) Store {
options := NewOptions()
for _, o := range opts {
o(&options)
}
return &noopStore{opts: options}
}
func (n *noopStore) Init(ctx context.Context, opts ...Option) error {
func (n *NoopStore) Init(opts ...Option) error {
for _, o := range opts {
o(&n.opts)
}
return nil
}
func (n *noopStore) Options() Options {
func (n *NoopStore) Options() Options {
return n.opts
}
func (n *noopStore) String() string {
func (n *NoopStore) String() string {
return "noop"
}
func (n *noopStore) Read(ctx context.Context, key string, opts ...ReadOption) ([]*Record, error) {
func (n *NoopStore) Read(ctx context.Context, key string, opts ...ReadOption) ([]*Record, error) {
return []*Record{}, nil
}
func (n *noopStore) Write(ctx context.Context, r *Record, opts ...WriteOption) error {
func (n *NoopStore) Write(ctx context.Context, r *Record, opts ...WriteOption) error {
return nil
}
func (n *noopStore) Delete(ctx context.Context, key string, opts ...DeleteOption) error {
func (n *NoopStore) Delete(ctx context.Context, key string, opts ...DeleteOption) error {
return nil
}
func (n *noopStore) List(ctx context.Context, opts ...ListOption) ([]string, error) {
func (n *NoopStore) List(ctx context.Context, opts ...ListOption) ([]string, error) {
return []string{}, nil
}
func (n *noopStore) Close(ctx context.Context) error {
func (n *NoopStore) Connect(ctx context.Context) error {
return nil
}
func (n *NoopStore) Disconnect(ctx context.Context) error {
return nil
}

View File

@@ -37,6 +37,12 @@ func NewOptions(opts ...Option) Options {
// Option sets values in Options
type Option func(o *Options)
func Context(ctx context.Context) Option {
return func(o *Options) {
o.Context = ctx
}
}
// Logger sets the logger
func Logger(l logger.Logger) Option {
return func(o *Options) {

View File

@@ -11,13 +11,14 @@ import (
var (
// ErrNotFound is returned when a key doesn't exist
ErrNotFound = errors.New("not found")
DefaultStore Store = newStore()
DefaultStore Store = &NoopStore{opts: NewOptions()}
)
// Store is a data storage interface
type Store interface {
// Init initialises the store. It must perform any required setup on the backing storage implementation and check that it is ready for use, returning any errors.
Init(ctx context.Context, opts ...Option) error
Init(opts ...Option) error
Connect(ctx context.Context) error
// Options allows you to view the current options.
Options() Options
// Read takes a single key name and optional ReadOptions. It returns matching []*Record or an error.
@@ -28,8 +29,8 @@ type Store interface {
Delete(ctx context.Context, key string, opts ...DeleteOption) error
// List returns any keys that match, or an empty list with no error if none matched.
List(ctx context.Context, opts ...ListOption) ([]string, error)
// Close the store
Close(ctx context.Context) error
// Disconnect the store
Disconnect(ctx context.Context) error
// String returns the name of the implementation.
String() string
}