add 4 dimensional map

This commit is contained in:
Roy Olav Purser 2022-04-02 13:09:01 +02:00
parent 6fea990cb9
commit 62ff297952
Signed by: roypur
GPG Key ID: E14D26A036F21656
2 changed files with 209 additions and 30 deletions

View File

@ -21,11 +21,13 @@ type cEntry struct {
}
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
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
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.
@ -38,13 +40,15 @@ func NewConfig() *Config {
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)
config.mapMapMapMapEnv = make(map[string]map[string]map[string]map[string]map[string]string)
config.mapMapMapMapMapEnv = make(map[string]map[string]map[string]map[string]map[string]map[string]string)
for _, v := range os.Environ() {
splitted := strings.SplitN(v, "=", 2)
if len(splitted) == 2 {
key := cleanKey(splitted[0])
val := splitted[1]
splitted = append(strings.Split(key, "_"), "", "", "")
splitted = append(strings.Split(key, "_"), "", "", "", "", "", "")
if unicode.IsLetter(getFirstRune(key)) {
var entry cEntry
entry.value = val
@ -54,36 +58,61 @@ func NewConfig() *Config {
config.env[key] = entry
if len(splitted) > 1 {
for count, _ := range splitted {
if count < len(splitted)-3 {
left := strings.Trim(strings.Join(splitted[:count], "_"), "_")
right := strings.Trim(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)
if count < len(splitted)-6 {
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 left != "" && right != "" {
config.mapEnv[left][right] = key
if p0 != "" && p1 != "" {
config.mapEnv[p0][p1] = key
}
middle := splitted[count]
right = strings.Trim(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)
p1 = splitted[count]
p2 = strings.Trim(strings.Join(splitted[count+1:], "_"), "_")
if len(config.mapMapEnv[p0][p1]) == 0 {
config.mapMapEnv[p0][p1] = make(map[string]string)
config.mapMapMapEnv[p0][p1] = make(map[string]map[string]string)
config.mapMapMapMapEnv[p0][p1] = make(map[string]map[string]map[string]string)
config.mapMapMapMapMapEnv[p0][p1] = make(map[string]map[string]map[string]map[string]string)
}
if left != "" && middle != "" && right != "" {
config.mapMapEnv[left][middle][right] = key
if p0 != "" && p1 != "" && p2 != "" {
config.mapMapEnv[p0][p1][p2] = key
}
lmiddle := splitted[count]
rmiddle := splitted[count+1]
right = strings.Trim(strings.Join(splitted[count+2:], "_"), "_")
if len(config.mapMapMapEnv[left][lmiddle][rmiddle]) == 0 {
config.mapMapMapEnv[left][lmiddle][rmiddle] = make(map[string]string)
p2 = splitted[count+1]
p3 = strings.Trim(strings.Join(splitted[count+2:], "_"), "_")
if len(config.mapMapMapEnv[p0][p1][p2]) == 0 {
config.mapMapMapEnv[p0][p1][p2] = make(map[string]string)
config.mapMapMapMapEnv[p0][p1][p2] = make(map[string]map[string]string)
config.mapMapMapMapMapEnv[p0][p1][p2] = make(map[string]map[string]map[string]string)
}
if left != "" && lmiddle != "" && rmiddle != "" && right != "" {
config.mapMapMapEnv[left][lmiddle][rmiddle][right] = key
if p0 != "" && p1 != "" && p2 != "" && p3 != "" {
config.mapMapMapEnv[p0][p1][p2][p3] = key
}
p3 = splitted[count+2]
p4 = strings.Trim(strings.Join(splitted[count+3:], "_"), "_")
if len(config.mapMapMapMapEnv[p0][p1][p2][p3]) == 0 {
config.mapMapMapMapEnv[p0][p1][p2][p3] = make(map[string]string)
config.mapMapMapMapMapEnv[p0][p1][p2][p3] = make(map[string]map[string]string)
}
if p0 != "" && p1 != "" && p2 != "" && p3 != "" && p4 != "" {
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
}
}
}
@ -386,6 +415,71 @@ func (c *Config) getRawMapMapMap(key string, dtype DataType) (empty map[string]m
return
}
func (c *Config) getRawMapMapMapMap(key string, dtype DataType) (empty map[string]map[string]map[string]map[string]cValue) {
empty = make(map[string]map[string]map[string]map[string]cValue)
retval := make(map[string]map[string]map[string]map[string]cValue)
if c.parsed {
key = cleanKey(key)
entries, ok := c.mapMapMapMapEnv[key]
if ok {
for k1, v1 := range entries {
retval[k1] = make(map[string]map[string]map[string]cValue)
for k2, v2 := range v1 {
retval[k1][k2] = make(map[string]map[string]cValue)
for k3, v3 := range v2 {
retval[k1][k2][k3] = make(map[string]cValue)
for k4, v4 := range v3 {
entry := c.env[v4]
if (entry.dtype.baseType() == dtype.baseType()) && (entry.parsed.err == nil) {
retval[k1][k2][k3][k4] = entry.parsed
} else {
return
}
}
}
}
}
return retval
}
}
return
}
func (c *Config) getRawMapMapMapMapMap(key string, dtype DataType) (empty map[string]map[string]map[string]map[string]map[string]cValue) {
empty = make(map[string]map[string]map[string]map[string]map[string]cValue)
retval := make(map[string]map[string]map[string]map[string]map[string]cValue)
if c.parsed {
key = cleanKey(key)
entries, ok := c.mapMapMapMapMapEnv[key]
if ok {
for k1, v1 := range entries {
retval[k1] = make(map[string]map[string]map[string]map[string]cValue)
for k2, v2 := range v1 {
retval[k1][k2] = make(map[string]map[string]map[string]cValue)
for k3, v3 := range v2 {
retval[k1][k2][k3] = make(map[string]map[string]cValue)
for k4, v4 := range v3 {
retval[k1][k2][k3][k4] = make(map[string]cValue)
for k5, v5 := range v4 {
entry := c.env[v5]
if (entry.dtype.baseType() == dtype.baseType()) && (entry.parsed.err == nil) {
retval[k1][k2][k3][k4][k5] = entry.parsed
} else {
return
}
}
}
}
}
}
return retval
}
}
return
}
// Returns the value of an environment variable.
// If the variable is not defined as envconf.TypeInt the function will return 0.
func (c *Config) GetInt(key string) int64 {
@ -604,6 +698,91 @@ func (c *Config) GetMapMapMapHex(key string) (retval map[string]map[string]map[s
return
}
func (c *Config) GetMapMapMapMapInt(key string) (retval map[string]map[string]map[string]map[string]int64) {
retval = make(map[string]map[string]map[string]map[string]int64)
for k1, v1 := range c.getRawMapMapMapMap(key, TypeInt) {
retval[k1] = make(map[string]map[string]map[string]int64)
for k2, v2 := range v1 {
retval[k1][k2] = make(map[string]map[string]int64)
for k3, v3 := range v2 {
retval[k1][k2][k3] = make(map[string]int64)
for k4, v4 := range v3 {
retval[k1][k2][k3][k4] = v4.intval
}
}
}
}
return
}
func (c *Config) GetMapMapMapMapDuration(key string) (retval map[string]map[string]map[string]map[string]time.Duration) {
retval = make(map[string]map[string]map[string]map[string]time.Duration)
for k1, v1 := range c.getRawMapMapMapMap(key, TypeDuration) {
retval[k1] = make(map[string]map[string]map[string]time.Duration)
for k2, v2 := range v1 {
retval[k1][k2] = make(map[string]map[string]time.Duration)
for k3, v3 := range v2 {
retval[k1][k2][k3] = make(map[string]time.Duration)
for k4, v4 := range v3 {
retval[k1][k2][k3][k4] = v4.durval
}
}
}
}
return
}
func (c *Config) GetMapMapMapMapString(key string) (retval map[string]map[string]map[string]map[string]string) {
retval = make(map[string]map[string]map[string]map[string]string)
for k1, v1 := range c.getRawMapMapMapMap(key, TypeString) {
retval[k1] = make(map[string]map[string]map[string]string)
for k2, v2 := range v1 {
retval[k1][k2] = make(map[string]map[string]string)
for k3, v3 := range v2 {
retval[k1][k2][k3] = make(map[string]string)
for k4, v4 := range v3 {
retval[k1][k2][k3][k4] = v4.strval
}
}
}
}
return
}
func (c *Config) GetMapMapMapMapBool(key string) (retval map[string]map[string]map[string]map[string]bool) {
retval = make(map[string]map[string]map[string]map[string]bool)
for k1, v1 := range c.getRawMapMapMapMap(key, TypeBool) {
retval[k1] = make(map[string]map[string]map[string]bool)
for k2, v2 := range v1 {
retval[k1][k2] = make(map[string]map[string]bool)
for k3, v3 := range v2 {
retval[k1][k2][k3] = make(map[string]bool)
for k4, v4 := range v3 {
retval[k1][k2][k3][k4] = v4.boolval
}
}
}
}
return
}
func (c *Config) GetMapMapMapMapHex(key string) (retval map[string]map[string]map[string]map[string][]byte) {
retval = make(map[string]map[string]map[string]map[string][]byte)
for k1, v1 := range c.getRawMapMapMapMap(key, TypeHex) {
retval[k1] = make(map[string]map[string]map[string][]byte)
for k2, v2 := range v1 {
retval[k1][k2] = make(map[string]map[string][]byte)
for k3, v3 := range v2 {
retval[k1][k2][k3] = make(map[string][]byte)
for k4, v4 := range v3 {
retval[k1][k2][k3][k4] = v4.binval
}
}
}
}
return
}
func getFirstRune(str string) rune {
for _, v := range str {
return v

View File

@ -11,6 +11,6 @@ func main() {
conf.DefineMap("test_map", envconf.TypeInt)
conf.Parse()
conf.Status()
fmt.Println(conf.GetMapMapInt("test_map"))
fmt.Println(conf.GetMapMapMapInt("test_map"))
fmt.Println(conf.GetInt("this_is_a_map"))
}