1
0
mirror of https://github.com/go-micro/go-micro.git synced 2025-06-24 22:26:54 +02:00

Move data to top level

This commit is contained in:
Asim Aslam
2019-06-11 17:20:52 +01:00
parent 6d06ee8078
commit 8e4e710e15
8 changed files with 39 additions and 63 deletions

96
data/consul/consul.go Normal file
View File

@ -0,0 +1,96 @@
// Package consul is a consul implementation of kv
package consul
import (
"fmt"
"net"
"github.com/hashicorp/consul/api"
"github.com/micro/go-micro/data"
"github.com/micro/go-micro/options"
)
type ckv struct {
options.Options
client *api.Client
}
func (c *ckv) Read(key string) (*data.Record, error) {
keyval, _, err := c.client.KV().Get(key, nil)
if err != nil {
return nil, err
}
if keyval == nil {
return nil, data.ErrNotFound
}
return &data.Record{
Key: keyval.Key,
Value: keyval.Value,
}, nil
}
func (c *ckv) Delete(key string) error {
_, err := c.client.KV().Delete(key, nil)
return err
}
func (c *ckv) Write(record *data.Record) error {
_, err := c.client.KV().Put(&api.KVPair{
Key: record.Key,
Value: record.Value,
}, nil)
return err
}
func (c *ckv) Dump() ([]*data.Record, error) {
keyval, _, err := c.client.KV().List("/", nil)
if err != nil {
return nil, err
}
if keyval == nil {
return nil, data.ErrNotFound
}
var vals []*data.Record
for _, keyv := range keyval {
vals = append(vals, &data.Record{
Key: keyv.Key,
Value: keyv.Value,
})
}
return vals, nil
}
func (c *ckv) String() string {
return "consul"
}
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
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", nodes[0], port)
} else if err == nil {
config.Address = fmt.Sprintf("%s:%s", addr, port)
}
}
client, _ := api.NewClient(config)
return &ckv{
Options: options,
client: client,
}
}

34
data/data.go Normal file
View File

@ -0,0 +1,34 @@
// Package data is an interface for key-value storage.
package data
import (
"errors"
"time"
"github.com/micro/go-micro/options"
)
var (
ErrNotFound = errors.New("not found")
)
// 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
Read(key string) (*Record, error)
// Write a record
Write(r *Record) error
// Delete a record with key
Delete(key string) error
}
// Record represents a data record
type Record struct {
Key string
Value []byte
Expiration time.Duration
}

15
data/options.go Normal file
View File

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