1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-22 08:50:56 +00:00

Add contect as first parameter for all config method

This commit is contained in:
Ming Deng 2020-08-29 15:05:18 +00:00
parent e831b97eb8
commit 03bec05714
19 changed files with 351 additions and 356 deletions

View File

@ -34,7 +34,7 @@ func getPort() string {
if err != nil {
return "8080"
}
port, err = config.String("httpport")
port, err = config.String(nil, "httpport")
if err != nil {
return "8080"
}

View File

@ -24,38 +24,38 @@ import (
func TestBaseConfiger_DefaultBool(t *testing.T) {
bc := newBaseConfier("true")
assert.True(t, bc.DefaultBool("key1", false))
assert.True(t, bc.DefaultBool("key2", true))
assert.True(t, bc.DefaultBool(context.Background(), "key1", false))
assert.True(t, bc.DefaultBool(context.Background(), "key2", true))
}
func TestBaseConfiger_DefaultFloat(t *testing.T) {
bc := newBaseConfier("12.3")
assert.Equal(t, 12.3, bc.DefaultFloat("key1", 0.1))
assert.Equal(t, 0.1, bc.DefaultFloat("key2", 0.1))
assert.Equal(t, 12.3, bc.DefaultFloat(context.Background(), "key1", 0.1))
assert.Equal(t, 0.1, bc.DefaultFloat(context.Background(), "key2", 0.1))
}
func TestBaseConfiger_DefaultInt(t *testing.T) {
bc := newBaseConfier("10")
assert.Equal(t, 10, bc.DefaultInt("key1", 8))
assert.Equal(t, 8, bc.DefaultInt("key2", 8))
assert.Equal(t, 10, bc.DefaultInt(context.Background(), "key1", 8))
assert.Equal(t, 8, bc.DefaultInt(context.Background(), "key2", 8))
}
func TestBaseConfiger_DefaultInt64(t *testing.T) {
bc := newBaseConfier("64")
assert.Equal(t, int64(64), bc.DefaultInt64("key1", int64(8)))
assert.Equal(t, int64(8), bc.DefaultInt64("key2", int64(8)))
assert.Equal(t, int64(64), bc.DefaultInt64(context.Background(), "key1", int64(8)))
assert.Equal(t, int64(8), bc.DefaultInt64(context.Background(), "key2", int64(8)))
}
func TestBaseConfiger_DefaultString(t *testing.T) {
bc := newBaseConfier("Hello")
assert.Equal(t, "Hello", bc.DefaultString("key1", "world"))
assert.Equal(t, "world", bc.DefaultString("key2", "world"))
assert.Equal(t, "Hello", bc.DefaultString(context.Background(), "key1", "world"))
assert.Equal(t, "world", bc.DefaultString(context.Background(), "key2", "world"))
}
func TestBaseConfiger_DefaultStrings(t *testing.T) {
bc := newBaseConfier("Hello;world")
assert.Equal(t, []string{"Hello", "world"}, bc.DefaultStrings("key1", []string{"world"}))
assert.Equal(t, []string{"world"}, bc.DefaultStrings("key2", []string{"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"}))
}
func newBaseConfier(str1 string) *BaseConfiger {

View File

@ -54,35 +54,32 @@ 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(key, val string) error
Set(ctx context.Context, 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(key string) (string, error)
String(ctx context.Context, key string) (string, error)
// get string slice
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)
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)
// support section::key type in key string when using ini and json type; Int,Int64,Bool,Float,DIY are same.
DefaultString(key string, defaultVal string) string
DefaultString(ctx context.Context, key string, defaultVal string) string
// get string slice
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(key string) (interface{}, error)
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
DIY(ctx context.Context, key string) (interface{}, error)
GetSection(section string) (map[string]string, error)
GetSectionWithCtx(ctx context.Context, section string) (map[string]string, error)
GetSection(ctx context.Context, section string) (map[string]string, error)
Unmarshaler(ctx context.Context, prefix string, obj interface{}, opt ...DecodeOption) error
Sub(key string) (Configer, error)
Sub(ctx context.Context, key string) (Configer, error)
OnChange(ctx context.Context, key string, fn func(value string))
// GetByPrefix(prefix string) ([]byte, error)
// GetSerializer() Serializer
SaveConfigFile(filename string) error
SaveConfigFile(ctx context.Context, filename string) error
}
type BaseConfiger struct {
@ -96,7 +93,7 @@ func NewBaseConfiger(reader func(ctx context.Context, key string) (string, error
}
}
func (c *BaseConfiger) Int(key string) (int, error) {
func (c *BaseConfiger) Int(ctx context.Context, key string) (int, error) {
res, err := c.reader(context.TODO(), key)
if err != nil {
return 0, err
@ -104,7 +101,7 @@ func (c *BaseConfiger) Int(key string) (int, error) {
return strconv.Atoi(res)
}
func (c *BaseConfiger) Int64(key string) (int64, error) {
func (c *BaseConfiger) Int64(ctx context.Context, key string) (int64, error) {
res, err := c.reader(context.TODO(), key)
if err != nil {
return 0, err
@ -112,7 +109,7 @@ func (c *BaseConfiger) Int64(key string) (int64, error) {
return strconv.ParseInt(res, 10, 64)
}
func (c *BaseConfiger) Bool(key string) (bool, error) {
func (c *BaseConfiger) Bool(ctx context.Context, key string) (bool, error) {
res, err := c.reader(context.TODO(), key)
if err != nil {
return false, err
@ -120,7 +117,7 @@ func (c *BaseConfiger) Bool(key string) (bool, error) {
return ParseBool(res)
}
func (c *BaseConfiger) Float(key string) (float64, error) {
func (c *BaseConfiger) Float(ctx context.Context, key string) (float64, error) {
res, err := c.reader(context.TODO(), key)
if err != nil {
return 0, err
@ -130,8 +127,8 @@ func (c *BaseConfiger) Float(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(key string, defaultVal string) string {
if res, err := c.String(key); res != "" && err == nil {
func (c *BaseConfiger) DefaultString(ctx context.Context, key string, defaultVal string) string {
if res, err := c.String(ctx, key); res != "" && err == nil {
return res
}
return defaultVal
@ -139,53 +136,48 @@ func (c *BaseConfiger) DefaultString(key string, defaultVal string) string {
// DefaultStrings returns the []string value for a given key.
// if err != nil return defaultval
func (c *BaseConfiger) DefaultStrings(key string, defaultVal []string) []string {
if res, err := c.Strings(key); len(res) > 0 && err == nil {
func (c *BaseConfiger) DefaultStrings(ctx context.Context, key string, defaultVal []string) []string {
if res, err := c.Strings(ctx, key); len(res) > 0 && err == nil {
return res
}
return defaultVal
}
func (c *BaseConfiger) DefaultInt(key string, defaultVal int) int {
if res, err := c.Int(key); err == nil {
func (c *BaseConfiger) DefaultInt(ctx context.Context, key string, defaultVal int) int {
if res, err := c.Int(ctx, key); err == nil {
return res
}
return defaultVal
}
func (c *BaseConfiger) DefaultInt64(key string, defaultVal int64) int64 {
if res, err := c.Int64(key); err == nil {
func (c *BaseConfiger) DefaultInt64(ctx context.Context, key string, defaultVal int64) int64 {
if res, err := c.Int64(ctx, key); err == nil {
return res
}
return defaultVal
}
func (c *BaseConfiger) DefaultBool(key string, defaultVal bool) bool {
if res, err := c.Bool(key); err == nil {
func (c *BaseConfiger) DefaultBool(ctx context.Context, key string, defaultVal bool) bool {
if res, err := c.Bool(ctx, key); err == nil {
return res
}
return defaultVal
}
func (c *BaseConfiger) DefaultFloat(key string, defaultVal float64) float64 {
if res, err := c.Float(key); err == nil {
func (c *BaseConfiger) DefaultFloat(ctx context.Context, key string, defaultVal float64) float64 {
if res, err := c.Float(ctx, key); err == nil {
return res
}
return defaultVal
}
func (c *BaseConfiger) GetSectionWithCtx(ctx context.Context, section string) (map[string]string, error) {
// TODO
return nil, nil
}
func (c *BaseConfiger) String(key string) (string, error) {
func (c *BaseConfiger) String(ctx context.Context, 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(key string) ([]string, error) {
res, err := c.String(key)
func (c *BaseConfiger) Strings(ctx context.Context, key string) ([]string, error) {
res, err := c.String(nil, key)
if err != nil || res == "" {
return nil, err
}
@ -198,7 +190,7 @@ func (c *BaseConfiger) Unmarshaler(ctx context.Context, prefix string, obj inter
}
// TODO remove this before release v2.0.0
func (c *BaseConfiger) Sub(key string) (Configer, error) {
func (c *BaseConfiger) Sub(ctx context.Context, key string) (Configer, error) {
return nil, errors.New("unsupported operation")
}

View File

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

View File

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

View File

@ -30,71 +30,71 @@ func (c *fakeConfigContainer) getData(key string) string {
return c.data[strings.ToLower(key)]
}
func (c *fakeConfigContainer) Set(key, val string) error {
func (c *fakeConfigContainer) Set(ctx context.Context, key, val string) error {
c.data[strings.ToLower(key)] = val
return nil
}
func (c *fakeConfigContainer) Int(key string) (int, error) {
func (c *fakeConfigContainer) Int(ctx context.Context, key string) (int, error) {
return strconv.Atoi(c.getData(key))
}
func (c *fakeConfigContainer) DefaultInt(key string, defaultval int) int {
v, err := c.Int(key)
func (c *fakeConfigContainer) DefaultInt(ctx context.Context, key string, defaultVal int) int {
v, err := c.Int(ctx, key)
if err != nil {
return defaultval
return defaultVal
}
return v
}
func (c *fakeConfigContainer) Int64(key string) (int64, error) {
func (c *fakeConfigContainer) Int64(ctx context.Context, key string) (int64, error) {
return strconv.ParseInt(c.getData(key), 10, 64)
}
func (c *fakeConfigContainer) DefaultInt64(key string, defaultval int64) int64 {
v, err := c.Int64(key)
func (c *fakeConfigContainer) DefaultInt64(ctx context.Context, key string, defaultVal int64) int64 {
v, err := c.Int64(ctx, key)
if err != nil {
return defaultval
return defaultVal
}
return v
}
func (c *fakeConfigContainer) Bool(key string) (bool, error) {
func (c *fakeConfigContainer) Bool(ctx context.Context, key string) (bool, error) {
return ParseBool(c.getData(key))
}
func (c *fakeConfigContainer) DefaultBool(key string, defaultval bool) bool {
v, err := c.Bool(key)
func (c *fakeConfigContainer) DefaultBool(ctx context.Context, key string, defaultVal bool) bool {
v, err := c.Bool(ctx, key)
if err != nil {
return defaultval
return defaultVal
}
return v
}
func (c *fakeConfigContainer) Float(key string) (float64, error) {
func (c *fakeConfigContainer) Float(ctx context.Context, key string) (float64, error) {
return strconv.ParseFloat(c.getData(key), 64)
}
func (c *fakeConfigContainer) DefaultFloat(key string, defaultval float64) float64 {
v, err := c.Float(key)
func (c *fakeConfigContainer) DefaultFloat(ctx context.Context, key string, defaultVal float64) float64 {
v, err := c.Float(ctx, key)
if err != nil {
return defaultval
return defaultVal
}
return v
}
func (c *fakeConfigContainer) DIY(key string) (interface{}, error) {
func (c *fakeConfigContainer) DIY(ctx context.Context, 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(section string) (map[string]string, error) {
func (c *fakeConfigContainer) GetSection(ctx context.Context, section string) (map[string]string, error) {
return nil, errors.New("not implement in the fakeConfigContainer")
}
func (c *fakeConfigContainer) SaveConfigFile(filename string) error {
func (c *fakeConfigContainer) SaveConfigFile(ctx context.Context, filename string) error {
return errors.New("not implement in the fakeConfigContainer")
}

View File

@ -17,6 +17,7 @@ package config
import (
"bufio"
"bytes"
"context"
"errors"
"io"
"io/ioutil"
@ -233,84 +234,84 @@ type IniConfigContainer struct {
}
// Bool returns the boolean value for a given key.
func (c *IniConfigContainer) Bool(key string) (bool, error) {
func (c *IniConfigContainer) Bool(ctx context.Context, 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(key string, defaultval bool) bool {
v, err := c.Bool(key)
// if err != nil return defaultVal
func (c *IniConfigContainer) DefaultBool(ctx context.Context, key string, defaultVal bool) bool {
v, err := c.Bool(ctx, key)
if err != nil {
return defaultval
return defaultVal
}
return v
}
// Int returns the integer value for a given key.
func (c *IniConfigContainer) Int(key string) (int, error) {
func (c *IniConfigContainer) Int(ctx context.Context, 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(key string, defaultval int) int {
v, err := c.Int(key)
// if err != nil return defaultVal
func (c *IniConfigContainer) DefaultInt(ctx context.Context, key string, defaultVal int) int {
v, err := c.Int(ctx, key)
if err != nil {
return defaultval
return defaultVal
}
return v
}
// Int64 returns the int64 value for a given key.
func (c *IniConfigContainer) Int64(key string) (int64, error) {
func (c *IniConfigContainer) Int64(ctx context.Context, 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(key string, defaultval int64) int64 {
v, err := c.Int64(key)
// if err != nil return defaultVal
func (c *IniConfigContainer) DefaultInt64(ctx context.Context, key string, defaultVal int64) int64 {
v, err := c.Int64(ctx, key)
if err != nil {
return defaultval
return defaultVal
}
return v
}
// Float returns the float value for a given key.
func (c *IniConfigContainer) Float(key string) (float64, error) {
func (c *IniConfigContainer) Float(ctx context.Context, 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(key string, defaultval float64) float64 {
v, err := c.Float(key)
// if err != nil return defaultVal
func (c *IniConfigContainer) DefaultFloat(ctx context.Context, key string, defaultVal float64) float64 {
v, err := c.Float(ctx, key)
if err != nil {
return defaultval
return defaultVal
}
return v
}
// String returns the string value for a given key.
func (c *IniConfigContainer) String(key string) (string, error) {
func (c *IniConfigContainer) String(ctx context.Context, 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(key string, defaultval string) string {
v, err := c.String(key)
// if err != nil return defaultVal
func (c *IniConfigContainer) DefaultString(ctx context.Context, key string, defaultVal string) string {
v, err := c.String(nil, key)
if v == "" || err != nil {
return defaultval
return defaultVal
}
return v
}
// Strings returns the []string value for a given key.
// Return nil if config value does not exist or is empty.
func (c *IniConfigContainer) Strings(key string) ([]string, error) {
v, err := c.String(key)
func (c *IniConfigContainer) Strings(ctx context.Context, key string) ([]string, error) {
v, err := c.String(nil, key)
if v == "" || err != nil {
return nil, err
}
@ -318,17 +319,17 @@ func (c *IniConfigContainer) Strings(key string) ([]string, error) {
}
// DefaultStrings returns the []string value for a given key.
// if err != nil return defaultval
func (c *IniConfigContainer) DefaultStrings(key string, defaultval []string) []string {
v, err := c.Strings(key)
// if err != nil return defaultVal
func (c *IniConfigContainer) DefaultStrings(ctx context.Context, key string, defaultVal []string) []string {
v, err := c.Strings(ctx, key)
if v == nil || err != nil {
return defaultval
return defaultVal
}
return v
}
// GetSection returns map for the given section
func (c *IniConfigContainer) GetSection(section string) (map[string]string, error) {
func (c *IniConfigContainer) GetSection(ctx context.Context, section string) (map[string]string, error) {
if v, ok := c.data[section]; ok {
return v, nil
}
@ -338,7 +339,7 @@ func (c *IniConfigContainer) GetSection(section string) (map[string]string, erro
// 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(filename string) (err error) {
func (c *IniConfigContainer) SaveConfigFile(ctx context.Context, filename string) (err error) {
// Write configuration file by filename.
f, err := os.Create(filename)
if err != nil {
@ -438,7 +439,7 @@ func (c *IniConfigContainer) SaveConfigFile(filename string) (err error) {
// 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(key, value string) error {
func (c *IniConfigContainer) Set(ctx context.Context, key, val string) error {
c.Lock()
defer c.Unlock()
if len(key) == 0 {
@ -461,12 +462,12 @@ func (c *IniConfigContainer) Set(key, value string) error {
if _, ok := c.data[section]; !ok {
c.data[section] = make(map[string]string)
}
c.data[section][k] = value
c.data[section][k] = val
return nil
}
// DIY returns the raw value by a given key.
func (c *IniConfigContainer) DIY(key string) (v interface{}, err error) {
func (c *IniConfigContainer) DIY(ctx context.Context, key string) (v interface{}, err error) {
if v, ok := c.data[strings.ToLower(key)]; ok {
return v, nil
}

View File

@ -101,19 +101,19 @@ password = ${GOPATH}
var value interface{}
switch v.(type) {
case int:
value, err = iniconf.Int(k)
value, err = iniconf.Int(nil, k)
case int64:
value, err = iniconf.Int64(k)
value, err = iniconf.Int64(nil, k)
case float64:
value, err = iniconf.Float(k)
value, err = iniconf.Float(nil, k)
case bool:
value, err = iniconf.Bool(k)
value, err = iniconf.Bool(nil, k)
case []string:
value, err = iniconf.Strings(k)
value, err = iniconf.Strings(nil, k)
case string:
value, err = iniconf.String(k)
value, err = iniconf.String(nil, k)
default:
value, err = iniconf.DIY(k)
value, err = iniconf.DIY(nil, 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("name", "astaxie"); err != nil {
if err = iniconf.Set(nil, "name", "astaxie"); err != nil {
t.Fatal(err)
}
res, _ := iniconf.String("name")
res, _ := iniconf.String(nil, "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(name); err != nil {
if err := cfg.SaveConfigFile(nil, name); err != nil {
t.Fatal(err)
}
defer os.Remove(name)

View File

@ -15,6 +15,7 @@
package json
import (
"context"
"encoding/json"
"errors"
"fmt"
@ -75,7 +76,7 @@ type JSONConfigContainer struct {
}
// Bool returns the boolean value for a given key.
func (c *JSONConfigContainer) Bool(key string) (bool, error) {
func (c *JSONConfigContainer) Bool(ctx context.Context, key string) (bool, error) {
val := c.getData(key)
if val != nil {
return config.ParseBool(val)
@ -85,15 +86,15 @@ func (c *JSONConfigContainer) Bool(key string) (bool, error) {
// DefaultBool return the bool value if has no error
// otherwise return the defaultval
func (c *JSONConfigContainer) DefaultBool(key string, defaultval bool) bool {
if v, err := c.Bool(key); err == nil {
func (c *JSONConfigContainer) DefaultBool(ctx context.Context, key string, defaultVal bool) bool {
if v, err := c.Bool(ctx, key); err == nil {
return v
}
return defaultval
return defaultVal
}
// Int returns the integer value for a given key.
func (c *JSONConfigContainer) Int(key string) (int, error) {
func (c *JSONConfigContainer) Int(ctx context.Context, key string) (int, error) {
val := c.getData(key)
if val != nil {
if v, ok := val.(float64); ok {
@ -108,15 +109,15 @@ func (c *JSONConfigContainer) Int(key string) (int, error) {
// DefaultInt returns the integer value for a given key.
// if err != nil return defaultval
func (c *JSONConfigContainer) DefaultInt(key string, defaultval int) int {
if v, err := c.Int(key); err == nil {
func (c *JSONConfigContainer) DefaultInt(ctx context.Context, key string, defaultVal int) int {
if v, err := c.Int(ctx, key); err == nil {
return v
}
return defaultval
return defaultVal
}
// Int64 returns the int64 value for a given key.
func (c *JSONConfigContainer) Int64(key string) (int64, error) {
func (c *JSONConfigContainer) Int64(ctx context.Context, key string) (int64, error) {
val := c.getData(key)
if val != nil {
if v, ok := val.(float64); ok {
@ -129,15 +130,15 @@ func (c *JSONConfigContainer) Int64(key string) (int64, error) {
// DefaultInt64 returns the int64 value for a given key.
// if err != nil return defaultval
func (c *JSONConfigContainer) DefaultInt64(key string, defaultval int64) int64 {
if v, err := c.Int64(key); err == nil {
func (c *JSONConfigContainer) DefaultInt64(ctx context.Context, key string, defaultVal int64) int64 {
if v, err := c.Int64(ctx, key); err == nil {
return v
}
return defaultval
return defaultVal
}
// Float returns the float value for a given key.
func (c *JSONConfigContainer) Float(key string) (float64, error) {
func (c *JSONConfigContainer) Float(ctx context.Context, key string) (float64, error) {
val := c.getData(key)
if val != nil {
if v, ok := val.(float64); ok {
@ -150,15 +151,15 @@ func (c *JSONConfigContainer) Float(key string) (float64, error) {
// DefaultFloat returns the float64 value for a given key.
// if err != nil return defaultval
func (c *JSONConfigContainer) DefaultFloat(key string, defaultval float64) float64 {
if v, err := c.Float(key); err == nil {
func (c *JSONConfigContainer) DefaultFloat(ctx context.Context, key string, defaultVal float64) float64 {
if v, err := c.Float(ctx, key); err == nil {
return v
}
return defaultval
return defaultVal
}
// String returns the string value for a given key.
func (c *JSONConfigContainer) String(key string) (string, error) {
func (c *JSONConfigContainer) String(ctx context.Context, key string) (string, error) {
val := c.getData(key)
if val != nil {
if v, ok := val.(string); ok {
@ -170,17 +171,17 @@ func (c *JSONConfigContainer) String(key string) (string, error) {
// DefaultString returns the string value for a given key.
// if err != nil return defaultval
func (c *JSONConfigContainer) DefaultString(key string, defaultval string) string {
func (c *JSONConfigContainer) DefaultString(ctx context.Context, key string, defaultVal string) string {
// TODO FIXME should not use "" to replace non existence
if v, err := c.String(key); v != "" && err == nil {
if v, err := c.String(ctx, key); v != "" && err == nil {
return v
}
return defaultval
return defaultVal
}
// Strings returns the []string value for a given key.
func (c *JSONConfigContainer) Strings(key string) ([]string, error) {
stringVal, err := c.String(key)
func (c *JSONConfigContainer) Strings(ctx context.Context, key string) ([]string, error) {
stringVal, err := c.String(nil, key)
if stringVal == "" || err != nil {
return nil, err
}
@ -189,15 +190,15 @@ func (c *JSONConfigContainer) Strings(key string) ([]string, error) {
// DefaultStrings returns the []string value for a given key.
// if err != nil return defaultval
func (c *JSONConfigContainer) DefaultStrings(key string, defaultval []string) []string {
if v, err := c.Strings(key); v != nil && err == nil {
func (c *JSONConfigContainer) DefaultStrings(ctx context.Context, key string, defaultVal []string) []string {
if v, err := c.Strings(ctx, key); v != nil && err == nil {
return v
}
return defaultval
return defaultVal
}
// GetSection returns map for the given section
func (c *JSONConfigContainer) GetSection(section string) (map[string]string, error) {
func (c *JSONConfigContainer) GetSection(ctx context.Context, section string) (map[string]string, error) {
if v, ok := c.data[section]; ok {
return v.(map[string]string), nil
}
@ -205,7 +206,7 @@ func (c *JSONConfigContainer) GetSection(section string) (map[string]string, err
}
// SaveConfigFile save the config into file
func (c *JSONConfigContainer) SaveConfigFile(filename string) (err error) {
func (c *JSONConfigContainer) SaveConfigFile(ctx context.Context, filename string) (err error) {
// Write configuration file by filename.
f, err := os.Create(filename)
if err != nil {
@ -221,7 +222,7 @@ func (c *JSONConfigContainer) SaveConfigFile(filename string) (err error) {
}
// Set writes a new value for key.
func (c *JSONConfigContainer) Set(key, val string) error {
func (c *JSONConfigContainer) Set(ctx context.Context, key, val string) error {
c.Lock()
defer c.Unlock()
c.data[key] = val
@ -229,7 +230,7 @@ func (c *JSONConfigContainer) Set(key, val string) error {
}
// DIY returns the raw value by a given key.
func (c *JSONConfigContainer) DIY(key string) (v interface{}, err error) {
func (c *JSONConfigContainer) DIY(ctx context.Context, key string) (v interface{}, err error) {
val := c.getData(key)
if val != nil {
return val, nil

View File

@ -49,7 +49,7 @@ func TestJsonStartsWithArray(t *testing.T) {
if err != nil {
t.Fatal(err)
}
rootArray, err := jsonconf.DIY("rootArray")
rootArray, err := jsonconf.DIY(nil, "rootArray")
if err != nil {
t.Error("array does not exist as element")
}
@ -155,19 +155,19 @@ func TestJson(t *testing.T) {
var value interface{}
switch v.(type) {
case int:
value, err = jsonconf.Int(k)
value, err = jsonconf.Int(nil, k)
case int64:
value, err = jsonconf.Int64(k)
value, err = jsonconf.Int64(nil, k)
case float64:
value, err = jsonconf.Float(k)
value, err = jsonconf.Float(nil, k)
case bool:
value, err = jsonconf.Bool(k)
value, err = jsonconf.Bool(nil, k)
case []string:
value, err = jsonconf.Strings(k)
value, err = jsonconf.Strings(nil, k)
case string:
value, err = jsonconf.String(k)
value, err = jsonconf.String(nil, k)
default:
value, err = jsonconf.DIY(k)
value, err = jsonconf.DIY(nil, k)
}
if err != nil {
t.Fatalf("get key %q value fatal,%v err %s", k, v, err)
@ -176,16 +176,16 @@ func TestJson(t *testing.T) {
}
}
if err = jsonconf.Set("name", "astaxie"); err != nil {
if err = jsonconf.Set(nil, "name", "astaxie"); err != nil {
t.Fatal(err)
}
res, _ := jsonconf.String("name")
res, _ := jsonconf.String(nil, "name")
if res != "astaxie" {
t.Fatal("get name error")
}
if db, err := jsonconf.DIY("database"); err != nil {
if db, err := jsonconf.DIY(nil, "database"); err != nil {
t.Fatal(err)
} else if m, ok := db.(map[string]interface{}); !ok {
t.Log(db)
@ -196,31 +196,31 @@ func TestJson(t *testing.T) {
}
}
if _, err := jsonconf.Int("unknown"); err == nil {
if _, err := jsonconf.Int(nil, "unknown"); err == nil {
t.Error("unknown keys should return an error when expecting an Int")
}
if _, err := jsonconf.Int64("unknown"); err == nil {
if _, err := jsonconf.Int64(nil, "unknown"); err == nil {
t.Error("unknown keys should return an error when expecting an Int64")
}
if _, err := jsonconf.Float("unknown"); err == nil {
if _, err := jsonconf.Float(nil, "unknown"); err == nil {
t.Error("unknown keys should return an error when expecting a Float")
}
if _, err := jsonconf.DIY("unknown"); err == nil {
if _, err := jsonconf.DIY(nil, "unknown"); err == nil {
t.Error("unknown keys should return an error when expecting an interface{}")
}
if val, _ := jsonconf.String("unknown"); val != "" {
if val, _ := jsonconf.String(nil, "unknown"); val != "" {
t.Error("unknown keys should return an empty string when expecting a String")
}
if _, err := jsonconf.Bool("unknown"); err == nil {
if _, err := jsonconf.Bool(nil, "unknown"); err == nil {
t.Error("unknown keys should return an error when expecting a Bool")
}
if !jsonconf.DefaultBool("unknown", true) {
if !jsonconf.DefaultBool(nil, "unknown", true) {
t.Error("unknown keys with default value wrong")
}
}

View File

@ -30,6 +30,7 @@
package xml
import (
"context"
"encoding/xml"
"errors"
"fmt"
@ -80,7 +81,7 @@ type ConfigContainer struct {
}
// Bool returns the boolean value for a given key.
func (c *ConfigContainer) Bool(key string) (bool, error) {
func (c *ConfigContainer) Bool(ctx context.Context, key string) (bool, error) {
if v := c.data[key]; v != nil {
return config.ParseBool(v)
}
@ -88,63 +89,63 @@ func (c *ConfigContainer) Bool(key string) (bool, error) {
}
// DefaultBool return the bool value if has no error
// otherwise return the defaultval
func (c *ConfigContainer) DefaultBool(key string, defaultval bool) bool {
v, err := c.Bool(key)
// otherwise return the defaultVal
func (c *ConfigContainer) DefaultBool(ctx context.Context, key string, defaultVal bool) bool {
v, err := c.Bool(ctx, key)
if err != nil {
return defaultval
return defaultVal
}
return v
}
// Int returns the integer value for a given key.
func (c *ConfigContainer) Int(key string) (int, error) {
func (c *ConfigContainer) Int(ctx context.Context, key string) (int, error) {
return strconv.Atoi(c.data[key].(string))
}
// DefaultInt returns the integer value for a given key.
// if err != nil return defaultval
func (c *ConfigContainer) DefaultInt(key string, defaultval int) int {
v, err := c.Int(key)
// if err != nil return defaultVal
func (c *ConfigContainer) DefaultInt(ctx context.Context, key string, defaultVal int) int {
v, err := c.Int(ctx, key)
if err != nil {
return defaultval
return defaultVal
}
return v
}
// Int64 returns the int64 value for a given key.
func (c *ConfigContainer) Int64(key string) (int64, error) {
func (c *ConfigContainer) Int64(ctx context.Context, key string) (int64, error) {
return strconv.ParseInt(c.data[key].(string), 10, 64)
}
// DefaultInt64 returns the int64 value for a given key.
// if err != nil return defaultval
func (c *ConfigContainer) DefaultInt64(key string, defaultval int64) int64 {
v, err := c.Int64(key)
// if err != nil return defaultVal
func (c *ConfigContainer) DefaultInt64(ctx context.Context, key string, defaultVal int64) int64 {
v, err := c.Int64(ctx, key)
if err != nil {
return defaultval
return defaultVal
}
return v
}
// Float returns the float value for a given key.
func (c *ConfigContainer) Float(key string) (float64, error) {
func (c *ConfigContainer) Float(ctx context.Context, key string) (float64, error) {
return strconv.ParseFloat(c.data[key].(string), 64)
}
// DefaultFloat returns the float64 value for a given key.
// if err != nil return defaultval
func (c *ConfigContainer) DefaultFloat(key string, defaultval float64) float64 {
v, err := c.Float(key)
// if err != nil return defaultVal
func (c *ConfigContainer) DefaultFloat(ctx context.Context, key string, defaultVal float64) float64 {
v, err := c.Float(ctx, key)
if err != nil {
return defaultval
return defaultVal
}
return v
}
// String returns the string value for a given key.
func (c *ConfigContainer) String(key string) (string, error) {
func (c *ConfigContainer) String(ctx context.Context, key string) (string, error) {
if v, ok := c.data[key].(string); ok {
return v, nil
}
@ -152,18 +153,18 @@ func (c *ConfigContainer) String(key string) (string, error) {
}
// DefaultString returns the string value for a given key.
// if err != nil return defaultval
func (c *ConfigContainer) DefaultString(key string, defaultval string) string {
v, err := c.String(key)
// if err != nil return defaultVal
func (c *ConfigContainer) DefaultString(ctx context.Context, key string, defaultVal string) string {
v, err := c.String(nil, key)
if v == "" || err != nil {
return defaultval
return defaultVal
}
return v
}
// Strings returns the []string value for a given key.
func (c *ConfigContainer) Strings(key string) ([]string, error) {
v, err := c.String(key)
func (c *ConfigContainer) Strings(ctx context.Context, key string) ([]string, error) {
v, err := c.String(ctx, key)
if v == "" || err != nil {
return nil, err
}
@ -171,17 +172,17 @@ func (c *ConfigContainer) Strings(key string) ([]string, error) {
}
// DefaultStrings returns the []string value for a given key.
// if err != nil return defaultval
func (c *ConfigContainer) DefaultStrings(key string, defaultval []string) []string {
v, err := c.Strings(key)
// if err != nil return defaultVal
func (c *ConfigContainer) DefaultStrings(ctx context.Context, key string, defaultVal []string) []string {
v, err := c.Strings(ctx, key)
if v == nil || err != nil {
return defaultval
return defaultVal
}
return v
}
// GetSection returns map for the given section
func (c *ConfigContainer) GetSection(section string) (map[string]string, error) {
func (c *ConfigContainer) GetSection(ctx context.Context, section string) (map[string]string, error) {
if v, ok := c.data[section].(map[string]interface{}); ok {
mapstr := make(map[string]string)
for k, val := range v {
@ -193,7 +194,7 @@ func (c *ConfigContainer) GetSection(section string) (map[string]string, error)
}
// SaveConfigFile save the config into file
func (c *ConfigContainer) SaveConfigFile(filename string) (err error) {
func (c *ConfigContainer) SaveConfigFile(ctx context.Context, filename string) (err error) {
// Write configuration file by filename.
f, err := os.Create(filename)
if err != nil {
@ -209,7 +210,7 @@ func (c *ConfigContainer) SaveConfigFile(filename string) (err error) {
}
// Set writes a new value for key.
func (c *ConfigContainer) Set(key, val string) error {
func (c *ConfigContainer) Set(ctx context.Context, key, val string) error {
c.Lock()
defer c.Unlock()
c.data[key] = val
@ -217,7 +218,7 @@ func (c *ConfigContainer) Set(key, val string) error {
}
// DIY returns the raw value by a given key.
func (c *ConfigContainer) DIY(key string) (v interface{}, err error) {
func (c *ConfigContainer) DIY(ctx context.Context, key string) (v interface{}, err error) {
if v, ok := c.data[key]; ok {
return v, nil
}

View File

@ -76,7 +76,7 @@ func TestXML(t *testing.T) {
}
var xmlsection map[string]string
xmlsection, err = xmlconf.GetSection("mysection")
xmlsection, err = xmlconf.GetSection(nil, "mysection")
if err != nil {
t.Fatal(err)
}
@ -94,19 +94,19 @@ func TestXML(t *testing.T) {
switch v.(type) {
case int:
value, err = xmlconf.Int(k)
value, err = xmlconf.Int(nil, k)
case int64:
value, err = xmlconf.Int64(k)
value, err = xmlconf.Int64(nil, k)
case float64:
value, err = xmlconf.Float(k)
value, err = xmlconf.Float(nil, k)
case bool:
value, err = xmlconf.Bool(k)
value, err = xmlconf.Bool(nil, k)
case []string:
value, err = xmlconf.Strings(k)
value, err = xmlconf.Strings(nil, k)
case string:
value, err = xmlconf.String(k)
value, err = xmlconf.String(nil, k)
default:
value, err = xmlconf.DIY(k)
value, err = xmlconf.DIY(nil, k)
}
if err != nil {
t.Errorf("get key %q value fatal,%v err %s", k, v, err)
@ -116,11 +116,11 @@ func TestXML(t *testing.T) {
}
if err = xmlconf.Set("name", "astaxie"); err != nil {
if err = xmlconf.Set(nil, "name", "astaxie"); err != nil {
t.Fatal(err)
}
res, _ := xmlconf.String("name")
res, _ := xmlconf.String(nil, "name")
if res != "astaxie" {
t.Fatal("get name error")
}

View File

@ -31,6 +31,7 @@ package yaml
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
@ -125,7 +126,7 @@ type ConfigContainer struct {
}
// Bool returns the boolean value for a given key.
func (c *ConfigContainer) Bool(key string) (bool, error) {
func (c *ConfigContainer) Bool(ctx context.Context, key string) (bool, error) {
v, err := c.getData(key)
if err != nil {
return false, err
@ -134,17 +135,17 @@ func (c *ConfigContainer) Bool(key string) (bool, error) {
}
// DefaultBool return the bool value if has no error
// otherwise return the defaultval
func (c *ConfigContainer) DefaultBool(key string, defaultval bool) bool {
v, err := c.Bool(key)
// otherwise return the defaultVal
func (c *ConfigContainer) DefaultBool(ctx context.Context, key string, defaultVal bool) bool {
v, err := c.Bool(ctx, key)
if err != nil {
return defaultval
return defaultVal
}
return v
}
// Int returns the integer value for a given key.
func (c *ConfigContainer) Int(key string) (int, error) {
func (c *ConfigContainer) Int(ctx context.Context, key string) (int, error) {
if v, err := c.getData(key); err != nil {
return 0, err
} else if vv, ok := v.(int); ok {
@ -156,17 +157,17 @@ func (c *ConfigContainer) Int(key string) (int, error) {
}
// DefaultInt returns the integer value for a given key.
// if err != nil return defaultval
func (c *ConfigContainer) DefaultInt(key string, defaultval int) int {
v, err := c.Int(key)
// if err != nil return defaultVal
func (c *ConfigContainer) DefaultInt(ctx context.Context, key string, defaultVal int) int {
v, err := c.Int(ctx, key)
if err != nil {
return defaultval
return defaultVal
}
return v
}
// Int64 returns the int64 value for a given key.
func (c *ConfigContainer) Int64(key string) (int64, error) {
func (c *ConfigContainer) Int64(ctx context.Context, key string) (int64, error) {
if v, err := c.getData(key); err != nil {
return 0, err
} else if vv, ok := v.(int64); ok {
@ -176,17 +177,17 @@ func (c *ConfigContainer) Int64(key string) (int64, error) {
}
// DefaultInt64 returns the int64 value for a given key.
// if err != nil return defaultval
func (c *ConfigContainer) DefaultInt64(key string, defaultval int64) int64 {
v, err := c.Int64(key)
// if err != nil return defaultVal
func (c *ConfigContainer) DefaultInt64(ctx context.Context, key string, defaultVal int64) int64 {
v, err := c.Int64(ctx, key)
if err != nil {
return defaultval
return defaultVal
}
return v
}
// Float returns the float value for a given key.
func (c *ConfigContainer) Float(key string) (float64, error) {
func (c *ConfigContainer) Float(ctx context.Context, key string) (float64, error) {
if v, err := c.getData(key); err != nil {
return 0.0, err
} else if vv, ok := v.(float64); ok {
@ -200,17 +201,17 @@ func (c *ConfigContainer) Float(key string) (float64, error) {
}
// DefaultFloat returns the float64 value for a given key.
// if err != nil return defaultval
func (c *ConfigContainer) DefaultFloat(key string, defaultval float64) float64 {
v, err := c.Float(key)
// if err != nil return defaultVal
func (c *ConfigContainer) DefaultFloat(ctx context.Context, key string, defaultVal float64) float64 {
v, err := c.Float(ctx, key)
if err != nil {
return defaultval
return defaultVal
}
return v
}
// String returns the string value for a given key.
func (c *ConfigContainer) String(key string) (string, error) {
func (c *ConfigContainer) String(ctx context.Context, key string) (string, error) {
if v, err := c.getData(key); err == nil {
if vv, ok := v.(string); ok {
return vv, nil
@ -220,18 +221,18 @@ func (c *ConfigContainer) String(key string) (string, error) {
}
// DefaultString returns the string value for a given key.
// if err != nil return defaultval
func (c *ConfigContainer) DefaultString(key string, defaultval string) string {
v, err := c.String(key)
// if err != nil return defaultVal
func (c *ConfigContainer) DefaultString(ctx context.Context, key string, defaultVal string) string {
v, err := c.String(nil, key)
if v == "" || err != nil {
return defaultval
return defaultVal
}
return v
}
// Strings returns the []string value for a given key.
func (c *ConfigContainer) Strings(key string) ([]string, error) {
v, err := c.String(key)
func (c *ConfigContainer) Strings(ctx context.Context, key string) ([]string, error) {
v, err := c.String(nil, key)
if v == "" || err != nil {
return nil, err
}
@ -239,17 +240,17 @@ func (c *ConfigContainer) Strings(key string) ([]string, error) {
}
// DefaultStrings returns the []string value for a given key.
// if err != nil return defaultval
func (c *ConfigContainer) DefaultStrings(key string, defaultval []string) []string {
v, err := c.Strings(key)
// if err != nil return defaultVal
func (c *ConfigContainer) DefaultStrings(ctx context.Context, key string, defaultVal []string) []string {
v, err := c.Strings(ctx, key)
if v == nil || err != nil {
return defaultval
return defaultVal
}
return v
}
// GetSection returns map for the given section
func (c *ConfigContainer) GetSection(section string) (map[string]string, error) {
func (c *ConfigContainer) GetSection(ctx context.Context, section string) (map[string]string, error) {
if v, ok := c.data[section]; ok {
return v.(map[string]string), nil
@ -258,7 +259,7 @@ func (c *ConfigContainer) GetSection(section string) (map[string]string, error)
}
// SaveConfigFile save the config into file
func (c *ConfigContainer) SaveConfigFile(filename string) (err error) {
func (c *ConfigContainer) SaveConfigFile(ctx context.Context, filename string) (err error) {
// Write configuration file by filename.
f, err := os.Create(filename)
if err != nil {
@ -270,7 +271,7 @@ func (c *ConfigContainer) SaveConfigFile(filename string) (err error) {
}
// Set writes a new value for key.
func (c *ConfigContainer) Set(key, val string) error {
func (c *ConfigContainer) Set(ctx context.Context, key, val string) error {
c.Lock()
defer c.Unlock()
c.data[key] = val
@ -278,7 +279,7 @@ func (c *ConfigContainer) Set(key, val string) error {
}
// DIY returns the raw value by a given key.
func (c *ConfigContainer) DIY(key string) (v interface{}, err error) {
func (c *ConfigContainer) DIY(ctx context.Context, key string) (v interface{}, err error) {
return c.getData(key)
}

View File

@ -70,7 +70,7 @@ func TestYaml(t *testing.T) {
t.Fatal(err)
}
res, _ := yamlconf.String("appname")
res, _ := yamlconf.String(nil, "appname")
if res != "beeapi" {
t.Fatal("appname not equal to beeapi")
}
@ -84,19 +84,19 @@ func TestYaml(t *testing.T) {
switch v.(type) {
case int:
value, err = yamlconf.Int(k)
value, err = yamlconf.Int(nil, k)
case int64:
value, err = yamlconf.Int64(k)
value, err = yamlconf.Int64(nil, k)
case float64:
value, err = yamlconf.Float(k)
value, err = yamlconf.Float(nil, k)
case bool:
value, err = yamlconf.Bool(k)
value, err = yamlconf.Bool(nil, k)
case []string:
value, err = yamlconf.Strings(k)
value, err = yamlconf.Strings(nil, k)
case string:
value, err = yamlconf.String(k)
value, err = yamlconf.String(nil, k)
default:
value, err = yamlconf.DIY(k)
value, err = yamlconf.DIY(nil, k)
}
if err != nil {
t.Errorf("get key %q value fatal,%v err %s", k, v, err)
@ -106,10 +106,10 @@ func TestYaml(t *testing.T) {
}
if err = yamlconf.Set("name", "astaxie"); err != nil {
if err = yamlconf.Set(nil, "name", "astaxie"); err != nil {
t.Fatal(err)
}
res, _ = yamlconf.String("name")
res, _ = yamlconf.String(nil, "name")
if res != "astaxie" {
t.Fatal("get name error")
}

View File

@ -15,6 +15,7 @@
package web
import (
context2 "context"
"fmt"
"os"
"path/filepath"
@ -292,11 +293,11 @@ func assignConfig(ac config.Configer) error {
// set the run mode first
if envRunMode := os.Getenv("BEEGO_RUNMODE"); envRunMode != "" {
BConfig.RunMode = envRunMode
} else if runMode, err := ac.String("RunMode"); runMode != "" && err == nil {
} else if runMode, err := ac.String(nil, "RunMode"); runMode != "" && err == nil {
BConfig.RunMode = runMode
}
if sd, err := ac.String("StaticDir"); sd != "" && err == nil {
if sd, err := ac.String(nil, "StaticDir"); sd != "" && err == nil {
BConfig.WebConfig.StaticDir = map[string]string{}
sds := strings.Fields(sd)
for _, v := range sds {
@ -308,7 +309,7 @@ func assignConfig(ac config.Configer) error {
}
}
if sgz, err := ac.String("StaticExtensionsToGzip"); sgz != "" && err == nil {
if sgz, err := ac.String(nil, "StaticExtensionsToGzip"); sgz != "" && err == nil {
extensions := strings.Split(sgz, ",")
fileExts := []string{}
for _, ext := range extensions {
@ -326,15 +327,15 @@ func assignConfig(ac config.Configer) error {
}
}
if sfs, err := ac.Int("StaticCacheFileSize"); err == nil {
if sfs, err := ac.Int(nil, "StaticCacheFileSize"); err == nil {
BConfig.WebConfig.StaticCacheFileSize = sfs
}
if sfn, err := ac.Int("StaticCacheFileNum"); err == nil {
if sfn, err := ac.Int(nil, "StaticCacheFileNum"); err == nil {
BConfig.WebConfig.StaticCacheFileNum = sfn
}
if lo, err := ac.String("LogOutputs"); lo != "" && err == nil {
if lo, err := ac.String(nil, "LogOutputs"); lo != "" && err == nil {
// if lo is not nil or empty
// means user has set his own LogOutputs
// clear the default setting to BConfig.Log.Outputs
@ -381,11 +382,11 @@ func assignSingleConfig(p interface{}, ac config.Configer) {
name := pt.Field(i).Name
switch pf.Kind() {
case reflect.String:
pf.SetString(ac.DefaultString(name, pf.String()))
pf.SetString(ac.DefaultString(nil, name, pf.String()))
case reflect.Int, reflect.Int64:
pf.SetInt(ac.DefaultInt64(name, pf.Int()))
pf.SetInt(ac.DefaultInt64(nil, name, pf.Int()))
case reflect.Bool:
pf.SetBool(ac.DefaultBool(name, pf.Bool()))
pf.SetBool(ac.DefaultBool(nil, name, pf.Bool()))
case reflect.Struct:
default:
// do nothing here
@ -424,105 +425,105 @@ func newAppConfig(appConfigProvider, appConfigPath string) (*beegoAppConfig, err
return &beegoAppConfig{innerConfig: ac}, nil
}
func (b *beegoAppConfig) Set(key, val string) error {
if err := b.innerConfig.Set(BConfig.RunMode+"::"+key, val); err != nil {
return b.innerConfig.Set(key, val)
func (b *beegoAppConfig) Set(ctx context2.Context, key, val string) error {
if err := b.innerConfig.Set(nil, BConfig.RunMode+"::"+key, val); err != nil {
return b.innerConfig.Set(nil, key, val)
}
return nil
}
func (b *beegoAppConfig) String(key string) (string, error) {
if v, err := b.innerConfig.String(BConfig.RunMode + "::" + key); v != "" && err == nil {
func (b *beegoAppConfig) String(ctx context2.Context, key string) (string, error) {
if v, err := b.innerConfig.String(nil, BConfig.RunMode+"::"+key); v != "" && err == nil {
return v, nil
}
return b.innerConfig.String(key)
return b.innerConfig.String(nil, key)
}
func (b *beegoAppConfig) Strings(key string) ([]string, error) {
if v, err := b.innerConfig.Strings(BConfig.RunMode + "::" + key); len(v) > 0 && err == nil {
func (b *beegoAppConfig) Strings(ctx context2.Context, key string) ([]string, error) {
if v, err := b.innerConfig.Strings(nil, BConfig.RunMode+"::"+key); len(v) > 0 && err == nil {
return v, nil
}
return b.innerConfig.Strings(key)
return b.innerConfig.Strings(nil, key)
}
func (b *beegoAppConfig) Int(key string) (int, error) {
if v, err := b.innerConfig.Int(BConfig.RunMode + "::" + key); err == nil {
func (b *beegoAppConfig) Int(ctx context2.Context, key string) (int, error) {
if v, err := b.innerConfig.Int(nil, BConfig.RunMode+"::"+key); err == nil {
return v, nil
}
return b.innerConfig.Int(key)
return b.innerConfig.Int(nil, key)
}
func (b *beegoAppConfig) Int64(key string) (int64, error) {
if v, err := b.innerConfig.Int64(BConfig.RunMode + "::" + key); err == nil {
func (b *beegoAppConfig) Int64(ctx context2.Context, key string) (int64, error) {
if v, err := b.innerConfig.Int64(nil, BConfig.RunMode+"::"+key); err == nil {
return v, nil
}
return b.innerConfig.Int64(key)
return b.innerConfig.Int64(nil, key)
}
func (b *beegoAppConfig) Bool(key string) (bool, error) {
if v, err := b.innerConfig.Bool(BConfig.RunMode + "::" + key); err == nil {
func (b *beegoAppConfig) Bool(ctx context2.Context, key string) (bool, error) {
if v, err := b.innerConfig.Bool(nil, BConfig.RunMode+"::"+key); err == nil {
return v, nil
}
return b.innerConfig.Bool(key)
return b.innerConfig.Bool(nil, key)
}
func (b *beegoAppConfig) Float(key string) (float64, error) {
if v, err := b.innerConfig.Float(BConfig.RunMode + "::" + key); err == nil {
func (b *beegoAppConfig) Float(ctx context2.Context, key string) (float64, error) {
if v, err := b.innerConfig.Float(nil, BConfig.RunMode+"::"+key); err == nil {
return v, nil
}
return b.innerConfig.Float(key)
return b.innerConfig.Float(nil, key)
}
func (b *beegoAppConfig) DefaultString(key string, defaultVal string) string {
if v, err := b.String(key); v != "" && err == nil {
func (b *beegoAppConfig) DefaultString(ctx context2.Context, key string, defaultVal string) string {
if v, err := b.String(nil, key); v != "" && err == nil {
return v
}
return defaultVal
}
func (b *beegoAppConfig) DefaultStrings(key string, defaultVal []string) []string {
if v, err := b.Strings(key); len(v) != 0 && err == nil {
func (b *beegoAppConfig) DefaultStrings(ctx context2.Context, key string, defaultVal []string) []string {
if v, err := b.Strings(ctx, key); len(v) != 0 && err == nil {
return v
}
return defaultVal
}
func (b *beegoAppConfig) DefaultInt(key string, defaultVal int) int {
if v, err := b.Int(key); err == nil {
func (b *beegoAppConfig) DefaultInt(ctx context2.Context, key string, defaultVal int) int {
if v, err := b.Int(ctx, key); err == nil {
return v
}
return defaultVal
}
func (b *beegoAppConfig) DefaultInt64(key string, defaultVal int64) int64 {
if v, err := b.Int64(key); err == nil {
func (b *beegoAppConfig) DefaultInt64(ctx context2.Context, key string, defaultVal int64) int64 {
if v, err := b.Int64(ctx, key); err == nil {
return v
}
return defaultVal
}
func (b *beegoAppConfig) DefaultBool(key string, defaultVal bool) bool {
if v, err := b.Bool(key); err == nil {
func (b *beegoAppConfig) DefaultBool(ctx context2.Context, key string, defaultVal bool) bool {
if v, err := b.Bool(ctx, key); err == nil {
return v
}
return defaultVal
}
func (b *beegoAppConfig) DefaultFloat(key string, defaultVal float64) float64 {
if v, err := b.Float(key); err == nil {
func (b *beegoAppConfig) DefaultFloat(ctx context2.Context, key string, defaultVal float64) float64 {
if v, err := b.Float(ctx, key); err == nil {
return v
}
return defaultVal
}
func (b *beegoAppConfig) DIY(key string) (interface{}, error) {
return b.innerConfig.DIY(key)
func (b *beegoAppConfig) DIY(ctx context2.Context, key string) (interface{}, error) {
return b.innerConfig.DIY(nil, key)
}
func (b *beegoAppConfig) GetSection(section string) (map[string]string, error) {
return b.innerConfig.GetSection(section)
func (b *beegoAppConfig) GetSection(ctx context2.Context, section string) (map[string]string, error) {
return b.innerConfig.GetSection(nil, section)
}
func (b *beegoAppConfig) SaveConfigFile(filename string) error {
return b.innerConfig.SaveConfigFile(filename)
func (b *beegoAppConfig) SaveConfigFile(ctx context2.Context, filename string) error {
return b.innerConfig.SaveConfigFile(nil, filename)
}

View File

@ -111,12 +111,12 @@ func TestAssignConfig_02(t *testing.T) {
func TestAssignConfig_03(t *testing.T) {
jcf := &beeJson.JSONConfig{}
ac, _ := jcf.ParseData([]byte(`{"AppName":"beego"}`))
ac.Set("AppName", "test_app")
ac.Set("RunMode", "online")
ac.Set("StaticDir", "download:down download2:down2")
ac.Set("StaticExtensionsToGzip", ".css,.js,.html,.jpg,.png")
ac.Set("StaticCacheFileSize", "87456")
ac.Set("StaticCacheFileNum", "1254")
ac.Set(nil, "AppName", "test_app")
ac.Set(nil, "RunMode", "online")
ac.Set(nil, "StaticDir", "download:down download2:down2")
ac.Set(nil, "StaticExtensionsToGzip", ".css,.js,.html,.jpg,.png")
ac.Set(nil, "StaticCacheFileSize", "87456")
ac.Set(nil, "StaticCacheFileNum", "1254")
assignConfig(ac)
t.Logf("%#v", BConfig)

View File

@ -1,6 +1,7 @@
package web
import (
context2 "context"
"encoding/json"
"mime"
"net/http"
@ -48,7 +49,7 @@ func registerDefaultErrorHandler() error {
func registerSession() error {
if BConfig.WebConfig.Session.SessionOn {
var err error
sessionConfig, err := AppConfig.String("sessionConfig")
sessionConfig, err := AppConfig.String(nil, "sessionConfig")
conf := new(session.ManagerConfig)
if sessionConfig == "" || err != nil {
conf.CookieName = BConfig.WebConfig.Session.SessionName
@ -96,9 +97,9 @@ func registerAdmin() error {
func registerGzip() error {
if BConfig.EnableGzip {
context.InitGzip(
AppConfig.DefaultInt("gzipMinLength", -1),
AppConfig.DefaultInt("gzipCompressLevel", -1),
AppConfig.DefaultStrings("includedMethods", []string{"GET"}),
AppConfig.DefaultInt(context2.Background(), "gzipMinLength", -1),
AppConfig.DefaultInt(context2.Background(), "gzipCompressLevel", -1),
AppConfig.DefaultStrings(context2.Background(), "includedMethods", []string{"GET"}),
)
}
return nil

View File

@ -15,6 +15,7 @@
package web
import (
"context"
"encoding/json"
"errors"
"fmt"
@ -516,7 +517,7 @@ func genRouterCode(pkgRealpath string) {
}
defer f.Close()
routersDir := AppConfig.DefaultString("routersdir", "routers")
routersDir := AppConfig.DefaultString(context.Background(), "routersdir", "routers")
content := strings.Replace(globalRouterTemplate, "{{.globalinfo}}", globalinfo, -1)
content = strings.Replace(content, "{{.routersDir}}", routersDir, -1)
content = strings.Replace(content, "{{.globalimport}}", globalimport, -1)
@ -585,7 +586,7 @@ func getpathTime(pkgRealpath string) (lastupdate int64, err error) {
func getRouterDir(pkgRealpath string) string {
dir := filepath.Dir(pkgRealpath)
for {
routersDir := AppConfig.DefaultString("routersdir", "routers")
routersDir := AppConfig.DefaultString(context.Background(), "routersdir", "routers")
d := filepath.Join(dir, routersDir)
if utils.FileExists(d) {
return d

View File

@ -15,6 +15,7 @@
package web
import (
"context"
"errors"
"fmt"
"html"
@ -58,11 +59,11 @@ func HTML2str(html string) string {
re := regexp.MustCompile(`\<[\S\s]+?\>`)
html = re.ReplaceAllStringFunc(html, strings.ToLower)
//remove STYLE
// remove STYLE
re = regexp.MustCompile(`\<style[\S\s]+?\</style\>`)
html = re.ReplaceAllString(html, "")
//remove SCRIPT
// remove SCRIPT
re = regexp.MustCompile(`\<script[\S\s]+?\</script\>`)
html = re.ReplaceAllString(html, "")
@ -85,7 +86,7 @@ func DateFormat(t time.Time, layout string) (datestring string) {
var datePatterns = []string{
// year
"Y", "2006", // A full numeric representation of a year, 4 digits Examples: 1999 or 2003
"y", "06", //A two digit representation of a year Examples: 99 or 03
"y", "06", // A two digit representation of a year Examples: 99 or 03
// month
"m", "01", // Numeric representation of a month, with leading zeros 01 through 12
@ -160,17 +161,17 @@ func NotNil(a interface{}) (isNil bool) {
func GetConfig(returnType, key string, defaultVal interface{}) (value interface{}, err error) {
switch returnType {
case "String":
value, err = AppConfig.String(key)
value, err = AppConfig.String(context.Background(), key)
case "Bool":
value, err = AppConfig.Bool(key)
value, err = AppConfig.Bool(context.Background(), key)
case "Int":
value, err = AppConfig.Int(key)
value, err = AppConfig.Int(context.Background(), key)
case "Int64":
value, err = AppConfig.Int64(key)
value, err = AppConfig.Int64(context.Background(), key)
case "Float":
value, err = AppConfig.Float(key)
value, err = AppConfig.Float(context.Background(), key)
case "DIY":
value, err = AppConfig.DIY(key)
value, err = AppConfig.DIY(context.Background(), key)
default:
err = errors.New("config keys must be of type String, Bool, Int, Int64, Float, or DIY")
}
@ -201,7 +202,7 @@ func Str2html(raw string) template.HTML {
// Htmlquote returns quoted html string.
func Htmlquote(text string) string {
//HTML编码为实体符号
// HTML编码为实体符号
/*
Encodes `text` for raw use in HTML.
>>> htmlquote("<'&\\">")
@ -220,7 +221,7 @@ func Htmlquote(text string) string {
// Htmlunquote returns unquoted html string.
func Htmlunquote(text string) string {
//实体符号解释为HTML
// 实体符号解释为HTML
/*
Decodes `text` that's HTML quoted.
>>> htmlunquote('&lt;&#39;&amp;&quot;&gt;')