mirror of
https://github.com/astaxie/beego.git
synced 2025-01-15 18:27:12 +00:00
Merge pull request #4264 from flycash/rft/configCtx
remove config API's context parameter
This commit is contained in:
commit
ccf873fa8b
@ -15,8 +15,6 @@
|
||||
package adapter
|
||||
|
||||
import (
|
||||
context2 "context"
|
||||
|
||||
"github.com/astaxie/beego/adapter/session"
|
||||
newCfg "github.com/astaxie/beego/core/config"
|
||||
"github.com/astaxie/beego/server/web"
|
||||
@ -74,54 +72,54 @@ type beegoAppConfig struct {
|
||||
}
|
||||
|
||||
func (b *beegoAppConfig) Set(key, val string) error {
|
||||
if err := b.innerConfig.Set(context2.Background(), BConfig.RunMode+"::"+key, val); err != nil {
|
||||
return b.innerConfig.Set(context2.Background(), key, val)
|
||||
if err := b.innerConfig.Set(BConfig.RunMode+"::"+key, val); err != nil {
|
||||
return b.innerConfig.Set(key, val)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *beegoAppConfig) String(key string) string {
|
||||
if v, err := b.innerConfig.String(context2.Background(), BConfig.RunMode+"::"+key); v != "" && err != nil {
|
||||
if v, err := b.innerConfig.String(BConfig.RunMode + "::" + key); v != "" && err != nil {
|
||||
return v
|
||||
}
|
||||
res, _ := b.innerConfig.String(context2.Background(), key)
|
||||
res, _ := b.innerConfig.String(key)
|
||||
return res
|
||||
}
|
||||
|
||||
func (b *beegoAppConfig) Strings(key string) []string {
|
||||
if v, err := b.innerConfig.Strings(context2.Background(), BConfig.RunMode+"::"+key); len(v) > 0 && err != nil {
|
||||
if v, err := b.innerConfig.Strings(BConfig.RunMode + "::" + key); len(v) > 0 && err != nil {
|
||||
return v
|
||||
}
|
||||
res, _ := b.innerConfig.Strings(context2.Background(), key)
|
||||
res, _ := b.innerConfig.Strings(key)
|
||||
return res
|
||||
}
|
||||
|
||||
func (b *beegoAppConfig) Int(key string) (int, error) {
|
||||
if v, err := b.innerConfig.Int(context2.Background(), BConfig.RunMode+"::"+key); err == nil {
|
||||
if v, err := b.innerConfig.Int(BConfig.RunMode + "::" + key); err == nil {
|
||||
return v, nil
|
||||
}
|
||||
return b.innerConfig.Int(context2.Background(), key)
|
||||
return b.innerConfig.Int(key)
|
||||
}
|
||||
|
||||
func (b *beegoAppConfig) Int64(key string) (int64, error) {
|
||||
if v, err := b.innerConfig.Int64(context2.Background(), BConfig.RunMode+"::"+key); err == nil {
|
||||
if v, err := b.innerConfig.Int64(BConfig.RunMode + "::" + key); err == nil {
|
||||
return v, nil
|
||||
}
|
||||
return b.innerConfig.Int64(context2.Background(), key)
|
||||
return b.innerConfig.Int64(key)
|
||||
}
|
||||
|
||||
func (b *beegoAppConfig) Bool(key string) (bool, error) {
|
||||
if v, err := b.innerConfig.Bool(context2.Background(), BConfig.RunMode+"::"+key); err == nil {
|
||||
if v, err := b.innerConfig.Bool(BConfig.RunMode + "::" + key); err == nil {
|
||||
return v, nil
|
||||
}
|
||||
return b.innerConfig.Bool(context2.Background(), key)
|
||||
return b.innerConfig.Bool(key)
|
||||
}
|
||||
|
||||
func (b *beegoAppConfig) Float(key string) (float64, error) {
|
||||
if v, err := b.innerConfig.Float(context2.Background(), BConfig.RunMode+"::"+key); err == nil {
|
||||
if v, err := b.innerConfig.Float(BConfig.RunMode + "::" + key); err == nil {
|
||||
return v, nil
|
||||
}
|
||||
return b.innerConfig.Float(context2.Background(), key)
|
||||
return b.innerConfig.Float(key)
|
||||
}
|
||||
|
||||
func (b *beegoAppConfig) DefaultString(key string, defaultVal string) string {
|
||||
@ -167,13 +165,13 @@ func (b *beegoAppConfig) DefaultFloat(key string, defaultVal float64) float64 {
|
||||
}
|
||||
|
||||
func (b *beegoAppConfig) DIY(key string) (interface{}, error) {
|
||||
return b.innerConfig.DIY(context2.Background(), key)
|
||||
return b.innerConfig.DIY(key)
|
||||
}
|
||||
|
||||
func (b *beegoAppConfig) GetSection(section string) (map[string]string, error) {
|
||||
return b.innerConfig.GetSection(context2.Background(), section)
|
||||
return b.innerConfig.GetSection(section)
|
||||
}
|
||||
|
||||
func (b *beegoAppConfig) SaveConfigFile(filename string) error {
|
||||
return b.innerConfig.SaveConfigFile(context2.Background(), filename)
|
||||
return b.innerConfig.SaveConfigFile(filename)
|
||||
}
|
||||
|
@ -15,8 +15,6 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/astaxie/beego/core/config"
|
||||
@ -27,148 +25,148 @@ type newToOldConfigerAdapter struct {
|
||||
}
|
||||
|
||||
func (c *newToOldConfigerAdapter) Set(key, val string) error {
|
||||
return c.delegate.Set(context.Background(), key, val)
|
||||
return c.delegate.Set(key, val)
|
||||
}
|
||||
|
||||
func (c *newToOldConfigerAdapter) String(key string) string {
|
||||
res, _ := c.delegate.String(context.Background(), key)
|
||||
res, _ := c.delegate.String(key)
|
||||
return res
|
||||
}
|
||||
|
||||
func (c *newToOldConfigerAdapter) Strings(key string) []string {
|
||||
res, _ := c.delegate.Strings(context.Background(), key)
|
||||
res, _ := c.delegate.Strings(key)
|
||||
return res
|
||||
}
|
||||
|
||||
func (c *newToOldConfigerAdapter) Int(key string) (int, error) {
|
||||
return c.delegate.Int(context.Background(), key)
|
||||
return c.delegate.Int(key)
|
||||
}
|
||||
|
||||
func (c *newToOldConfigerAdapter) Int64(key string) (int64, error) {
|
||||
return c.delegate.Int64(context.Background(), key)
|
||||
return c.delegate.Int64(key)
|
||||
}
|
||||
|
||||
func (c *newToOldConfigerAdapter) Bool(key string) (bool, error) {
|
||||
return c.delegate.Bool(context.Background(), key)
|
||||
return c.delegate.Bool(key)
|
||||
}
|
||||
|
||||
func (c *newToOldConfigerAdapter) Float(key string) (float64, error) {
|
||||
return c.delegate.Float(context.Background(), key)
|
||||
return c.delegate.Float(key)
|
||||
}
|
||||
|
||||
func (c *newToOldConfigerAdapter) DefaultString(key string, defaultVal string) string {
|
||||
return c.delegate.DefaultString(context.Background(), key, defaultVal)
|
||||
return c.delegate.DefaultString(key, defaultVal)
|
||||
}
|
||||
|
||||
func (c *newToOldConfigerAdapter) DefaultStrings(key string, defaultVal []string) []string {
|
||||
return c.delegate.DefaultStrings(context.Background(), key, defaultVal)
|
||||
return c.delegate.DefaultStrings(key, defaultVal)
|
||||
}
|
||||
|
||||
func (c *newToOldConfigerAdapter) DefaultInt(key string, defaultVal int) int {
|
||||
return c.delegate.DefaultInt(context.Background(), key, defaultVal)
|
||||
return c.delegate.DefaultInt(key, defaultVal)
|
||||
}
|
||||
|
||||
func (c *newToOldConfigerAdapter) DefaultInt64(key string, defaultVal int64) int64 {
|
||||
return c.delegate.DefaultInt64(context.Background(), key, defaultVal)
|
||||
return c.delegate.DefaultInt64(key, defaultVal)
|
||||
}
|
||||
|
||||
func (c *newToOldConfigerAdapter) DefaultBool(key string, defaultVal bool) bool {
|
||||
return c.delegate.DefaultBool(context.Background(), key, defaultVal)
|
||||
return c.delegate.DefaultBool(key, defaultVal)
|
||||
}
|
||||
|
||||
func (c *newToOldConfigerAdapter) DefaultFloat(key string, defaultVal float64) float64 {
|
||||
return c.delegate.DefaultFloat(context.Background(), key, defaultVal)
|
||||
return c.delegate.DefaultFloat(key, defaultVal)
|
||||
}
|
||||
|
||||
func (c *newToOldConfigerAdapter) DIY(key string) (interface{}, error) {
|
||||
return c.delegate.DIY(context.Background(), key)
|
||||
return c.delegate.DIY(key)
|
||||
}
|
||||
|
||||
func (c *newToOldConfigerAdapter) GetSection(section string) (map[string]string, error) {
|
||||
return c.delegate.GetSection(context.Background(), section)
|
||||
return c.delegate.GetSection(section)
|
||||
}
|
||||
|
||||
func (c *newToOldConfigerAdapter) SaveConfigFile(filename string) error {
|
||||
return c.delegate.SaveConfigFile(context.Background(), filename)
|
||||
return c.delegate.SaveConfigFile(filename)
|
||||
}
|
||||
|
||||
type oldToNewConfigerAdapter struct {
|
||||
delegate Configer
|
||||
}
|
||||
|
||||
func (o *oldToNewConfigerAdapter) Set(ctx context.Context, key, val string) error {
|
||||
func (o *oldToNewConfigerAdapter) Set(key, val string) error {
|
||||
return o.delegate.Set(key, val)
|
||||
}
|
||||
|
||||
func (o *oldToNewConfigerAdapter) String(ctx context.Context, key string) (string, error) {
|
||||
func (o *oldToNewConfigerAdapter) String(key string) (string, error) {
|
||||
return o.delegate.String(key), nil
|
||||
}
|
||||
|
||||
func (o *oldToNewConfigerAdapter) Strings(ctx context.Context, key string) ([]string, error) {
|
||||
func (o *oldToNewConfigerAdapter) Strings(key string) ([]string, error) {
|
||||
return o.delegate.Strings(key), nil
|
||||
}
|
||||
|
||||
func (o *oldToNewConfigerAdapter) Int(ctx context.Context, key string) (int, error) {
|
||||
func (o *oldToNewConfigerAdapter) Int(key string) (int, error) {
|
||||
return o.delegate.Int(key)
|
||||
}
|
||||
|
||||
func (o *oldToNewConfigerAdapter) Int64(ctx context.Context, key string) (int64, error) {
|
||||
func (o *oldToNewConfigerAdapter) Int64(key string) (int64, error) {
|
||||
return o.delegate.Int64(key)
|
||||
}
|
||||
|
||||
func (o *oldToNewConfigerAdapter) Bool(ctx context.Context, key string) (bool, error) {
|
||||
func (o *oldToNewConfigerAdapter) Bool(key string) (bool, error) {
|
||||
return o.delegate.Bool(key)
|
||||
}
|
||||
|
||||
func (o *oldToNewConfigerAdapter) Float(ctx context.Context, key string) (float64, error) {
|
||||
func (o *oldToNewConfigerAdapter) Float(key string) (float64, error) {
|
||||
return o.delegate.Float(key)
|
||||
}
|
||||
|
||||
func (o *oldToNewConfigerAdapter) DefaultString(ctx context.Context, key string, defaultVal string) string {
|
||||
func (o *oldToNewConfigerAdapter) DefaultString(key string, defaultVal string) string {
|
||||
return o.delegate.DefaultString(key, defaultVal)
|
||||
}
|
||||
|
||||
func (o *oldToNewConfigerAdapter) DefaultStrings(ctx context.Context, key string, defaultVal []string) []string {
|
||||
func (o *oldToNewConfigerAdapter) DefaultStrings(key string, defaultVal []string) []string {
|
||||
return o.delegate.DefaultStrings(key, defaultVal)
|
||||
}
|
||||
|
||||
func (o *oldToNewConfigerAdapter) DefaultInt(ctx context.Context, key string, defaultVal int) int {
|
||||
func (o *oldToNewConfigerAdapter) DefaultInt(key string, defaultVal int) int {
|
||||
return o.delegate.DefaultInt(key, defaultVal)
|
||||
}
|
||||
|
||||
func (o *oldToNewConfigerAdapter) DefaultInt64(ctx context.Context, key string, defaultVal int64) int64 {
|
||||
func (o *oldToNewConfigerAdapter) DefaultInt64(key string, defaultVal int64) int64 {
|
||||
return o.delegate.DefaultInt64(key, defaultVal)
|
||||
}
|
||||
|
||||
func (o *oldToNewConfigerAdapter) DefaultBool(ctx context.Context, key string, defaultVal bool) bool {
|
||||
func (o *oldToNewConfigerAdapter) DefaultBool(key string, defaultVal bool) bool {
|
||||
return o.delegate.DefaultBool(key, defaultVal)
|
||||
}
|
||||
|
||||
func (o *oldToNewConfigerAdapter) DefaultFloat(ctx context.Context, key string, defaultVal float64) float64 {
|
||||
func (o *oldToNewConfigerAdapter) DefaultFloat(key string, defaultVal float64) float64 {
|
||||
return o.delegate.DefaultFloat(key, defaultVal)
|
||||
}
|
||||
|
||||
func (o *oldToNewConfigerAdapter) DIY(ctx context.Context, key string) (interface{}, error) {
|
||||
func (o *oldToNewConfigerAdapter) DIY(key string) (interface{}, error) {
|
||||
return o.delegate.DIY(key)
|
||||
}
|
||||
|
||||
func (o *oldToNewConfigerAdapter) GetSection(ctx context.Context, section string) (map[string]string, error) {
|
||||
func (o *oldToNewConfigerAdapter) GetSection(section string) (map[string]string, error) {
|
||||
return o.delegate.GetSection(section)
|
||||
}
|
||||
|
||||
func (o *oldToNewConfigerAdapter) Unmarshaler(ctx context.Context, prefix string, obj interface{}, opt ...config.DecodeOption) error {
|
||||
func (o *oldToNewConfigerAdapter) Unmarshaler(prefix string, obj interface{}, opt ...config.DecodeOption) error {
|
||||
return errors.New("unsupported operation, please use actual config.Configer")
|
||||
}
|
||||
|
||||
func (o *oldToNewConfigerAdapter) Sub(ctx context.Context, key string) (config.Configer, error) {
|
||||
func (o *oldToNewConfigerAdapter) Sub(key string) (config.Configer, error) {
|
||||
return nil, errors.New("unsupported operation, please use actual config.Configer")
|
||||
}
|
||||
|
||||
func (o *oldToNewConfigerAdapter) OnChange(ctx context.Context, key string, fn func(value string)) {
|
||||
func (o *oldToNewConfigerAdapter) OnChange(key string, fn func(value string)) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
func (o *oldToNewConfigerAdapter) SaveConfigFile(ctx context.Context, filename string) error {
|
||||
func (o *oldToNewConfigerAdapter) SaveConfigFile(filename string) error {
|
||||
return o.delegate.SaveConfigFile(filename)
|
||||
}
|
||||
|
||||
|
@ -24,38 +24,38 @@ import (
|
||||
|
||||
func TestBaseConfiger_DefaultBool(t *testing.T) {
|
||||
bc := newBaseConfier("true")
|
||||
assert.True(t, bc.DefaultBool(context.Background(), "key1", false))
|
||||
assert.True(t, bc.DefaultBool(context.Background(), "key2", true))
|
||||
assert.True(t, bc.DefaultBool("key1", false))
|
||||
assert.True(t, bc.DefaultBool("key2", true))
|
||||
}
|
||||
|
||||
func TestBaseConfiger_DefaultFloat(t *testing.T) {
|
||||
bc := newBaseConfier("12.3")
|
||||
assert.Equal(t, 12.3, bc.DefaultFloat(context.Background(), "key1", 0.1))
|
||||
assert.Equal(t, 0.1, bc.DefaultFloat(context.Background(), "key2", 0.1))
|
||||
assert.Equal(t, 12.3, bc.DefaultFloat("key1", 0.1))
|
||||
assert.Equal(t, 0.1, bc.DefaultFloat("key2", 0.1))
|
||||
}
|
||||
|
||||
func TestBaseConfiger_DefaultInt(t *testing.T) {
|
||||
bc := newBaseConfier("10")
|
||||
assert.Equal(t, 10, bc.DefaultInt(context.Background(), "key1", 8))
|
||||
assert.Equal(t, 8, bc.DefaultInt(context.Background(), "key2", 8))
|
||||
assert.Equal(t, 10, bc.DefaultInt("key1", 8))
|
||||
assert.Equal(t, 8, bc.DefaultInt("key2", 8))
|
||||
}
|
||||
|
||||
func TestBaseConfiger_DefaultInt64(t *testing.T) {
|
||||
bc := newBaseConfier("64")
|
||||
assert.Equal(t, int64(64), bc.DefaultInt64(context.Background(), "key1", int64(8)))
|
||||
assert.Equal(t, int64(8), bc.DefaultInt64(context.Background(), "key2", int64(8)))
|
||||
assert.Equal(t, int64(64), bc.DefaultInt64("key1", int64(8)))
|
||||
assert.Equal(t, int64(8), bc.DefaultInt64("key2", int64(8)))
|
||||
}
|
||||
|
||||
func TestBaseConfiger_DefaultString(t *testing.T) {
|
||||
bc := newBaseConfier("Hello")
|
||||
assert.Equal(t, "Hello", bc.DefaultString(context.Background(), "key1", "world"))
|
||||
assert.Equal(t, "world", bc.DefaultString(context.Background(), "key2", "world"))
|
||||
assert.Equal(t, "Hello", bc.DefaultString("key1", "world"))
|
||||
assert.Equal(t, "world", bc.DefaultString("key2", "world"))
|
||||
}
|
||||
|
||||
func TestBaseConfiger_DefaultStrings(t *testing.T) {
|
||||
bc := newBaseConfier("Hello;world")
|
||||
assert.Equal(t, []string{"Hello", "world"}, bc.DefaultStrings(context.Background(), "key1", []string{"world"}))
|
||||
assert.Equal(t, []string{"world"}, bc.DefaultStrings(context.Background(), "key2", []string{"world"}))
|
||||
assert.Equal(t, []string{"Hello", "world"}, bc.DefaultStrings("key1", []string{"world"}))
|
||||
assert.Equal(t, []string{"world"}, bc.DefaultStrings("key2", []string{"world"}))
|
||||
}
|
||||
|
||||
func newBaseConfier(str1 string) *BaseConfiger {
|
||||
|
@ -54,34 +54,34 @@ import (
|
||||
// Configer defines how to get and set value from configuration raw data.
|
||||
type Configer interface {
|
||||
// support section::key type in given key when using ini type.
|
||||
Set(ctx context.Context, key, val string) error
|
||||
Set(key, val string) error
|
||||
|
||||
// support section::key type in key string when using ini and json type; Int,Int64,Bool,Float,DIY are same.
|
||||
String(ctx context.Context, key string) (string, error)
|
||||
String(key string) (string, error)
|
||||
// get string slice
|
||||
Strings(ctx context.Context, key string) ([]string, error)
|
||||
Int(ctx context.Context, key string) (int, error)
|
||||
Int64(ctx context.Context, key string) (int64, error)
|
||||
Bool(ctx context.Context, key string) (bool, error)
|
||||
Float(ctx context.Context, key string) (float64, error)
|
||||
Strings(key string) ([]string, error)
|
||||
Int(key string) (int, error)
|
||||
Int64(key string) (int64, error)
|
||||
Bool(key string) (bool, error)
|
||||
Float(key string) (float64, error)
|
||||
// support section::key type in key string when using ini and json type; Int,Int64,Bool,Float,DIY are same.
|
||||
DefaultString(ctx context.Context, key string, defaultVal string) string
|
||||
DefaultString(key string, defaultVal string) string
|
||||
// get string slice
|
||||
DefaultStrings(ctx context.Context, key string, defaultVal []string) []string
|
||||
DefaultInt(ctx context.Context, key string, defaultVal int) int
|
||||
DefaultInt64(ctx context.Context, key string, defaultVal int64) int64
|
||||
DefaultBool(ctx context.Context, key string, defaultVal bool) bool
|
||||
DefaultFloat(ctx context.Context, key string, defaultVal float64) float64
|
||||
DefaultStrings(key string, defaultVal []string) []string
|
||||
DefaultInt(key string, defaultVal int) int
|
||||
DefaultInt64(key string, defaultVal int64) int64
|
||||
DefaultBool(key string, defaultVal bool) bool
|
||||
DefaultFloat(key string, defaultVal float64) float64
|
||||
|
||||
// DIY return the original value
|
||||
DIY(ctx context.Context, key string) (interface{}, error)
|
||||
DIY(key string) (interface{}, error)
|
||||
|
||||
GetSection(ctx context.Context, section string) (map[string]string, error)
|
||||
GetSection(section string) (map[string]string, error)
|
||||
|
||||
Unmarshaler(ctx context.Context, prefix string, obj interface{}, opt ...DecodeOption) error
|
||||
Sub(ctx context.Context, key string) (Configer, error)
|
||||
OnChange(ctx context.Context, key string, fn func(value string))
|
||||
SaveConfigFile(ctx context.Context, filename string) error
|
||||
Unmarshaler(prefix string, obj interface{}, opt ...DecodeOption) error
|
||||
Sub(key string) (Configer, error)
|
||||
OnChange(key string, fn func(value string))
|
||||
SaveConfigFile(filename string) error
|
||||
}
|
||||
|
||||
type BaseConfiger struct {
|
||||
@ -95,7 +95,7 @@ func NewBaseConfiger(reader func(ctx context.Context, key string) (string, error
|
||||
}
|
||||
}
|
||||
|
||||
func (c *BaseConfiger) Int(ctx context.Context, key string) (int, error) {
|
||||
func (c *BaseConfiger) Int(key string) (int, error) {
|
||||
res, err := c.reader(context.TODO(), key)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
@ -103,7 +103,7 @@ func (c *BaseConfiger) Int(ctx context.Context, key string) (int, error) {
|
||||
return strconv.Atoi(res)
|
||||
}
|
||||
|
||||
func (c *BaseConfiger) Int64(ctx context.Context, key string) (int64, error) {
|
||||
func (c *BaseConfiger) Int64(key string) (int64, error) {
|
||||
res, err := c.reader(context.TODO(), key)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
@ -111,7 +111,7 @@ func (c *BaseConfiger) Int64(ctx context.Context, key string) (int64, error) {
|
||||
return strconv.ParseInt(res, 10, 64)
|
||||
}
|
||||
|
||||
func (c *BaseConfiger) Bool(ctx context.Context, key string) (bool, error) {
|
||||
func (c *BaseConfiger) Bool(key string) (bool, error) {
|
||||
res, err := c.reader(context.TODO(), key)
|
||||
if err != nil {
|
||||
return false, err
|
||||
@ -119,7 +119,7 @@ func (c *BaseConfiger) Bool(ctx context.Context, key string) (bool, error) {
|
||||
return ParseBool(res)
|
||||
}
|
||||
|
||||
func (c *BaseConfiger) Float(ctx context.Context, key string) (float64, error) {
|
||||
func (c *BaseConfiger) Float(key string) (float64, error) {
|
||||
res, err := c.reader(context.TODO(), key)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
@ -129,8 +129,8 @@ func (c *BaseConfiger) Float(ctx context.Context, key string) (float64, error) {
|
||||
|
||||
// DefaultString returns the string value for a given key.
|
||||
// if err != nil or value is empty return defaultval
|
||||
func (c *BaseConfiger) DefaultString(ctx context.Context, key string, defaultVal string) string {
|
||||
if res, err := c.String(ctx, key); res != "" && err == nil {
|
||||
func (c *BaseConfiger) DefaultString(key string, defaultVal string) string {
|
||||
if res, err := c.String(key); res != "" && err == nil {
|
||||
return res
|
||||
}
|
||||
return defaultVal
|
||||
@ -138,63 +138,63 @@ func (c *BaseConfiger) DefaultString(ctx context.Context, key string, defaultVal
|
||||
|
||||
// DefaultStrings returns the []string value for a given key.
|
||||
// if err != nil return defaultval
|
||||
func (c *BaseConfiger) DefaultStrings(ctx context.Context, key string, defaultVal []string) []string {
|
||||
if res, err := c.Strings(ctx, key); len(res) > 0 && err == nil {
|
||||
func (c *BaseConfiger) DefaultStrings(key string, defaultVal []string) []string {
|
||||
if res, err := c.Strings(key); len(res) > 0 && err == nil {
|
||||
return res
|
||||
}
|
||||
return defaultVal
|
||||
}
|
||||
|
||||
func (c *BaseConfiger) DefaultInt(ctx context.Context, key string, defaultVal int) int {
|
||||
if res, err := c.Int(ctx, key); err == nil {
|
||||
func (c *BaseConfiger) DefaultInt(key string, defaultVal int) int {
|
||||
if res, err := c.Int(key); err == nil {
|
||||
return res
|
||||
}
|
||||
return defaultVal
|
||||
}
|
||||
|
||||
func (c *BaseConfiger) DefaultInt64(ctx context.Context, key string, defaultVal int64) int64 {
|
||||
if res, err := c.Int64(ctx, key); err == nil {
|
||||
func (c *BaseConfiger) DefaultInt64(key string, defaultVal int64) int64 {
|
||||
if res, err := c.Int64(key); err == nil {
|
||||
return res
|
||||
}
|
||||
return defaultVal
|
||||
}
|
||||
|
||||
func (c *BaseConfiger) DefaultBool(ctx context.Context, key string, defaultVal bool) bool {
|
||||
if res, err := c.Bool(ctx, key); err == nil {
|
||||
func (c *BaseConfiger) DefaultBool(key string, defaultVal bool) bool {
|
||||
if res, err := c.Bool(key); err == nil {
|
||||
return res
|
||||
}
|
||||
return defaultVal
|
||||
}
|
||||
func (c *BaseConfiger) DefaultFloat(ctx context.Context, key string, defaultVal float64) float64 {
|
||||
if res, err := c.Float(ctx, key); err == nil {
|
||||
func (c *BaseConfiger) DefaultFloat(key string, defaultVal float64) float64 {
|
||||
if res, err := c.Float(key); err == nil {
|
||||
return res
|
||||
}
|
||||
return defaultVal
|
||||
}
|
||||
|
||||
func (c *BaseConfiger) String(ctx context.Context, key string) (string, error) {
|
||||
func (c *BaseConfiger) String(key string) (string, error) {
|
||||
return c.reader(context.TODO(), key)
|
||||
}
|
||||
|
||||
// Strings returns the []string value for a given key.
|
||||
// Return nil if config value does not exist or is empty.
|
||||
func (c *BaseConfiger) Strings(ctx context.Context, key string) ([]string, error) {
|
||||
res, err := c.String(nil, key)
|
||||
func (c *BaseConfiger) Strings(key string) ([]string, error) {
|
||||
res, err := c.String(key)
|
||||
if err != nil || res == "" {
|
||||
return nil, err
|
||||
}
|
||||
return strings.Split(res, ";"), nil
|
||||
}
|
||||
|
||||
func (c *BaseConfiger) Unmarshaler(ctx context.Context, prefix string, obj interface{}, opt ...DecodeOption) error {
|
||||
func (c *BaseConfiger) Unmarshaler(prefix string, obj interface{}, opt ...DecodeOption) error {
|
||||
return errors.New("unsupported operation")
|
||||
}
|
||||
|
||||
func (c *BaseConfiger) Sub(ctx context.Context, key string) (Configer, error) {
|
||||
func (c *BaseConfiger) Sub(key string) (Configer, error) {
|
||||
return nil, errors.New("unsupported operation")
|
||||
}
|
||||
|
||||
func (c *BaseConfiger) OnChange(ctx context.Context, key string, fn func(value string)) {
|
||||
func (c *BaseConfiger) OnChange(key string, fn func(value string)) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -30,71 +30,71 @@ func (c *fakeConfigContainer) getData(key string) string {
|
||||
return c.data[strings.ToLower(key)]
|
||||
}
|
||||
|
||||
func (c *fakeConfigContainer) Set(ctx context.Context, key, val string) error {
|
||||
func (c *fakeConfigContainer) Set(key, val string) error {
|
||||
c.data[strings.ToLower(key)] = val
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *fakeConfigContainer) Int(ctx context.Context, key string) (int, error) {
|
||||
func (c *fakeConfigContainer) Int(key string) (int, error) {
|
||||
return strconv.Atoi(c.getData(key))
|
||||
}
|
||||
|
||||
func (c *fakeConfigContainer) DefaultInt(ctx context.Context, key string, defaultVal int) int {
|
||||
v, err := c.Int(ctx, key)
|
||||
func (c *fakeConfigContainer) DefaultInt(key string, defaultVal int) int {
|
||||
v, err := c.Int(key)
|
||||
if err != nil {
|
||||
return defaultVal
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
func (c *fakeConfigContainer) Int64(ctx context.Context, key string) (int64, error) {
|
||||
func (c *fakeConfigContainer) Int64(key string) (int64, error) {
|
||||
return strconv.ParseInt(c.getData(key), 10, 64)
|
||||
}
|
||||
|
||||
func (c *fakeConfigContainer) DefaultInt64(ctx context.Context, key string, defaultVal int64) int64 {
|
||||
v, err := c.Int64(ctx, key)
|
||||
func (c *fakeConfigContainer) DefaultInt64(key string, defaultVal int64) int64 {
|
||||
v, err := c.Int64(key)
|
||||
if err != nil {
|
||||
return defaultVal
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
func (c *fakeConfigContainer) Bool(ctx context.Context, key string) (bool, error) {
|
||||
func (c *fakeConfigContainer) Bool(key string) (bool, error) {
|
||||
return ParseBool(c.getData(key))
|
||||
}
|
||||
|
||||
func (c *fakeConfigContainer) DefaultBool(ctx context.Context, key string, defaultVal bool) bool {
|
||||
v, err := c.Bool(ctx, key)
|
||||
func (c *fakeConfigContainer) DefaultBool(key string, defaultVal bool) bool {
|
||||
v, err := c.Bool(key)
|
||||
if err != nil {
|
||||
return defaultVal
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
func (c *fakeConfigContainer) Float(ctx context.Context, key string) (float64, error) {
|
||||
func (c *fakeConfigContainer) Float(key string) (float64, error) {
|
||||
return strconv.ParseFloat(c.getData(key), 64)
|
||||
}
|
||||
|
||||
func (c *fakeConfigContainer) DefaultFloat(ctx context.Context, key string, defaultVal float64) float64 {
|
||||
v, err := c.Float(ctx, key)
|
||||
func (c *fakeConfigContainer) DefaultFloat(key string, defaultVal float64) float64 {
|
||||
v, err := c.Float(key)
|
||||
if err != nil {
|
||||
return defaultVal
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
func (c *fakeConfigContainer) DIY(ctx context.Context, key string) (interface{}, error) {
|
||||
func (c *fakeConfigContainer) DIY(key string) (interface{}, error) {
|
||||
if v, ok := c.data[strings.ToLower(key)]; ok {
|
||||
return v, nil
|
||||
}
|
||||
return nil, errors.New("key not find")
|
||||
}
|
||||
|
||||
func (c *fakeConfigContainer) GetSection(ctx context.Context, section string) (map[string]string, error) {
|
||||
func (c *fakeConfigContainer) GetSection(section string) (map[string]string, error) {
|
||||
return nil, errors.New("not implement in the fakeConfigContainer")
|
||||
}
|
||||
|
||||
func (c *fakeConfigContainer) SaveConfigFile(ctx context.Context, filename string) error {
|
||||
func (c *fakeConfigContainer) SaveConfigFile(filename string) error {
|
||||
return errors.New("not implement in the fakeConfigContainer")
|
||||
}
|
||||
|
||||
|
@ -238,14 +238,14 @@ type IniConfigContainer struct {
|
||||
}
|
||||
|
||||
// Bool returns the boolean value for a given key.
|
||||
func (c *IniConfigContainer) Bool(ctx context.Context, key string) (bool, error) {
|
||||
func (c *IniConfigContainer) Bool(key string) (bool, error) {
|
||||
return ParseBool(c.getdata(key))
|
||||
}
|
||||
|
||||
// DefaultBool returns the boolean value for a given key.
|
||||
// if err != nil return defaultVal
|
||||
func (c *IniConfigContainer) DefaultBool(ctx context.Context, key string, defaultVal bool) bool {
|
||||
v, err := c.Bool(ctx, key)
|
||||
func (c *IniConfigContainer) DefaultBool(key string, defaultVal bool) bool {
|
||||
v, err := c.Bool(key)
|
||||
if err != nil {
|
||||
return defaultVal
|
||||
}
|
||||
@ -253,14 +253,14 @@ func (c *IniConfigContainer) DefaultBool(ctx context.Context, key string, defaul
|
||||
}
|
||||
|
||||
// Int returns the integer value for a given key.
|
||||
func (c *IniConfigContainer) Int(ctx context.Context, key string) (int, error) {
|
||||
func (c *IniConfigContainer) Int(key string) (int, error) {
|
||||
return strconv.Atoi(c.getdata(key))
|
||||
}
|
||||
|
||||
// DefaultInt returns the integer value for a given key.
|
||||
// if err != nil return defaultVal
|
||||
func (c *IniConfigContainer) DefaultInt(ctx context.Context, key string, defaultVal int) int {
|
||||
v, err := c.Int(ctx, key)
|
||||
func (c *IniConfigContainer) DefaultInt(key string, defaultVal int) int {
|
||||
v, err := c.Int(key)
|
||||
if err != nil {
|
||||
return defaultVal
|
||||
}
|
||||
@ -268,14 +268,14 @@ func (c *IniConfigContainer) DefaultInt(ctx context.Context, key string, default
|
||||
}
|
||||
|
||||
// Int64 returns the int64 value for a given key.
|
||||
func (c *IniConfigContainer) Int64(ctx context.Context, key string) (int64, error) {
|
||||
func (c *IniConfigContainer) Int64(key string) (int64, error) {
|
||||
return strconv.ParseInt(c.getdata(key), 10, 64)
|
||||
}
|
||||
|
||||
// DefaultInt64 returns the int64 value for a given key.
|
||||
// if err != nil return defaultVal
|
||||
func (c *IniConfigContainer) DefaultInt64(ctx context.Context, key string, defaultVal int64) int64 {
|
||||
v, err := c.Int64(ctx, key)
|
||||
func (c *IniConfigContainer) DefaultInt64(key string, defaultVal int64) int64 {
|
||||
v, err := c.Int64(key)
|
||||
if err != nil {
|
||||
return defaultVal
|
||||
}
|
||||
@ -283,14 +283,14 @@ func (c *IniConfigContainer) DefaultInt64(ctx context.Context, key string, defau
|
||||
}
|
||||
|
||||
// Float returns the float value for a given key.
|
||||
func (c *IniConfigContainer) Float(ctx context.Context, key string) (float64, error) {
|
||||
func (c *IniConfigContainer) Float(key string) (float64, error) {
|
||||
return strconv.ParseFloat(c.getdata(key), 64)
|
||||
}
|
||||
|
||||
// DefaultFloat returns the float64 value for a given key.
|
||||
// if err != nil return defaultVal
|
||||
func (c *IniConfigContainer) DefaultFloat(ctx context.Context, key string, defaultVal float64) float64 {
|
||||
v, err := c.Float(ctx, key)
|
||||
func (c *IniConfigContainer) DefaultFloat(key string, defaultVal float64) float64 {
|
||||
v, err := c.Float(key)
|
||||
if err != nil {
|
||||
return defaultVal
|
||||
}
|
||||
@ -298,14 +298,14 @@ func (c *IniConfigContainer) DefaultFloat(ctx context.Context, key string, defau
|
||||
}
|
||||
|
||||
// String returns the string value for a given key.
|
||||
func (c *IniConfigContainer) String(ctx context.Context, key string) (string, error) {
|
||||
func (c *IniConfigContainer) String(key string) (string, error) {
|
||||
return c.getdata(key), nil
|
||||
}
|
||||
|
||||
// DefaultString returns the string value for a given key.
|
||||
// if err != nil return defaultVal
|
||||
func (c *IniConfigContainer) DefaultString(ctx context.Context, key string, defaultVal string) string {
|
||||
v, err := c.String(nil, key)
|
||||
func (c *IniConfigContainer) DefaultString(key string, defaultVal string) string {
|
||||
v, err := c.String(key)
|
||||
if v == "" || err != nil {
|
||||
return defaultVal
|
||||
}
|
||||
@ -314,8 +314,8 @@ func (c *IniConfigContainer) DefaultString(ctx context.Context, key string, defa
|
||||
|
||||
// Strings returns the []string value for a given key.
|
||||
// Return nil if config value does not exist or is empty.
|
||||
func (c *IniConfigContainer) Strings(ctx context.Context, key string) ([]string, error) {
|
||||
v, err := c.String(nil, key)
|
||||
func (c *IniConfigContainer) Strings(key string) ([]string, error) {
|
||||
v, err := c.String(key)
|
||||
if v == "" || err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -324,8 +324,8 @@ func (c *IniConfigContainer) Strings(ctx context.Context, key string) ([]string,
|
||||
|
||||
// DefaultStrings returns the []string value for a given key.
|
||||
// if err != nil return defaultVal
|
||||
func (c *IniConfigContainer) DefaultStrings(ctx context.Context, key string, defaultVal []string) []string {
|
||||
v, err := c.Strings(ctx, key)
|
||||
func (c *IniConfigContainer) DefaultStrings(key string, defaultVal []string) []string {
|
||||
v, err := c.Strings(key)
|
||||
if v == nil || err != nil {
|
||||
return defaultVal
|
||||
}
|
||||
@ -333,7 +333,7 @@ func (c *IniConfigContainer) DefaultStrings(ctx context.Context, key string, def
|
||||
}
|
||||
|
||||
// GetSection returns map for the given section
|
||||
func (c *IniConfigContainer) GetSection(ctx context.Context, section string) (map[string]string, error) {
|
||||
func (c *IniConfigContainer) GetSection(section string) (map[string]string, error) {
|
||||
if v, ok := c.data[section]; ok {
|
||||
return v, nil
|
||||
}
|
||||
@ -343,7 +343,7 @@ func (c *IniConfigContainer) GetSection(ctx context.Context, section string) (ma
|
||||
// SaveConfigFile save the config into file.
|
||||
//
|
||||
// BUG(env): The environment variable config item will be saved with real value in SaveConfigFile Function.
|
||||
func (c *IniConfigContainer) SaveConfigFile(ctx context.Context, filename string) (err error) {
|
||||
func (c *IniConfigContainer) SaveConfigFile(filename string) (err error) {
|
||||
// Write configuration file by filename.
|
||||
f, err := os.Create(filename)
|
||||
if err != nil {
|
||||
@ -443,7 +443,7 @@ func (c *IniConfigContainer) SaveConfigFile(ctx context.Context, filename string
|
||||
// Set writes a new value for key.
|
||||
// if write to one section, the key need be "section::key".
|
||||
// if the section is not existed, it panics.
|
||||
func (c *IniConfigContainer) Set(ctx context.Context, key, val string) error {
|
||||
func (c *IniConfigContainer) Set(key, val string) error {
|
||||
c.Lock()
|
||||
defer c.Unlock()
|
||||
if len(key) == 0 {
|
||||
@ -471,7 +471,7 @@ func (c *IniConfigContainer) Set(ctx context.Context, key, val string) error {
|
||||
}
|
||||
|
||||
// DIY returns the raw value by a given key.
|
||||
func (c *IniConfigContainer) DIY(ctx context.Context, key string) (v interface{}, err error) {
|
||||
func (c *IniConfigContainer) DIY(key string) (v interface{}, err error) {
|
||||
if v, ok := c.data[strings.ToLower(key)]; ok {
|
||||
return v, nil
|
||||
}
|
||||
|
@ -101,19 +101,19 @@ password = ${GOPATH}
|
||||
var value interface{}
|
||||
switch v.(type) {
|
||||
case int:
|
||||
value, err = iniconf.Int(nil, k)
|
||||
value, err = iniconf.Int(k)
|
||||
case int64:
|
||||
value, err = iniconf.Int64(nil, k)
|
||||
value, err = iniconf.Int64(k)
|
||||
case float64:
|
||||
value, err = iniconf.Float(nil, k)
|
||||
value, err = iniconf.Float(k)
|
||||
case bool:
|
||||
value, err = iniconf.Bool(nil, k)
|
||||
value, err = iniconf.Bool(k)
|
||||
case []string:
|
||||
value, err = iniconf.Strings(nil, k)
|
||||
value, err = iniconf.Strings(k)
|
||||
case string:
|
||||
value, err = iniconf.String(nil, k)
|
||||
value, err = iniconf.String(k)
|
||||
default:
|
||||
value, err = iniconf.DIY(nil, k)
|
||||
value, err = iniconf.DIY(k)
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("get key %q value fail,err %s", k, err)
|
||||
@ -122,10 +122,10 @@ password = ${GOPATH}
|
||||
}
|
||||
|
||||
}
|
||||
if err = iniconf.Set(nil, "name", "astaxie"); err != nil {
|
||||
if err = iniconf.Set("name", "astaxie"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
res, _ := iniconf.String(nil, "name")
|
||||
res, _ := iniconf.String("name")
|
||||
if res != "astaxie" {
|
||||
t.Fatal("get name error")
|
||||
}
|
||||
@ -171,7 +171,7 @@ name=mysql
|
||||
t.Fatal(err)
|
||||
}
|
||||
name := "newIniConfig.ini"
|
||||
if err := cfg.SaveConfigFile(nil, name); err != nil {
|
||||
if err := cfg.SaveConfigFile(name); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.Remove(name)
|
||||
|
@ -15,7 +15,6 @@
|
||||
package json
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
@ -77,16 +76,16 @@ type JSONConfigContainer struct {
|
||||
sync.RWMutex
|
||||
}
|
||||
|
||||
func (c *JSONConfigContainer) Unmarshaler(ctx context.Context, prefix string, obj interface{}, opt ...config.DecodeOption) error {
|
||||
sub, err := c.sub(ctx, prefix)
|
||||
func (c *JSONConfigContainer) Unmarshaler(prefix string, obj interface{}, opt ...config.DecodeOption) error {
|
||||
sub, err := c.sub(prefix)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return mapstructure.Decode(sub, obj)
|
||||
}
|
||||
|
||||
func (c *JSONConfigContainer) Sub(ctx context.Context, key string) (config.Configer, error) {
|
||||
sub, err := c.sub(ctx, key)
|
||||
func (c *JSONConfigContainer) Sub(key string) (config.Configer, error) {
|
||||
sub, err := c.sub(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -95,7 +94,7 @@ func (c *JSONConfigContainer) Sub(ctx context.Context, key string) (config.Confi
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *JSONConfigContainer) sub(ctx context.Context, key string) (map[string]interface{}, error) {
|
||||
func (c *JSONConfigContainer) sub(key string) (map[string]interface{}, error) {
|
||||
if key == "" {
|
||||
return c.data, nil
|
||||
}
|
||||
@ -111,12 +110,12 @@ func (c *JSONConfigContainer) sub(ctx context.Context, key string) (map[string]i
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (c *JSONConfigContainer) OnChange(ctx context.Context, key string, fn func(value string)) {
|
||||
func (c *JSONConfigContainer) OnChange(key string, fn func(value string)) {
|
||||
logs.Warn("unsupported operation")
|
||||
}
|
||||
|
||||
// Bool returns the boolean value for a given key.
|
||||
func (c *JSONConfigContainer) Bool(ctx context.Context, key string) (bool, error) {
|
||||
func (c *JSONConfigContainer) Bool(key string) (bool, error) {
|
||||
val := c.getData(key)
|
||||
if val != nil {
|
||||
return config.ParseBool(val)
|
||||
@ -126,15 +125,15 @@ func (c *JSONConfigContainer) Bool(ctx context.Context, key string) (bool, error
|
||||
|
||||
// DefaultBool return the bool value if has no error
|
||||
// otherwise return the defaultval
|
||||
func (c *JSONConfigContainer) DefaultBool(ctx context.Context, key string, defaultVal bool) bool {
|
||||
if v, err := c.Bool(ctx, key); err == nil {
|
||||
func (c *JSONConfigContainer) DefaultBool(key string, defaultVal bool) bool {
|
||||
if v, err := c.Bool(key); err == nil {
|
||||
return v
|
||||
}
|
||||
return defaultVal
|
||||
}
|
||||
|
||||
// Int returns the integer value for a given key.
|
||||
func (c *JSONConfigContainer) Int(ctx context.Context, key string) (int, error) {
|
||||
func (c *JSONConfigContainer) Int(key string) (int, error) {
|
||||
val := c.getData(key)
|
||||
if val != nil {
|
||||
if v, ok := val.(float64); ok {
|
||||
@ -149,15 +148,15 @@ func (c *JSONConfigContainer) Int(ctx context.Context, key string) (int, error)
|
||||
|
||||
// DefaultInt returns the integer value for a given key.
|
||||
// if err != nil return defaultval
|
||||
func (c *JSONConfigContainer) DefaultInt(ctx context.Context, key string, defaultVal int) int {
|
||||
if v, err := c.Int(ctx, key); err == nil {
|
||||
func (c *JSONConfigContainer) DefaultInt(key string, defaultVal int) int {
|
||||
if v, err := c.Int(key); err == nil {
|
||||
return v
|
||||
}
|
||||
return defaultVal
|
||||
}
|
||||
|
||||
// Int64 returns the int64 value for a given key.
|
||||
func (c *JSONConfigContainer) Int64(ctx context.Context, key string) (int64, error) {
|
||||
func (c *JSONConfigContainer) Int64(key string) (int64, error) {
|
||||
val := c.getData(key)
|
||||
if val != nil {
|
||||
if v, ok := val.(float64); ok {
|
||||
@ -170,15 +169,15 @@ func (c *JSONConfigContainer) Int64(ctx context.Context, key string) (int64, err
|
||||
|
||||
// DefaultInt64 returns the int64 value for a given key.
|
||||
// if err != nil return defaultval
|
||||
func (c *JSONConfigContainer) DefaultInt64(ctx context.Context, key string, defaultVal int64) int64 {
|
||||
if v, err := c.Int64(ctx, key); err == nil {
|
||||
func (c *JSONConfigContainer) DefaultInt64(key string, defaultVal int64) int64 {
|
||||
if v, err := c.Int64(key); err == nil {
|
||||
return v
|
||||
}
|
||||
return defaultVal
|
||||
}
|
||||
|
||||
// Float returns the float value for a given key.
|
||||
func (c *JSONConfigContainer) Float(ctx context.Context, key string) (float64, error) {
|
||||
func (c *JSONConfigContainer) Float(key string) (float64, error) {
|
||||
val := c.getData(key)
|
||||
if val != nil {
|
||||
if v, ok := val.(float64); ok {
|
||||
@ -191,15 +190,15 @@ func (c *JSONConfigContainer) Float(ctx context.Context, key string) (float64, e
|
||||
|
||||
// DefaultFloat returns the float64 value for a given key.
|
||||
// if err != nil return defaultval
|
||||
func (c *JSONConfigContainer) DefaultFloat(ctx context.Context, key string, defaultVal float64) float64 {
|
||||
if v, err := c.Float(ctx, key); err == nil {
|
||||
func (c *JSONConfigContainer) DefaultFloat(key string, defaultVal float64) float64 {
|
||||
if v, err := c.Float(key); err == nil {
|
||||
return v
|
||||
}
|
||||
return defaultVal
|
||||
}
|
||||
|
||||
// String returns the string value for a given key.
|
||||
func (c *JSONConfigContainer) String(ctx context.Context, key string) (string, error) {
|
||||
func (c *JSONConfigContainer) String(key string) (string, error) {
|
||||
val := c.getData(key)
|
||||
if val != nil {
|
||||
if v, ok := val.(string); ok {
|
||||
@ -211,17 +210,17 @@ func (c *JSONConfigContainer) String(ctx context.Context, key string) (string, e
|
||||
|
||||
// DefaultString returns the string value for a given key.
|
||||
// if err != nil return defaultval
|
||||
func (c *JSONConfigContainer) DefaultString(ctx context.Context, key string, defaultVal string) string {
|
||||
func (c *JSONConfigContainer) DefaultString(key string, defaultVal string) string {
|
||||
// TODO FIXME should not use "" to replace non existence
|
||||
if v, err := c.String(ctx, key); v != "" && err == nil {
|
||||
if v, err := c.String(key); v != "" && err == nil {
|
||||
return v
|
||||
}
|
||||
return defaultVal
|
||||
}
|
||||
|
||||
// Strings returns the []string value for a given key.
|
||||
func (c *JSONConfigContainer) Strings(ctx context.Context, key string) ([]string, error) {
|
||||
stringVal, err := c.String(nil, key)
|
||||