recursive map generation

This commit is contained in:
Roy Olav Purser 2022-09-17 21:02:57 +02:00
parent 4a84dcdabe
commit 27d793492c
Signed by: roypur
GPG Key ID: 90142918D5E59AD3

View File

@ -40,20 +40,41 @@ func (kl *keyLookupStruct) setKey(parts []string, key string) {
} }
} }
func (kl *keyLookupStruct) print() {
if kl.end {
fmt.Println(kl.key)
fmt.Println("-----------------")
return
}
for key, val := range kl.next {
fmt.Println(key)
val.print()
}
}
type Config struct { type Config struct {
parsed bool parsed bool
env map[string]cEntry env map[string]cEntry
mapEnv map[int]*keyLookupStruct mapEnvTest map[int]*keyLookupStruct
mapEnv map[string]map[string]string
mapMapEnv map[string]map[string]map[string]string
mapMapMapEnv map[string]map[string]map[string]map[string]string
mapMapMapMapEnv map[string]map[string]map[string]map[string]map[string]string
mapMapMapMapMapEnv map[string]map[string]map[string]map[string]map[string]map[string]string
} }
// NewConfig returns an envconf.Config that is used to read configuration from environment variables. // NewConfig returns an envconf.Config that is used to read configuration from environment variables.
// The environment variables are stored in envconf.Config, so changes to the environment after NewConfig has been called // The environment variables are stored in envconf.Config, so changes to the environment after NewConfig has been called
// will not be taken into account. // will not be taken into account.
func NewConfig(levels int) *Config { func NewConfig() *Config {
levels := 5
config := new(Config) config := new(Config)
config.parsed = false config.parsed = false
config.env = make(map[string]cEntry) config.env = make(map[string]cEntry)
config.mapEnv = make(map[int]*keyLookupStruct) config.mapEnvTest = make(map[int]*keyLookupStruct)
for level := 1; level <= levels; level++ {
config.mapEnvTest[level] = new(keyLookupStruct)
}
for _, v := range os.Environ() { for _, v := range os.Environ() {
splitted := strings.SplitN(v, "=", 2) splitted := strings.SplitN(v, "=", 2)
@ -69,81 +90,82 @@ func NewConfig(levels int) *Config {
entry.empty = false entry.empty = false
config.env[key] = entry config.env[key] = entry
if len(splitted) > 1 { if len(splitted) > 1 {
for count, _ := range splitted { for level := 1; level <= levels; level++ {
if count >= len(splitted)-levels { for count := 0; count < level; count++ {
break
}
for level := 1; level <= levels; level++ {
parts := make([]string, level, level) parts := make([]string, level, level)
for partPos, part := range parts { parts[0] = strings.Trim(strings.Join(splitted[:count+1], "_"), "_")
firstPartPos := 0 for partPos, _ := range parts {
if partPos > 0 { if partPos > 0 {
firstPartPos = count + partPos - 1 if partPos+1 == len(parts) {
parts[partPos] = strings.Trim(strings.Join(splitted[partPos+count:], "_"), "_")
} else {
parts[partPos] = splitted[partPos+count]
}
} }
lastPartPos := firstPartPos
if partPos-1 == len(parts) {
lastPartPos = len(parts)
}
parts[partPos] = strings.Trim(strings.Join(splitted[:count], "_"), "_")
} }
config.mapEnv[level].setKey(parts, key) if parts[len(parts)-1] == "" {
break
}
config.mapEnvTest[level].setKey(parts, key)
} }
config.mapEnvTest[level].print()
/*
var p0, p1, p2, p3, p4, p5 string
p0 = strings.Trim(strings.Join(splitted[:count], "_"), "_")
p1 = strings.Trim(strings.Join(splitted[count:], "_"), "_")
if len(config.mapEnv[p0]) == 0 {
config.mapEnv[p0] = make(map[string]string)
config.mapMapEnv[p0] = make(map[string]map[string]string)
config.mapMapMapEnv[p0] = make(map[string]map[string]map[string]string)
config.mapMapMapMapEnv[p0] = make(map[string]map[string]map[string]map[string]string)
config.mapMapMapMapMapEnv[p0] = make(map[string]map[string]map[string]map[string]map[string]string)
}
if p0 != "" && p1 != "" {
config.mapEnv[p0][p1] = key
}
var p0, p1, p2, p3, p4, p5 string p1 = splitted[count]
p0 = strings.Trim(strings.Join(splitted[:count], "_"), "_") p2 = strings.Trim(strings.Join(splitted[count+1:], "_"), "_")
p1 = strings.Trim(strings.Join(splitted[count:], "_"), "_") if len(config.mapMapEnv[p0][p1]) == 0 {
if len(config.mapEnv[p0]) == 0 { config.mapMapEnv[p0][p1] = make(map[string]string)
config.mapEnv[p0] = make(map[string]string) config.mapMapMapEnv[p0][p1] = make(map[string]map[string]string)
config.mapMapEnv[p0] = make(map[string]map[string]string) config.mapMapMapMapEnv[p0][p1] = make(map[string]map[string]map[string]string)
config.mapMapMapEnv[p0] = make(map[string]map[string]map[string]string) config.mapMapMapMapMapEnv[p0][p1] = make(map[string]map[string]map[string]map[string]string)
config.mapMapMapMapEnv[p0] = make(map[string]map[string]map[string]map[string]string) }
config.mapMapMapMapMapEnv[p0] = make(map[string]map[string]map[string]map[string]map[string]string) if p0 != "" && p1 != "" && p2 != "" {
} config.mapMapEnv[p0][p1][p2] = key
if p0 != "" && p1 != "" { }
config.mapEnv[p0][p1] = key
}
p1 = splitted[count] p2 = splitted[count+1]
p2 = strings.Trim(strings.Join(splitted[count+1:], "_"), "_") p3 = strings.Trim(strings.Join(splitted[count+2:], "_"), "_")
if len(config.mapMapEnv[p0][p1]) == 0 { if len(config.mapMapMapEnv[p0][p1][p2]) == 0 {
config.mapMapEnv[p0][p1] = make(map[string]string) config.mapMapMapEnv[p0][p1][p2] = make(map[string]string)
config.mapMapMapEnv[p0][p1] = make(map[string]map[string]string) config.mapMapMapMapEnv[p0][p1][p2] = make(map[string]map[string]string)
config.mapMapMapMapEnv[p0][p1] = make(map[string]map[string]map[string]string) config.mapMapMapMapMapEnv[p0][p1][p2] = make(map[string]map[string]map[string]string)
config.mapMapMapMapMapEnv[p0][p1] = make(map[string]map[string]map[string]map[string]string) }
} if p0 != "" && p1 != "" && p2 != "" && p3 != "" {
if p0 != "" && p1 != "" && p2 != "" { config.mapMapMapEnv[p0][p1][p2][p3] = key
config.mapMapEnv[p0][p1][p2] = key }
}
p2 = splitted[count+1] p3 = splitted[count+2]
p3 = strings.Trim(strings.Join(splitted[count+2:], "_"), "_") p4 = strings.Trim(strings.Join(splitted[count+3:], "_"), "_")
if len(config.mapMapMapEnv[p0][p1][p2]) == 0 { if len(config.mapMapMapMapEnv[p0][p1][p2][p3]) == 0 {
config.mapMapMapEnv[p0][p1][p2] = make(map[string]string) config.mapMapMapMapEnv[p0][p1][p2][p3] = make(map[string]string)
config.mapMapMapMapEnv[p0][p1][p2] = make(map[string]map[string]string) config.mapMapMapMapMapEnv[p0][p1][p2][p3] = make(map[string]map[string]string)
config.mapMapMapMapMapEnv[p0][p1][p2] = make(map[string]map[string]map[string]string) }
} if p0 != "" && p1 != "" && p2 != "" && p3 != "" && p4 != "" {
if p0 != "" && p1 != "" && p2 != "" && p3 != "" { config.mapMapMapMapEnv[p0][p1][p2][p3][p4] = key
config.mapMapMapEnv[p0][p1][p2][p3] = key }
}
p3 = splitted[count+2] p4 = splitted[count+3]
p4 = strings.Trim(strings.Join(splitted[count+3:], "_"), "_") p5 = strings.Trim(strings.Join(splitted[count+4:], "_"), "_")
if len(config.mapMapMapMapEnv[p0][p1][p2][p3]) == 0 { if len(config.mapMapMapMapMapEnv[p0][p1][p2][p3][p4]) == 0 {
config.mapMapMapMapEnv[p0][p1][p2][p3] = make(map[string]string) config.mapMapMapMapMapEnv[p0][p1][p2][p3][p4] = make(map[string]string)
config.mapMapMapMapMapEnv[p0][p1][p2][p3] = make(map[string]map[string]string) }
} if p0 != "" && p1 != "" && p2 != "" && p3 != "" && p4 != "" && p5 != "" {
if p0 != "" && p1 != "" && p2 != "" && p3 != "" && p4 != "" { config.mapMapMapMapMapEnv[p0][p1][p2][p3][p4][p5] = key
config.mapMapMapMapEnv[p0][p1][p2][p3][p4] = key }
} */
p4 = splitted[count+3]
p5 = strings.Trim(strings.Join(splitted[count+4:], "_"), "_")
if len(config.mapMapMapMapMapEnv[p0][p1][p2][p3][p4]) == 0 {
config.mapMapMapMapMapEnv[p0][p1][p2][p3][p4] = make(map[string]string)
}
if p0 != "" && p1 != "" && p2 != "" && p3 != "" && p4 != "" && p5 != "" {
config.mapMapMapMapMapEnv[p0][p1][p2][p3][p4][p5] = key
}
} }
} }