mirror of
https://github.com/astaxie/beego.git
synced 2025-06-13 09:00:39 +00:00
remove config API's context parameter
This commit is contained in:
@ -30,8 +30,6 @@ import (
|
||||
"github.com/astaxie/beego/core/logs"
|
||||
)
|
||||
|
||||
const etcdOpts = "etcdOpts"
|
||||
|
||||
type EtcdConfiger struct {
|
||||
prefix string
|
||||
client *clientv3.Client
|
||||
@ -50,7 +48,7 @@ func newEtcdConfiger(client *clientv3.Client, prefix string) *EtcdConfiger {
|
||||
|
||||
// reader is an general implementation that read config from etcd.
|
||||
func (e *EtcdConfiger) reader(ctx context.Context, key string) (string, error) {
|
||||
resp, err := get(e.client, ctx, e.prefix+key)
|
||||
resp, err := get(e.client, e.prefix+key)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@ -64,29 +62,24 @@ func (e *EtcdConfiger) reader(ctx context.Context, key string) (string, error) {
|
||||
|
||||
// Set do nothing and return an error
|
||||
// I think write data to remote config center is not a good practice
|
||||
func (e *EtcdConfiger) Set(ctx context.Context, key, val string) error {
|
||||
func (e *EtcdConfiger) Set(key, val string) error {
|
||||
return errors.New("Unsupported operation")
|
||||
}
|
||||
|
||||
// DIY return the original response from etcd
|
||||
// be careful when you decide to use this
|
||||
func (e *EtcdConfiger) DIY(ctx context.Context, key string) (interface{}, error) {
|
||||
return get(e.client, context.TODO(), key)
|
||||
func (e *EtcdConfiger) DIY(key string) (interface{}, error) {
|
||||
return get(e.client, key)
|
||||
}
|
||||
|
||||
// GetSection in this implementation, we use section as prefix
|
||||
func (e *EtcdConfiger) GetSection(ctx context.Context, section string) (map[string]string, error) {
|
||||
func (e *EtcdConfiger) GetSection(section string) (map[string]string, error) {
|
||||
var (
|
||||
resp *clientv3.GetResponse
|
||||
err error
|
||||
)
|
||||
|
||||
if opts, ok := ctx.Value(etcdOpts).([]clientv3.OpOption); ok {
|
||||
opts = append(opts, clientv3.WithPrefix())
|
||||
resp, err = e.client.Get(context.TODO(), e.prefix+section, opts...)
|
||||
} else {
|
||||
resp, err = e.client.Get(context.TODO(), e.prefix+section, clientv3.WithPrefix())
|
||||
}
|
||||
resp, err = e.client.Get(context.TODO(), e.prefix+section, clientv3.WithPrefix())
|
||||
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "GetSection failed")
|
||||
@ -98,15 +91,15 @@ func (e *EtcdConfiger) GetSection(ctx context.Context, section string) (map[stri
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (e *EtcdConfiger) SaveConfigFile(ctx context.Context, filename string) error {
|
||||
func (e *EtcdConfiger) SaveConfigFile(filename string) error {
|
||||
return errors.New("Unsupported operation")
|
||||
}
|
||||
|
||||
// Unmarshaler is not very powerful because we lost the type information when we get configuration from etcd
|
||||
// for example, when we got "5", we are not sure whether it's int 5, or it's string "5"
|
||||
// TODO(support more complicated decoder)
|
||||
func (e *EtcdConfiger) Unmarshaler(ctx context.Context, prefix string, obj interface{}, opt ...config.DecodeOption) error {
|
||||
res, err := e.GetSection(ctx, prefix)
|
||||
func (e *EtcdConfiger) Unmarshaler(prefix string, obj interface{}, opt ...config.DecodeOption) error {
|
||||
res, err := e.GetSection(prefix)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, fmt.Sprintf("could not read config with prefix: %s", prefix))
|
||||
}
|
||||
@ -120,22 +113,18 @@ func (e *EtcdConfiger) Unmarshaler(ctx context.Context, prefix string, obj inter
|
||||
}
|
||||
|
||||
// Sub return an sub configer.
|
||||
func (e *EtcdConfiger) Sub(ctx context.Context, key string) (config.Configer, error) {
|
||||
func (e *EtcdConfiger) Sub(key string) (config.Configer, error) {
|
||||
return newEtcdConfiger(e.client, e.prefix+key), nil
|
||||
}
|
||||
|
||||
// TODO remove this before release v2.0.0
|
||||
func (e *EtcdConfiger) OnChange(ctx context.Context, key string, fn func(value string)) {
|
||||
func (e *EtcdConfiger) OnChange(key string, fn func(value string)) {
|
||||
|
||||
buildOptsFunc := func() []clientv3.OpOption {
|
||||
if opts, ok := ctx.Value(etcdOpts).([]clientv3.OpOption); ok {
|
||||
opts = append(opts, clientv3.WithCreatedNotify())
|
||||
return opts
|
||||
}
|
||||
return []clientv3.OpOption{}
|
||||
}
|
||||
|
||||
rch := e.client.Watch(ctx, e.prefix+key, buildOptsFunc()...)
|
||||
rch := e.client.Watch(context.Background(), e.prefix+key, buildOptsFunc()...)
|
||||
go func() {
|
||||
for {
|
||||
for resp := range rch {
|
||||
@ -152,7 +141,7 @@ func (e *EtcdConfiger) OnChange(ctx context.Context, key string, fn func(value s
|
||||
}
|
||||
}
|
||||
time.Sleep(time.Second)
|
||||
rch = e.client.Watch(ctx, e.prefix+key, buildOptsFunc()...)
|
||||
rch = e.client.Watch(context.Background(), e.prefix+key, buildOptsFunc()...)
|
||||
}
|
||||
}()
|
||||
|
||||
@ -188,16 +177,12 @@ func (provider *EtcdConfigerProvider) ParseData(data []byte) (config.Configer, e
|
||||
return newEtcdConfiger(client, ""), nil
|
||||
}
|
||||
|
||||
func get(client *clientv3.Client, ctx context.Context, key string) (*clientv3.GetResponse, error) {
|
||||
func get(client *clientv3.Client, key string) (*clientv3.GetResponse, error) {
|
||||
var (
|
||||
resp *clientv3.GetResponse
|
||||
err error
|
||||
)
|
||||
if opts, ok := ctx.Value(etcdOpts).([]clientv3.OpOption); ok {
|
||||
resp, err = client.Get(ctx, key, opts...)
|
||||
} else {
|
||||
resp, err = client.Get(ctx, key)
|
||||
}
|
||||
resp, err = client.Get(context.Background(), key)
|
||||
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, fmt.Sprintf("read config from etcd with key %s failed", key))
|
||||
@ -205,10 +190,6 @@ func get(client *clientv3.Client, ctx context.Context, key string) (*clientv3.Ge
|
||||
return resp, err
|
||||
}
|
||||
|
||||
func WithEtcdOption(ctx context.Context, opts ...clientv3.OpOption) context.Context {
|
||||
return context.WithValue(ctx, etcdOpts, opts)
|
||||
}
|
||||
|
||||
func init() {
|
||||
config.Register("json", &EtcdConfigerProvider{})
|
||||
}
|
||||
|
@ -15,7 +15,6 @@
|
||||
package etcd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"testing"
|
||||
@ -25,11 +24,6 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestWithEtcdOption(t *testing.T) {
|
||||
ctx := WithEtcdOption(context.Background(), clientv3.WithPrefix())
|
||||
assert.NotNil(t, ctx.Value(etcdOpts))
|
||||
}
|
||||
|
||||
func TestEtcdConfigerProvider_Parse(t *testing.T) {
|
||||
provider := &EtcdConfigerProvider{}
|
||||
cfger, err := provider.Parse(readEtcdConfig())
|
||||
@ -42,59 +36,59 @@ func TestEtcdConfiger(t *testing.T) {
|
||||
provider := &EtcdConfigerProvider{}
|
||||
cfger, _ := provider.Parse(readEtcdConfig())
|
||||
|
||||
subCfger, err := cfger.Sub(nil, "sub.")
|
||||
subCfger, err := cfger.Sub("sub.")
|
||||
assert.Nil(t, err)
|
||||
assert.NotNil(t, subCfger)
|
||||
|
||||
subSubCfger, err := subCfger.Sub(nil, "sub.")
|
||||
subSubCfger, err := subCfger.Sub("sub.")
|
||||
assert.NotNil(t, subSubCfger)
|
||||
assert.Nil(t, err)
|
||||
|
||||
str, err := subSubCfger.String(nil, "key1")
|
||||
str, err := subSubCfger.String("key1")
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, "sub.sub.key", str)
|
||||
|
||||
// we cannot test it
|
||||
subSubCfger.OnChange(context.Background(), "watch", func(value string) {
|
||||
subSubCfger.OnChange("watch", func(value string) {
|
||||
// do nothing
|
||||
})
|
||||
|
||||
defStr := cfger.DefaultString(nil, "not_exit", "default value")
|
||||
defStr := cfger.DefaultString("not_exit", "default value")
|
||||
assert.Equal(t, "default value", defStr)
|
||||
|
||||
defInt64 := cfger.DefaultInt64(nil, "not_exit", -1)
|
||||
defInt64 := cfger.DefaultInt64("not_exit", -1)
|
||||
assert.Equal(t, int64(-1), defInt64)
|
||||
|
||||
defInt := cfger.DefaultInt(nil, "not_exit", -2)
|
||||
defInt := cfger.DefaultInt("not_exit", -2)
|
||||
assert.Equal(t, -2, defInt)
|
||||
|
||||
defFlt := cfger.DefaultFloat(nil, "not_exit", 12.3)
|
||||
defFlt := cfger.DefaultFloat("not_exit", 12.3)
|
||||
assert.Equal(t, 12.3, defFlt)
|
||||
|
||||
defBl := cfger.DefaultBool(nil, "not_exit", true)
|
||||
defBl := cfger.DefaultBool("not_exit", true)
|
||||
assert.True(t, defBl)
|
||||
|
||||
defStrs := cfger.DefaultStrings(nil, "not_exit", []string{"hello"})
|
||||
defStrs := cfger.DefaultStrings("not_exit", []string{"hello"})
|
||||
assert.Equal(t, []string{"hello"}, defStrs)
|
||||
|
||||
fl, err := cfger.Float(nil, "current.float")
|
||||
fl, err := cfger.Float("current.float")
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, 1.23, fl)
|
||||
|
||||
bl, err := cfger.Bool(nil, "current.bool")
|
||||
bl, err := cfger.Bool("current.bool")
|
||||
assert.Nil(t, err)
|
||||
assert.True(t, bl)
|
||||
|
||||
it, err := cfger.Int(nil, "current.int")
|
||||
it, err := cfger.Int("current.int")
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, 11, it)
|
||||
|
||||
str, err = cfger.String(nil, "current.string")
|
||||
str, err = cfger.String("current.string")
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, "hello", str)
|
||||
|
||||
tn := &TestEntity{}
|
||||
err = cfger.Unmarshaler(context.Background(), "current.serialize.", tn)
|
||||
err = cfger.Unmarshaler("current.serialize.", tn)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, "test", tn.Name)
|
||||
}
|
||||
|
Reference in New Issue
Block a user