diff --git a/config/config.go b/config/config.go index 7dfca88a..ef7738d9 100644 --- a/config/config.go +++ b/config/config.go @@ -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, diff --git a/config/xml/xml.go b/config/xml/xml.go index d48cfd8e..7222f745 100644 --- a/config/xml/xml.go +++ b/config/xml/xml.go @@ -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") } diff --git a/config/yaml/yaml.go b/config/yaml/yaml.go index a2ec299c..db849949 100644 --- a/config/yaml/yaml.go +++ b/config/yaml/yaml.go @@ -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") }