Skip to content

Commit

Permalink
change store options
Browse files Browse the repository at this point in the history
  • Loading branch information
asim committed Dec 16, 2019
1 parent e8e1121 commit 59751c0
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 175 deletions.
42 changes: 14 additions & 28 deletions store/cloudflare/cloudflare.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"strconv"
"time"

"github.com/micro/go-micro/config/options"
"github.com/micro/go-micro/store"
"github.com/pkg/errors"
)
Expand All @@ -27,7 +26,7 @@ const (
)

type workersKV struct {
options.Options
options store.Options
// cf account id
account string
// cf api token
Expand Down Expand Up @@ -291,38 +290,25 @@ func (w *workersKV) request(ctx context.Context, method, path string, body inter
// CF_API_TOKEN to a cloudflare API token scoped to Workers KV.
// CF_ACCOUNT_ID to contain a string with your cloudflare account ID.
// KV_NAMESPACE_ID to contain the namespace UUID for your KV storage.
func NewStore(opts ...options.Option) store.Store {
// create new Options
options := options.NewOptions(opts...)
func NewStore(opts ...store.Option) store.Store {
var options store.Options
for _, o := range opts {
o(&options)
}

// get values from the environment
// get options from environment
account, token, namespace := getOptions()

// set api token from options if exists
if apiToken, ok := options.Values().Get("CF_API_TOKEN"); ok {
tk, ok := apiToken.(string)
if !ok {
log.Fatal("Store: Option CF_API_TOKEN contains a non-string")
}
token = tk
if len(account) == 0 {
account = getAccount(options.Context)
}

// set account id from options if exists
if accountID, ok := options.Values().Get("CF_ACCOUNT_ID"); ok {
id, ok := accountID.(string)
if !ok {
log.Fatal("Store: Option CF_ACCOUNT_ID contains a non-string")
}
account = id
if len(token) == 0 {
token = getToken(options.Context)
}

// set namespace from options if exists
if uuid, ok := options.Values().Get("KV_NAMESPACE_ID"); ok {
ns, ok := uuid.(string)
if !ok {
log.Fatal("Store: Option KV_NAMESPACE_ID contains a non-string")
}
namespace = ns
if len(namespace) == 0 {
namespace = getNamespace(options.Context)
}

// validate options are not blank or log.Fatal
Expand All @@ -332,7 +318,7 @@ func NewStore(opts ...options.Option) store.Store {
account: account,
namespace: namespace,
token: token,
Options: options,
options: options,
httpClient: &http.Client{},
}
}
57 changes: 47 additions & 10 deletions store/cloudflare/options.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,60 @@
package cloudflare

import (
"github.com/micro/go-micro/config/options"
"context"

"github.com/micro/go-micro/store"
)

func getOption(ctx context.Context, key string) string {
if ctx == nil {
return ""
}
val, ok := ctx.Value(key).(string)
if !ok {
return ""
}
return val
}

func getToken(ctx context.Context) string {
return getOption(ctx, "CF_API_TOKEN")
}

func getAccount(ctx context.Context) string {
return getOption(ctx, "CF_ACCOUNT_ID")
}

func getNamespace(ctx context.Context) string {
return getOption(ctx, "KV_NAMESPACE_ID")
}

// Token sets the cloudflare api token
func Token(t string) options.Option {
// TODO: change to store.cf.api_token
return options.WithValue("CF_API_TOKEN", t)
func Token(t string) store.Option {
return func(o *store.Options) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, "CF_API_TOKEN", t)
}
}

// Account sets the cloudflare account id
func Account(id string) options.Option {
// TODO: change to store.cf.account_id
return options.WithValue("CF_ACCOUNT_ID", id)
func Account(id string) store.Option {
return func(o *store.Options) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, "CF_ACCOUNT_ID", id)
}
}

// Namespace sets the KV namespace
func Namespace(ns string) options.Option {
// TODO: change to store.cf.namespace
return options.WithValue("KV_NAMESPACE_ID", ns)
func Namespace(ns string) store.Option {
return func(o *store.Options) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, "KV_NAMESPACE_ID", ns)
}
}
21 changes: 10 additions & 11 deletions store/etcd/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ import (

client "github.com/coreos/etcd/clientv3"
"github.com/coreos/etcd/mvcc/mvccpb"
"github.com/micro/go-micro/config/options"
"github.com/micro/go-micro/store"
)

type ekv struct {
options.Options
kv client.KV
options store.Options
kv client.KV
}

func (e *ekv) Read(keys ...string) ([]*store.Record, error) {
Expand Down Expand Up @@ -91,15 +90,15 @@ func (e *ekv) String() string {
return "etcd"
}

func NewStore(opts ...options.Option) store.Store {
options := options.NewOptions(opts...)

var endpoints []string

if e, ok := options.Values().Get("store.nodes"); ok {
endpoints = e.([]string)
func NewStore(opts ...store.Option) store.Store {
var options store.Options
for _, o := range opts {
o(&options)
}

// get the endpoints
endpoints := options.Nodes

if len(endpoints) == 0 {
endpoints = []string{"http://127.0.0.1:2379"}
}
Expand All @@ -113,7 +112,7 @@ func NewStore(opts ...options.Option) store.Store {
}

return &ekv{
Options: options,
options: options,
kv: client.NewKV(c),
}
}
12 changes: 7 additions & 5 deletions store/memory/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ import (
"sync"
"time"

"github.com/micro/go-micro/config/options"
"github.com/micro/go-micro/store"
)

type memoryStore struct {
options.Options
options store.Options

sync.RWMutex
values map[string]*memoryRecord
Expand Down Expand Up @@ -110,11 +109,14 @@ func (m *memoryStore) Delete(keys ...string) error {
}

// NewStore returns a new store.Store
func NewStore(opts ...options.Option) store.Store {
options := options.NewOptions(opts...)
func NewStore(opts ...store.Option) store.Store {
var options store.Options
for _, o := range opts {
o(&options)
}

return &memoryStore{
Options: options,
options: options,
values: make(map[string]*memoryRecord),
}
}
24 changes: 17 additions & 7 deletions store/options.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package store

import (
"github.com/micro/go-micro/config/options"
"context"
)

type Options struct {
Expand All @@ -11,20 +11,30 @@ type Options struct {
Namespace string
// Prefix of the keys used
Prefix string
// Alternative options
Context context.Context
}

type Option func(o *Options)

// Nodes is a list of nodes used to back the store
func Nodes(a ...string) options.Option {
return options.WithValue("store.nodes", a)
func Nodes(a ...string) Option {
return func(o *Options) {
o.Nodes = a
}
}

// Prefix sets a prefix to any key ids used
func Prefix(p string) options.Option {
return options.WithValue("store.prefix", p)
func Prefix(p string) Option {
return func(o *Options) {
o.Prefix = p
}
}

// Namespace offers a way to have multiple isolated
// stores in the same backend, if supported.
func Namespace(n string) options.Option {
return options.WithValue("store.namespace", n)
func Namespace(ns string) Option {
return func(o *Options) {
o.Namespace = ns
}
}
Loading

0 comments on commit 59751c0

Please sign in to comment.