mirror of
https://github.com/astaxie/beego.git
synced 2024-12-26 00:40:50 +00:00
remove interfaceToStr function to package config
This commit is contained in:
parent
1222c87be3
commit
36f69a04a9
@ -119,6 +119,7 @@ func Getenv(env interface{}) (string, bool) {
|
||||
|
||||
// Onley support string key.
|
||||
if key, ok := env.(string); ok {
|
||||
|
||||
if envKey := strings.TrimPrefix(key, envKeySign); envKey != key {
|
||||
return os.Getenv(envKey), true
|
||||
}
|
||||
@ -126,6 +127,36 @@ func Getenv(env interface{}) (string, bool) {
|
||||
return "", false
|
||||
}
|
||||
|
||||
// ConvertToStringMap convert interface to string config value only for map[string]interface{} config info.
|
||||
func ConvertToStringMap(m map[string]interface{}) map[string]string {
|
||||
items := make(map[string]string, len(m))
|
||||
if m == nil || len(m) == 0 {
|
||||
return items
|
||||
}
|
||||
|
||||
var s string
|
||||
for k, v := range m {
|
||||
s = ""
|
||||
if v == nil {
|
||||
s = ""
|
||||
} else if str, ok := v.(string); ok {
|
||||
s = str
|
||||
} else if m, ok := v.(map[string]interface{}); ok {
|
||||
s = fmt.Sprintf("%+v", ConvertToStringMap(m))
|
||||
} else {
|
||||
s = fmt.Sprintf("%+v", v)
|
||||
}
|
||||
|
||||
if len(s) > 0 {
|
||||
if env, ok := Getenv(s); ok {
|
||||
s = env
|
||||
}
|
||||
}
|
||||
items[k] = s
|
||||
}
|
||||
return items
|
||||
}
|
||||
|
||||
// ParseBool returns the boolean value represented by the string.
|
||||
//
|
||||
// It accepts 1, 1.0, t, T, TRUE, true, True, YES, yes, Yes,Y, y, ON, on, On,
|
||||
|
@ -190,28 +190,7 @@ func (c *ConfigContainer) DefaultStrings(key string, defaultval []string) []stri
|
||||
// GetSection returns map for the given section
|
||||
func (c *ConfigContainer) GetSection(section string) (map[string]string, error) {
|
||||
if v, ok := c.data[section]; ok {
|
||||
|
||||
var interfaceToStr func(map[string]interface{}) map[string]string
|
||||
|
||||
interfaceToStr = func(values map[string]interface{}) map[string]string {
|
||||
strValues := make(map[string]string, len(values))
|
||||
for k, vv := range values {
|
||||
if vv == nil {
|
||||
strValues[k] = ""
|
||||
} else if env, ok := config.Getenv(vv); ok {
|
||||
strValues[k] = env
|
||||
} else if str, ok := vv.(string); ok {
|
||||
strValues[k] = str
|
||||
} else if m, ok := vv.(map[string]interface{}); ok {
|
||||
strValues[k] = fmt.Sprintf("%v", interfaceToStr(m))
|
||||
} else {
|
||||
// TODO: no better.
|
||||
strValues[k] = fmt.Sprintf("%v", vv)
|
||||
}
|
||||
}
|
||||
return strValues
|
||||
}
|
||||
return interfaceToStr(v.(map[string]interface{})), nil
|
||||
return config.ConvertToStringMap(v.(map[string]interface{})), nil
|
||||
}
|
||||
return nil, errors.New("not exist setction")
|
||||
}
|
||||
|
@ -242,29 +242,9 @@ func (c *ConfigContainer) DefaultStrings(key string, defaultval []string) []stri
|
||||
|
||||
// GetSection returns map for the given section
|
||||
func (c *ConfigContainer) GetSection(section string) (map[string]string, error) {
|
||||
v, ok := c.data[section]
|
||||
if ok {
|
||||
var interfaceToStr func(map[string]interface{}) map[string]string
|
||||
|
||||
interfaceToStr = func(values map[string]interface{}) map[string]string {
|
||||
strValues := make(map[string]string, len(values))
|
||||
for k, vv := range values {
|
||||
if vv == nil {
|
||||
strValues[k] = ""
|
||||
} else if env, ok := config.Getenv(vv); ok {
|
||||
strValues[k] = env
|
||||
} else if str, ok := vv.(string); ok {
|
||||
strValues[k] = str
|
||||
} else if m, ok := vv.(map[string]interface{}); ok {
|
||||
strValues[k] = fmt.Sprintf("%v", interfaceToStr(m))
|
||||
} else {
|
||||
// TODO: no better.
|
||||
strValues[k] = fmt.Sprintf("%v", vv)
|
||||
}
|
||||
}
|
||||
return strValues
|
||||
}
|
||||
return interfaceToStr(v.(map[string]interface{})), nil
|
||||
if v, ok := c.data[section]; ok {
|
||||
return config.ConvertToStringMap(v.(map[string]interface{})), nil
|
||||
}
|
||||
return nil, errors.New("not exist setction")
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user