1
0
mirror of https://github.com/astaxie/beego.git synced 2025-06-14 07:00:40 +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

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