move options to dedicated package
Some checks failed
lint / lint (pull_request) Failing after 1m31s
pr / test (pull_request) Failing after 2m37s

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
2023-07-29 00:40:58 +03:00
parent b1dbd99ce2
commit 6f6f850af6
84 changed files with 1154 additions and 4521 deletions

View File

@@ -22,63 +22,3 @@ func NewContext(ctx context.Context, c Store) context.Context {
}
return context.WithValue(ctx, storeKey{}, c)
}
// SetOption returns a function to setup a context with given value
func SetOption(k, v interface{}) Option {
return func(o *Options) {
if o.Context == nil {
o.Context = context.Background()
}
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

@@ -3,6 +3,8 @@ package store
import (
"context"
"testing"
"go.unistack.org/micro/v4/options"
)
func TestFromNilContext(t *testing.T) {
@@ -43,10 +45,7 @@ func TestNewContext(t *testing.T) {
func TestSetOption(t *testing.T) {
type key struct{}
o := SetOption(key{}, "test")
opts := &Options{}
o(opts)
opts := NewOptions(options.ContextOption(key{}, "test"))
if v, ok := opts.Context.Value(key{}).(string); !ok || v == "" {
t.Fatal("SetOption not works")
}

View File

@@ -7,10 +7,11 @@ import (
"time"
"github.com/patrickmn/go-cache"
"go.unistack.org/micro/v4/options"
)
// NewStore returns a memory store
func NewStore(opts ...Option) Store {
func NewStore(opts ...options.Option) Store {
return &memoryStore{
opts: NewOptions(opts...),
store: cache.New(cache.NoExpiration, 5*time.Minute),
@@ -100,7 +101,7 @@ func (m *memoryStore) list(prefix string, limit, offset uint) []string {
return allKeys
}
func (m *memoryStore) Init(opts ...Option) error {
func (m *memoryStore) Init(opts ...options.Option) error {
for _, o := range opts {
o(&m.opts)
}
@@ -115,7 +116,7 @@ func (m *memoryStore) Name() string {
return m.opts.Name
}
func (m *memoryStore) Exists(ctx context.Context, key string, opts ...ExistsOption) error {
func (m *memoryStore) Exists(ctx context.Context, key string, opts ...options.Option) error {
options := NewExistsOptions(opts...)
if options.Namespace == "" {
options.Namespace = m.opts.Namespace
@@ -123,7 +124,7 @@ func (m *memoryStore) Exists(ctx context.Context, key string, opts ...ExistsOpti
return m.exists(options.Namespace, key)
}
func (m *memoryStore) Read(ctx context.Context, key string, val interface{}, opts ...ReadOption) error {
func (m *memoryStore) Read(ctx context.Context, key string, val interface{}, opts ...options.Option) error {
options := NewReadOptions(opts...)
if options.Namespace == "" {
options.Namespace = m.opts.Namespace
@@ -131,7 +132,7 @@ func (m *memoryStore) Read(ctx context.Context, key string, val interface{}, opt
return m.get(options.Namespace, key, val)
}
func (m *memoryStore) Write(ctx context.Context, key string, val interface{}, opts ...WriteOption) error {
func (m *memoryStore) Write(ctx context.Context, key string, val interface{}, opts ...options.Option) error {
options := NewWriteOptions(opts...)
if options.Namespace == "" {
options.Namespace = m.opts.Namespace
@@ -151,7 +152,7 @@ func (m *memoryStore) Write(ctx context.Context, key string, val interface{}, op
return nil
}
func (m *memoryStore) Delete(ctx context.Context, key string, opts ...DeleteOption) error {
func (m *memoryStore) Delete(ctx context.Context, key string, opts ...options.Option) error {
options := NewDeleteOptions(opts...)
if options.Namespace == "" {
options.Namespace = m.opts.Namespace
@@ -165,7 +166,7 @@ func (m *memoryStore) Options() Options {
return m.opts
}
func (m *memoryStore) List(ctx context.Context, opts ...ListOption) ([]string, error) {
func (m *memoryStore) List(ctx context.Context, opts ...options.Option) ([]string, error) {
options := NewListOptions(opts...)
if options.Namespace == "" {
options.Namespace = m.opts.Namespace

View File

@@ -9,6 +9,7 @@ import (
"go.unistack.org/micro/v4/logger"
"go.unistack.org/micro/v4/metadata"
"go.unistack.org/micro/v4/meter"
"go.unistack.org/micro/v4/options"
"go.unistack.org/micro/v4/tracer"
)
@@ -32,16 +33,14 @@ type Options struct {
Namespace string
// Separator used as key parts separator
Separator string
// Addrs contains store address
Addrs []string
// Wrappers store wrapper that called before actual functions
// Wrappers []Wrapper
// Address contains store address
Address []string
// Timeout specifies timeout duration for all operations
Timeout time.Duration
}
// NewOptions creates options struct
func NewOptions(opts ...Option) Options {
func NewOptions(opts ...options.Option) Options {
options := Options{
Logger: logger.DefaultLogger,
Context: context.Background(),
@@ -56,85 +55,17 @@ func NewOptions(opts ...Option) Options {
return 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) {
o.Context = ctx
}
}
// Codec sets the codec
func Codec(c codec.Codec) Option {
return func(o *Options) {
o.Codec = c
}
}
// Logger sets the logger
func Logger(l logger.Logger) Option {
return func(o *Options) {
o.Logger = l
}
}
// Meter sets the meter
func Meter(m meter.Meter) Option {
return func(o *Options) {
o.Meter = m
}
}
// Name the name of the store
func Name(n string) Option {
return func(o *Options) {
o.Name = n
}
}
// Separator the value used as key parts separator
func Separator(s string) Option {
return func(o *Options) {
o.Separator = s
}
}
// Namespace sets namespace of the store
func Namespace(ns string) Option {
return func(o *Options) {
o.Namespace = ns
}
}
// Tracer sets the tracer
func Tracer(t tracer.Tracer) Option {
return func(o *Options) {
o.Tracer = t
func Separator(s string) options.Option {
return func(src interface{}) error {
return options.Set(src, s, "Separator")
}
}
// Timeout sets the timeout
func Timeout(td time.Duration) Option {
return func(o *Options) {
o.Timeout = td
}
}
// Addrs contains the addresses or other connection information of the backing storage.
// For example, an etcd implementation would contain the nodes of the cluster.
// A SQL implementation could contain one or more connection strings.
func Addrs(addrs ...string) Option {
return func(o *Options) {
o.Addrs = addrs
func Timeout(td time.Duration) options.Option {
return func(src interface{}) error {
return options.Set(src, td, ".Timeout")
}
}
@@ -147,7 +78,7 @@ type ReadOptions struct {
}
// NewReadOptions fills ReadOptions struct with opts slice
func NewReadOptions(opts ...ReadOption) ReadOptions {
func NewReadOptions(opts ...options.Option) ReadOptions {
options := ReadOptions{}
for _, o := range opts {
o(&options)
@@ -155,23 +86,6 @@ func NewReadOptions(opts ...ReadOption) ReadOptions {
return options
}
// ReadOption sets values in ReadOptions
type ReadOption func(r *ReadOptions)
// ReadContext pass context.Context to ReadOptions
func ReadContext(ctx context.Context) ReadOption {
return func(o *ReadOptions) {
o.Context = ctx
}
}
// ReadNamespace pass namespace to ReadOptions
func ReadNamespace(ns string) ReadOption {
return func(o *ReadOptions) {
o.Namespace = ns
}
}
// WriteOptions configures an individual Write operation
type WriteOptions struct {
// Context holds external options
@@ -185,7 +99,7 @@ type WriteOptions struct {
}
// NewWriteOptions fills WriteOptions struct with opts slice
func NewWriteOptions(opts ...WriteOption) WriteOptions {
func NewWriteOptions(opts ...options.Option) WriteOptions {
options := WriteOptions{}
for _, o := range opts {
o(&options)
@@ -193,34 +107,17 @@ func NewWriteOptions(opts ...WriteOption) WriteOptions {
return options
}
// WriteOption sets values in WriteOptions
type WriteOption func(w *WriteOptions)
// WriteContext pass context.Context to wirte options
func WriteContext(ctx context.Context) WriteOption {
return func(o *WriteOptions) {
o.Context = ctx
}
}
// WriteMetadata add metadata.Metadata
func WriteMetadata(md metadata.Metadata) WriteOption {
return func(o *WriteOptions) {
o.Metadata = metadata.Copy(md)
func WriteMetadata(md metadata.Metadata) options.Option {
return func(src interface{}) error {
return options.Set(src, metadata.Copy(md), ".Metadata")
}
}
// WriteTTL is the time the record expires
func WriteTTL(d time.Duration) WriteOption {
return func(o *WriteOptions) {
o.TTL = d
}
}
// WriteNamespace pass namespace to write options
func WriteNamespace(ns string) WriteOption {
return func(o *WriteOptions) {
o.Namespace = ns
func WriteTTL(td time.Duration) options.Option {
return func(src interface{}) error {
return options.Set(src, td, ".TTL")
}
}
@@ -233,7 +130,7 @@ type DeleteOptions struct {
}
// NewDeleteOptions fills DeleteOptions struct with opts slice
func NewDeleteOptions(opts ...DeleteOption) DeleteOptions {
func NewDeleteOptions(opts ...options.Option) DeleteOptions {
options := DeleteOptions{}
for _, o := range opts {
o(&options)
@@ -241,23 +138,6 @@ func NewDeleteOptions(opts ...DeleteOption) DeleteOptions {
return options
}
// DeleteOption sets values in DeleteOptions
type DeleteOption func(d *DeleteOptions)
// DeleteContext pass context.Context to delete options
func DeleteContext(ctx context.Context) DeleteOption {
return func(o *DeleteOptions) {
o.Context = ctx
}
}
// DeleteNamespace pass namespace to delete options
func DeleteNamespace(ns string) DeleteOption {
return func(o *DeleteOptions) {
o.Namespace = ns
}
}
// ListOptions configures an individual List operation
type ListOptions struct {
Context context.Context
@@ -269,7 +149,7 @@ type ListOptions struct {
}
// NewListOptions fills ListOptions struct with opts slice
func NewListOptions(opts ...ListOption) ListOptions {
func NewListOptions(opts ...options.Option) ListOptions {
options := ListOptions{}
for _, o := range opts {
o(&options)
@@ -277,48 +157,31 @@ func NewListOptions(opts ...ListOption) ListOptions {
return options
}
// ListOption sets values in ListOptions
type ListOption func(l *ListOptions)
// ListContext pass context.Context to list options
func ListContext(ctx context.Context) ListOption {
return func(o *ListOptions) {
o.Context = ctx
}
}
// ListPrefix returns all keys that are prefixed with key
func ListPrefix(s string) ListOption {
return func(o *ListOptions) {
o.Prefix = s
func ListPrefix(s string) options.Option {
return func(src interface{}) error {
return options.Set(src, s, ".Prefix")
}
}
// ListSuffix returns all keys that end with key
func ListSuffix(s string) ListOption {
return func(o *ListOptions) {
o.Suffix = s
func ListSuffix(s string) options.Option {
return func(src interface{}) error {
return options.Set(src, s, ".Prefix")
}
}
// ListLimit limits the number of returned keys
func ListLimit(n uint) ListOption {
return func(o *ListOptions) {
o.Limit = n
func ListLimit(n uint) options.Option {
return func(src interface{}) error {
return options.Set(src, n, ".Limit")
}
}
// ListOffset use with Limit for pagination
func ListOffset(n uint) ListOption {
return func(o *ListOptions) {
o.Offset = n
}
}
// ListNamespace pass namespace to list options
func ListNamespace(ns string) ListOption {
return func(o *ListOptions) {
o.Namespace = ns
func ListOffset(n uint) options.Option {
return func(src interface{}) error {
return options.Set(src, n, ".Offset")
}
}
@@ -330,11 +193,8 @@ type ExistsOptions struct {
Namespace string
}
// ExistsOption specifies Exists call options
type ExistsOption func(*ExistsOptions)
// NewExistsOptions helper for Exists method
func NewExistsOptions(opts ...ExistsOption) ExistsOptions {
func NewExistsOptions(opts ...options.Option) ExistsOptions {
options := ExistsOptions{
Context: context.Background(),
}
@@ -343,26 +203,3 @@ func NewExistsOptions(opts ...ExistsOption) ExistsOptions {
}
return options
}
// ExistsContext pass context.Context to exist options
func ExistsContext(ctx context.Context) ExistsOption {
return func(o *ExistsOptions) {
o.Context = ctx
}
}
// ExistsNamespace pass namespace to exist options
func ExistsNamespace(ns string) ExistsOption {
return func(o *ExistsOptions) {
o.Namespace = ns
}
}
/*
// WrapStore adds a store Wrapper to a list of options passed into the store
func WrapStore(w Wrapper) Option {
return func(o *Options) {
o.Wrappers = append(o.Wrappers, w)
}
}
*/

View File

@@ -4,6 +4,8 @@ package store // import "go.unistack.org/micro/v4/store"
import (
"context"
"errors"
"go.unistack.org/micro/v4/options"
)
var (
@@ -21,21 +23,21 @@ var (
type Store interface {
Name() string
// Init initialises the store
Init(opts ...Option) error
Init(opts ...options.Option) error
// Connect is used when store needs to be connected
Connect(ctx context.Context) error
// Options allows you to view the current options.
Options() Options
// Exists check that key exists in store
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
Write(ctx context.Context, key string, val interface{}, opts ...WriteOption) error
// Delete removes the record with the corresponding key from the store.
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)
Exists(ctx context.Context, key string, opts ...options.Option) error
// Read reads a single key name to provided value with optional options
Read(ctx context.Context, key string, val interface{}, opts ...options.Option) error
// Write writes a value to key name to the store with optional options
Write(ctx context.Context, key string, val interface{}, opts ...options.Option) error
// Delete removes the record with the corresponding key from the store with optional options
Delete(ctx context.Context, key string, opts ...options.Option) error
// List returns any keys that match, or an empty list with no error if none matched with optional options
List(ctx context.Context, opts ...options.Option) ([]string, error)
// Disconnect the store
Disconnect(ctx context.Context) error
// String returns the name of the implementation.

View File

@@ -2,14 +2,9 @@ package store
import (
"context"
)
// LogfFunc function used for Logf method
// type LogfFunc func(ctx context.Context, level Level, msg string, args ...interface{})
// type Wrapper interface {
// Logf logs message with needed level
// Logf(LogfFunc) LogfFunc
// }
"go.unistack.org/micro/v4/options"
)
// NamespaceStore wrap store with namespace
type NamespaceStore struct {
@@ -23,7 +18,7 @@ func NewNamespaceStore(s Store, ns string) Store {
return &NamespaceStore{s: s, ns: ns}
}
func (w *NamespaceStore) Init(opts ...Option) error {
func (w *NamespaceStore) Init(opts ...options.Option) error {
return w.s.Init(opts...)
}
@@ -35,24 +30,24 @@ func (w *NamespaceStore) Disconnect(ctx context.Context) error {
return w.s.Disconnect(ctx)
}
func (w *NamespaceStore) Read(ctx context.Context, key string, val interface{}, opts ...ReadOption) error {
return w.s.Read(ctx, key, val, append(opts, ReadNamespace(w.ns))...)
func (w *NamespaceStore) Read(ctx context.Context, key string, val interface{}, opts ...options.Option) error {
return w.s.Read(ctx, key, val, append(opts, options.Namespace(w.ns))...)
}
func (w *NamespaceStore) Write(ctx context.Context, key string, val interface{}, opts ...WriteOption) error {
return w.s.Write(ctx, key, val, append(opts, WriteNamespace(w.ns))...)
func (w *NamespaceStore) Write(ctx context.Context, key string, val interface{}, opts ...options.Option) error {
return w.s.Write(ctx, key, val, append(opts, options.Namespace(w.ns))...)
}
func (w *NamespaceStore) Delete(ctx context.Context, key string, opts ...DeleteOption) error {
return w.s.Delete(ctx, key, append(opts, DeleteNamespace(w.ns))...)
func (w *NamespaceStore) Delete(ctx context.Context, key string, opts ...options.Option) error {
return w.s.Delete(ctx, key, append(opts, options.Namespace(w.ns))...)
}
func (w *NamespaceStore) Exists(ctx context.Context, key string, opts ...ExistsOption) error {
return w.s.Exists(ctx, key, append(opts, ExistsNamespace(w.ns))...)
func (w *NamespaceStore) Exists(ctx context.Context, key string, opts ...options.Option) error {
return w.s.Exists(ctx, key, append(opts, options.Namespace(w.ns))...)
}
func (w *NamespaceStore) List(ctx context.Context, opts ...ListOption) ([]string, error) {
return w.s.List(ctx, append(opts, ListNamespace(w.ns))...)
func (w *NamespaceStore) List(ctx context.Context, opts ...options.Option) ([]string, error) {
return w.s.List(ctx, append(opts, options.Namespace(w.ns))...)
}
func (w *NamespaceStore) Options() Options {
@@ -66,17 +61,3 @@ func (w *NamespaceStore) Name() string {
func (w *NamespaceStore) String() string {
return w.s.String()
}
// type NamespaceWrapper struct{}
// func NewNamespaceWrapper() Wrapper {
// return &NamespaceWrapper{}
// }
/*
func (w *OmitWrapper) Logf(fn LogfFunc) LogfFunc {
return func(ctx context.Context, level Level, msg string, args ...interface{}) {
fn(ctx, level, msg, getArgs(args)...)
}
}
*/