From be6e8a7c7827bd99cbf376228343df5ed4bf7a01 Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Mon, 6 Jan 2020 17:44:32 +0000 Subject: [PATCH] add store to defaults (#1086) --- config/cmd/cmd.go | 28 ++++++++++++++++++++++++++++ config/cmd/options.go | 3 +++ store/cockroach/cockroach.go | 2 +- store/cockroach/cockroach_test.go | 2 +- store/store.go | 20 ++++++++++++++++++++ 5 files changed, 53 insertions(+), 2 deletions(-) diff --git a/config/cmd/cmd.go b/config/cmd/cmd.go index 381bde0c..96973c2f 100644 --- a/config/cmd/cmd.go +++ b/config/cmd/cmd.go @@ -49,6 +49,14 @@ import ( // runtimes "github.com/micro/go-micro/runtime" "github.com/micro/go-micro/runtime/kubernetes" + + // stores + "github.com/micro/go-micro/store" + cfStore "github.com/micro/go-micro/store/cloudflare" + ckStore "github.com/micro/go-micro/store/cockroach" + etcdStore "github.com/micro/go-micro/store/etcd" + memStore "github.com/micro/go-micro/store/memory" + svcStore "github.com/micro/go-micro/store/service" ) type Cmd interface { @@ -239,6 +247,14 @@ var ( "kubernetes": kubernetes.NewRuntime, } + DefaultStores = map[string]func(...store.Option) store.Store{ + "memory": memStore.NewStore, + "cockroach": ckStore.NewStore, + "etcd": etcdStore.NewStore, + "cloudflare": cfStore.NewStore, + "service": svcStore.NewStore, + } + // used for default selection as the fall back defaultClient = "grpc" defaultServer = "grpc" @@ -267,6 +283,7 @@ func newCmd(opts ...Option) Cmd { Selector: &selector.DefaultSelector, Transport: &transport.DefaultTransport, Runtime: &runtime.DefaultRuntime, + Store: &store.DefaultStore, Brokers: DefaultBrokers, Clients: DefaultClients, @@ -275,6 +292,7 @@ func newCmd(opts ...Option) Cmd { Servers: DefaultServers, Transports: DefaultTransports, Runtimes: DefaultRuntimes, + Stores: DefaultStores, } for _, o := range opts { @@ -315,6 +333,16 @@ func (c *cmd) Before(ctx *cli.Context) error { var serverOpts []server.Option var clientOpts []client.Option + // Set the runtime + if name := ctx.String("store"); len(name) > 0 { + s, ok := c.opts.Stores[name] + if !ok { + return fmt.Errorf("Unsupported store: %s", name) + } + + *c.opts.Store = s() + } + // Set the runtime if name := ctx.String("runtime"); len(name) > 0 { r, ok := c.opts.Runtimes[name] diff --git a/config/cmd/options.go b/config/cmd/options.go index be831fe1..b6bf0ede 100644 --- a/config/cmd/options.go +++ b/config/cmd/options.go @@ -9,6 +9,7 @@ import ( "github.com/micro/go-micro/registry" "github.com/micro/go-micro/runtime" "github.com/micro/go-micro/server" + "github.com/micro/go-micro/store" "github.com/micro/go-micro/transport" ) @@ -26,6 +27,7 @@ type Options struct { Client *client.Client Server *server.Server Runtime *runtime.Runtime + Store *store.Store Brokers map[string]func(...broker.Option) broker.Broker Clients map[string]func(...client.Option) client.Client @@ -34,6 +36,7 @@ type Options struct { Servers map[string]func(...server.Option) server.Server Transports map[string]func(...transport.Option) transport.Transport Runtimes map[string]func(...runtime.Option) runtime.Runtime + Stores map[string]func(...store.Option) store.Store // Other options for implementations of the interface // can be stored in a context diff --git a/store/cockroach/cockroach.go b/store/cockroach/cockroach.go index 31ec7d0c..7979d501 100644 --- a/store/cockroach/cockroach.go +++ b/store/cockroach/cockroach.go @@ -173,7 +173,7 @@ func (s *sqlStore) initDB() { } // New returns a new micro Store backed by sql -func New(opts ...store.Option) store.Store { +func NewStore(opts ...store.Option) store.Store { var options store.Options for _, o := range opts { o(&options) diff --git a/store/cockroach/cockroach_test.go b/store/cockroach/cockroach_test.go index 45948634..08f20135 100644 --- a/store/cockroach/cockroach_test.go +++ b/store/cockroach/cockroach_test.go @@ -27,7 +27,7 @@ func TestSQL(t *testing.T) { } db.Close() - sqlStore := New( + sqlStore := NewStore( store.Namespace("testsql"), store.Nodes(connection), ) diff --git a/store/store.go b/store/store.go index 9d265500..9fb97328 100644 --- a/store/store.go +++ b/store/store.go @@ -9,6 +9,8 @@ import ( var ( // ErrNotFound is returned when a Read key doesn't exist ErrNotFound = errors.New("not found") + // Default store + DefaultStore Store = new(noop) ) // Store is a data storage interface @@ -29,3 +31,21 @@ type Record struct { Value []byte Expiry time.Duration } + +type noop struct{} + +func (n *noop) List() ([]*Record, error) { + return nil, nil +} + +func (n *noop) Read(key ...string) ([]*Record, error) { + return nil, nil +} + +func (n *noop) Write(rec ...*Record) error { + return nil +} + +func (n *noop) Delete(key ...string) error { + return nil +}