multi map test

This commit is contained in:
Roy Olav Purser 2022-03-27 13:37:04 +02:00
parent 5262d7abb7
commit 113ff46fee
Signed by: roypur
GPG Key ID: E14D26A036F21656

View File

@ -24,6 +24,8 @@ type Config struct {
parsed bool
env map[string]cEntry
mapEnv map[string]map[string]string
mapMapEnv map[string]map[string]map[string]string
mapMapMapEnv map[string]map[string]map[string]map[string]string
}
// NewConfig returns an envconf.Config that is used to read configuration from environment variables.
@ -34,6 +36,8 @@ func NewConfig() *Config {
config.parsed = false
config.env = make(map[string]cEntry)
config.mapEnv = make(map[string]map[string]string)
config.mapMapEnv = make(map[string]map[string]map[string]string)
config.mapMapMapEnv = make(map[string]map[string]map[string]map[string]string)
for _, v := range os.Environ() {
splitted := strings.SplitN(v, "=", 2)
@ -54,8 +58,28 @@ func NewConfig() *Config {
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)-2 {
middle := splitted[count]
right = strings.Join(splitted[count+1:], "_")
if len(config.mapMapEnv[left][middle]) == 0 {
config.mapMapEnv[left][middle] = make(map[string]string)
config.mapMapMapEnv[left][middle] = make(map[string]map[string]string)
}
config.mapMapEnv[left][middle][right] = key
}
if count < len(splitted)-3 {
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
}
}
}
}