add store to defaults (#1086)

This commit is contained in:
Asim Aslam 2020-01-06 17:44:32 +00:00 committed by GitHub
parent df9055f69c
commit be6e8a7c78
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 53 additions and 2 deletions

View File

@ -49,6 +49,14 @@ import (
// runtimes // runtimes
"github.com/micro/go-micro/runtime" "github.com/micro/go-micro/runtime"
"github.com/micro/go-micro/runtime/kubernetes" "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 { type Cmd interface {
@ -239,6 +247,14 @@ var (
"kubernetes": kubernetes.NewRuntime, "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 // used for default selection as the fall back
defaultClient = "grpc" defaultClient = "grpc"
defaultServer = "grpc" defaultServer = "grpc"
@ -267,6 +283,7 @@ func newCmd(opts ...Option) Cmd {
Selector: &selector.DefaultSelector, Selector: &selector.DefaultSelector,
Transport: &transport.DefaultTransport, Transport: &transport.DefaultTransport,
Runtime: &runtime.DefaultRuntime, Runtime: &runtime.DefaultRuntime,
Store: &store.DefaultStore,
Brokers: DefaultBrokers, Brokers: DefaultBrokers,
Clients: DefaultClients, Clients: DefaultClients,
@ -275,6 +292,7 @@ func newCmd(opts ...Option) Cmd {
Servers: DefaultServers, Servers: DefaultServers,
Transports: DefaultTransports, Transports: DefaultTransports,
Runtimes: DefaultRuntimes, Runtimes: DefaultRuntimes,
Stores: DefaultStores,
} }
for _, o := range opts { for _, o := range opts {
@ -315,6 +333,16 @@ func (c *cmd) Before(ctx *cli.Context) error {
var serverOpts []server.Option var serverOpts []server.Option
var clientOpts []client.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 // Set the runtime
if name := ctx.String("runtime"); len(name) > 0 { if name := ctx.String("runtime"); len(name) > 0 {
r, ok := c.opts.Runtimes[name] r, ok := c.opts.Runtimes[name]

View File

@ -9,6 +9,7 @@ import (
"github.com/micro/go-micro/registry" "github.com/micro/go-micro/registry"
"github.com/micro/go-micro/runtime" "github.com/micro/go-micro/runtime"
"github.com/micro/go-micro/server" "github.com/micro/go-micro/server"
"github.com/micro/go-micro/store"
"github.com/micro/go-micro/transport" "github.com/micro/go-micro/transport"
) )
@ -26,6 +27,7 @@ type Options struct {
Client *client.Client Client *client.Client
Server *server.Server Server *server.Server
Runtime *runtime.Runtime Runtime *runtime.Runtime
Store *store.Store
Brokers map[string]func(...broker.Option) broker.Broker Brokers map[string]func(...broker.Option) broker.Broker
Clients map[string]func(...client.Option) client.Client Clients map[string]func(...client.Option) client.Client
@ -34,6 +36,7 @@ type Options struct {
Servers map[string]func(...server.Option) server.Server Servers map[string]func(...server.Option) server.Server
Transports map[string]func(...transport.Option) transport.Transport Transports map[string]func(...transport.Option) transport.Transport
Runtimes map[string]func(...runtime.Option) runtime.Runtime Runtimes map[string]func(...runtime.Option) runtime.Runtime
Stores map[string]func(...store.Option) store.Store
// Other options for implementations of the interface // Other options for implementations of the interface
// can be stored in a context // can be stored in a context

View File

@ -173,7 +173,7 @@ func (s *sqlStore) initDB() {
} }
// New returns a new micro Store backed by sql // 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 var options store.Options
for _, o := range opts { for _, o := range opts {
o(&options) o(&options)

View File

@ -27,7 +27,7 @@ func TestSQL(t *testing.T) {
} }
db.Close() db.Close()
sqlStore := New( sqlStore := NewStore(
store.Namespace("testsql"), store.Namespace("testsql"),
store.Nodes(connection), store.Nodes(connection),
) )

View File

@ -9,6 +9,8 @@ import (
var ( var (
// ErrNotFound is returned when a Read key doesn't exist // ErrNotFound is returned when a Read key doesn't exist
ErrNotFound = errors.New("not found") ErrNotFound = errors.New("not found")
// Default store
DefaultStore Store = new(noop)
) )
// Store is a data storage interface // Store is a data storage interface
@ -29,3 +31,21 @@ type Record struct {
Value []byte Value []byte
Expiry time.Duration 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
}