diff --git a/envconf.go b/envconf.go index 07629c3..5465e9f 100644 --- a/envconf.go +++ b/envconf.go @@ -44,7 +44,7 @@ func NewConfig() *Config { if len(splitted) == 2 { key := cleanKey(splitted[0]) val := splitted[1] - splitted = strings.Split(key, "_") + splitted = append(strings.Split(key, "_"), "", "", "") if unicode.IsLetter(getFirstRune(key)) { var entry cEntry entry.value = val @@ -54,15 +54,16 @@ func NewConfig() *Config { config.env[key] = entry if len(splitted) > 1 { for count, _ := range splitted { - left := strings.Join(splitted[:count], "_") - right := strings.Join(splitted[count:], "_") - if len(config.mapEnv[left]) == 0 { - config.mapEnv[left] = make(map[string]string) - config.mapMapEnv[left] = make(map[string]map[string]string) - config.mapMapMapEnv[left] = make(map[string]map[string]map[string]string) - } - config.mapEnv[left][right] = key - if count < len(splitted)-1 { + if count < len(splitted)-3 { + left := strings.Join(splitted[:count], "_") + right := strings.Join(splitted[count:], "_") + if len(config.mapEnv[left]) == 0 { + config.mapEnv[left] = make(map[string]string) + config.mapMapEnv[left] = make(map[string]map[string]string) + config.mapMapMapEnv[left] = make(map[string]map[string]map[string]string) + } + config.mapEnv[left][right] = key + middle := splitted[count] right = strings.Join(splitted[count+1:], "_") if len(config.mapMapEnv[left][middle]) == 0 { @@ -70,15 +71,14 @@ func NewConfig() *Config { config.mapMapMapEnv[left][middle] = make(map[string]map[string]string) } config.mapMapEnv[left][middle][right] = key - if count < len(splitted)-2 { - lmiddle := splitted[count] - rmiddle := splitted[count+1] - right = strings.Join(splitted[count+2:], "_") - if len(config.mapMapEnv[left][lmiddle][rmiddle]) == 0 { - config.mapMapMapEnv[left][lmiddle][rmiddle] = make(map[string]string) - } - config.mapMapMapEnv[left][lmiddle][rmiddle][right] = key + + lmiddle := splitted[count] + rmiddle := splitted[count+1] + right = strings.Join(splitted[count+2:], "_") + if len(config.mapMapEnv[left][lmiddle][rmiddle]) == 0 { + config.mapMapMapEnv[left][lmiddle][rmiddle] = make(map[string]string) } + config.mapMapMapEnv[left][lmiddle][rmiddle][right] = key } } }