From 8e4e710e1539712f36b9abc52422ae976fd901b0 Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Tue, 11 Jun 2019 17:20:52 +0100 Subject: [PATCH] Move data to top level --- {sync/data => data}/consul/consul.go | 29 +++++++++++++++------------- {sync/data => data}/data.go | 6 ++++-- data/options.go | 15 ++++++++++++++ sync/README.md | 25 ------------------------ sync/data/options.go | 19 ------------------ sync/db.go | 4 ++-- sync/options.go | 2 +- sync/sync.go | 2 +- 8 files changed, 39 insertions(+), 63 deletions(-) rename {sync/data => data}/consul/consul.go (75%) rename {sync/data => data}/data.go (88%) create mode 100644 data/options.go delete mode 100644 sync/data/options.go diff --git a/sync/data/consul/consul.go b/data/consul/consul.go similarity index 75% rename from sync/data/consul/consul.go rename to data/consul/consul.go index 9d2c7a93..58c62495 100644 --- a/sync/data/consul/consul.go +++ b/data/consul/consul.go @@ -6,10 +6,12 @@ import ( "net" "github.com/hashicorp/consul/api" - "github.com/micro/go-micro/sync/data" + "github.com/micro/go-micro/data" + "github.com/micro/go-micro/options" ) type ckv struct { + options.Options client *api.Client } @@ -64,22 +66,22 @@ func (c *ckv) String() string { return "consul" } -func NewData(opts ...data.Option) data.Data { - var options data.Options - for _, o := range opts { - o(&options) - } - +func NewData(opts ...options.Option) data.Data { + options := options.NewOptions(opts...) config := api.DefaultConfig() + var nodes []string + + if n, ok := options.Values().Get("data.nodes"); ok { + nodes = n.([]string) + } + // set host - // config.Host something - // check if there are any addrs - if len(options.Nodes) > 0 { - addr, port, err := net.SplitHostPort(options.Nodes[0]) + if len(nodes) > 0 { + addr, port, err := net.SplitHostPort(nodes[0]) if ae, ok := err.(*net.AddrError); ok && ae.Err == "missing port in address" { port = "8500" - config.Address = fmt.Sprintf("%s:%s", options.Nodes[0], port) + config.Address = fmt.Sprintf("%s:%s", nodes[0], port) } else if err == nil { config.Address = fmt.Sprintf("%s:%s", addr, port) } @@ -88,6 +90,7 @@ func NewData(opts ...data.Option) data.Data { client, _ := api.NewClient(config) return &ckv{ - client: client, + Options: options, + client: client, } } diff --git a/sync/data/data.go b/data/data.go similarity index 88% rename from sync/data/data.go rename to data/data.go index 20e4a4c8..004c5160 100644 --- a/sync/data/data.go +++ b/data/data.go @@ -4,6 +4,8 @@ package data import ( "errors" "time" + + "github.com/micro/go-micro/options" ) var ( @@ -12,6 +14,8 @@ var ( // Data is a data storage interface type Data interface { + // embed options + options.Options // Dump the known records Dump() ([]*Record, error) // Read a record with key @@ -28,5 +32,3 @@ type Record struct { Value []byte Expiration time.Duration } - -type Option func(o *Options) diff --git a/data/options.go b/data/options.go new file mode 100644 index 00000000..510e0fe3 --- /dev/null +++ b/data/options.go @@ -0,0 +1,15 @@ +package data + +import ( + "github.com/micro/go-micro/options" +) + +// Set the nodes used to back the data +func Nodes(a ...string) options.Option { + return options.WithValue("data.nodes", a) +} + +// Prefix sets a prefix to any key ids used +func Prefix(p string) options.Option { + return options.WithValue("data.prefix", p) +} diff --git a/sync/README.md b/sync/README.md index 94349374..d69c2060 100644 --- a/sync/README.md +++ b/sync/README.md @@ -10,7 +10,6 @@ an external database or eventing system. Go Sync provides a framework for synchr ## Getting Started -- [Data](#data) - simple distributed data storage - [Leader](#leader) - leadership election for group coordination - [Lock](#lock) - distributed locking for exclusive resource access - [Task](#task) - distributed job execution @@ -70,30 +69,6 @@ for { e.Resign() ``` -## Data - -Data provides a simple interface for distributed data storage. - -```go -import ( - "github.com/micro/go-micro/sync/data" - "github.com/micro/go-micro/sync/data/consul" -) - -keyval := consul.NewData() - -err := keyval.Write(&data.Record{ - Key: "foo", - Value: []byte(`bar`), -}) -// handle err - -v, err := keyval.Read("foo") -// handle err - -err = keyval.Delete("foo") -``` - ## Task Task provides distributed job execution. It's a simple way to distribute work across a coordinated pool of workers. diff --git a/sync/data/options.go b/sync/data/options.go deleted file mode 100644 index 1d85aca3..00000000 --- a/sync/data/options.go +++ /dev/null @@ -1,19 +0,0 @@ -package data - -type Options struct { - Nodes []string - Prefix string -} - -func Nodes(a ...string) Option { - return func(o *Options) { - o.Nodes = a - } -} - -// Prefix sets a prefix to any lock ids used -func Prefix(p string) Option { - return func(o *Options) { - o.Prefix = p - } -} diff --git a/sync/db.go b/sync/db.go index a16af598..b5141d42 100644 --- a/sync/db.go +++ b/sync/db.go @@ -6,8 +6,8 @@ import ( "encoding/json" "fmt" - "github.com/micro/go-micro/sync/data" - ckv "github.com/micro/go-micro/sync/data/consul" + "github.com/micro/go-micro/data" + ckv "github.com/micro/go-micro/data/consul" lock "github.com/micro/go-micro/sync/lock/consul" ) diff --git a/sync/options.go b/sync/options.go index 8b46acf0..2922b77c 100644 --- a/sync/options.go +++ b/sync/options.go @@ -1,7 +1,7 @@ package sync import ( - "github.com/micro/go-micro/sync/data" + "github.com/micro/go-micro/data" "github.com/micro/go-micro/sync/leader" "github.com/micro/go-micro/sync/lock" "github.com/micro/go-micro/sync/time" diff --git a/sync/sync.go b/sync/sync.go index 7cb63c51..b27118e5 100644 --- a/sync/sync.go +++ b/sync/sync.go @@ -2,7 +2,7 @@ package sync import ( - "github.com/micro/go-micro/sync/data" + "github.com/micro/go-micro/data" "github.com/micro/go-micro/sync/leader" "github.com/micro/go-micro/sync/lock" "github.com/micro/go-micro/sync/task"