mirror of
https://github.com/astaxie/beego.git
synced 2025-06-12 08:00:40 +00:00
Add contect as first parameter for all config method
This commit is contained in:
@ -64,23 +64,18 @@ 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(key, val string) error {
|
||||
func (e *EtcdConfiger) Set(ctx context.Context, 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(key string) (interface{}, error) {
|
||||
func (e *EtcdConfiger) DIY(ctx context.Context, key string) (interface{}, error) {
|
||||
return get(e.client, context.TODO(), key)
|
||||
}
|
||||
|
||||
// GetSection in this implementation, we use section as prefix
|
||||
func (e *EtcdConfiger) GetSection(section string) (map[string]string, error) {
|
||||
return e.GetSectionWithCtx(context.Background(), section)
|
||||
}
|
||||
|
||||
func (e *EtcdConfiger) GetSectionWithCtx(ctx context.Context, section string) (map[string]string, error) {
|
||||
|
||||
func (e *EtcdConfiger) GetSection(ctx context.Context, section string) (map[string]string, error) {
|
||||
var (
|
||||
resp *clientv3.GetResponse
|
||||
err error
|
||||
@ -103,7 +98,7 @@ func (e *EtcdConfiger) GetSectionWithCtx(ctx context.Context, section string) (m
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (e *EtcdConfiger) SaveConfigFile(filename string) error {
|
||||
func (e *EtcdConfiger) SaveConfigFile(ctx context.Context, filename string) error {
|
||||
return errors.New("Unsupported operation")
|
||||
}
|
||||
|
||||
@ -111,7 +106,7 @@ func (e *EtcdConfiger) SaveConfigFile(filename string) error {
|
||||
// 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.GetSectionWithCtx(ctx, prefix)
|
||||
res, err := e.GetSection(ctx, prefix)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, fmt.Sprintf("could not read config with prefix: %s", prefix))
|
||||
}
|
||||
@ -125,7 +120,7 @@ func (e *EtcdConfiger) Unmarshaler(ctx context.Context, prefix string, obj inter
|
||||
}
|
||||
|
||||
// Sub return an sub configer.
|
||||
func (e *EtcdConfiger) Sub(key string) (config.Configer, error) {
|
||||
func (e *EtcdConfiger) Sub(ctx context.Context, key string) (config.Configer, error) {
|
||||
return newEtcdConfiger(e.client, e.prefix+key), nil
|
||||
}
|
||||
|
||||
|
@ -42,15 +42,15 @@ func TestEtcdConfiger(t *testing.T) {
|
||||
provider := &EtcdConfigerProvider{}
|
||||
cfger, _ := provider.Parse(readEtcdConfig())
|
||||
|
||||
subCfger, err := cfger.Sub("sub.")
|
||||
subCfger, err := cfger.Sub(nil, "sub.")
|
||||
assert.Nil(t, err)
|
||||
assert.NotNil(t, subCfger)
|
||||
|
||||
subSubCfger, err := subCfger.Sub("sub.")
|
||||
subSubCfger, err := subCfger.Sub(nil, "sub.")
|
||||
assert.NotNil(t, subSubCfger)
|
||||
assert.Nil(t, err)
|
||||
|
||||
str, err := subSubCfger.String("key1")
|
||||
str, err := subSubCfger.String(nil, "key1")
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, "sub.sub.key", str)
|
||||
|
||||
@ -59,37 +59,37 @@ func TestEtcdConfiger(t *testing.T) {
|
||||
// do nothing
|
||||
})
|
||||
|
||||
defStr := cfger.DefaultString("not_exit", "default value")
|
||||
defStr := cfger.DefaultString(nil, "not_exit", "default value")
|
||||
assert.Equal(t, "default value", defStr)
|
||||
|
||||
defInt64 := cfger.DefaultInt64("not_exit", -1)
|
||||
defInt64 := cfger.DefaultInt64(nil, "not_exit", -1)
|
||||
assert.Equal(t, int64(-1), defInt64)
|
||||
|
||||
defInt := cfger.DefaultInt("not_exit", -2)
|
||||
defInt := cfger.DefaultInt(nil, "not_exit", -2)
|
||||
assert.Equal(t, -2, defInt)
|
||||
|
||||
defFlt := cfger.DefaultFloat("not_exit", 12.3)
|
||||
defFlt := cfger.DefaultFloat(nil, "not_exit", 12.3)
|
||||
assert.Equal(t, 12.3, defFlt)
|
||||
|
||||
defBl := cfger.DefaultBool("not_exit", true)
|
||||
defBl := cfger.DefaultBool(nil, "not_exit", true)
|
||||
assert.True(t, defBl)
|
||||
|
||||
defStrs := cfger.DefaultStrings("not_exit", []string{"hello"})
|
||||
defStrs := cfger.DefaultStrings(nil, "not_exit", []string{"hello"})
|
||||
assert.Equal(t, []string{"hello"}, defStrs)
|
||||
|
||||
fl, err := cfger.Float("current.float")
|
||||
fl, err := cfger.Float(nil, "current.float")
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, 1.23, fl)
|
||||
|
||||
bl, err := cfger.Bool("current.bool")
|
||||
bl, err := cfger.Bool(nil, "current.bool")
|
||||
assert.Nil(t, err)
|
||||
assert.True(t, bl)
|
||||
|
||||
it, err := cfger.Int("current.int")
|
||||
it, err := cfger.Int(nil, "current.int")
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, 11, it)
|
||||
|
||||
str, err = cfger.String("current.string")
|
||||
str, err = cfger.String(nil, "current.string")
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, "hello", str)
|
||||
|
||||
|
Reference in New Issue
Block a user