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
"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]

View File

@ -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

View File

@ -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)

View File

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

View File

@ -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
}