add constructors

This commit is contained in:
Roy Olav Purser 2022-03-27 14:14:43 +02:00
parent 113ff46fee
commit 4520530b7f
Signed by: roypur
GPG Key ID: E14D26A036F21656
2 changed files with 187 additions and 9 deletions

View File

@ -70,15 +70,15 @@ func NewConfig() *Config {
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)
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
}
config.mapMapMapEnv[left][lmiddle][rmiddle][right] = key
}
}
}
@ -327,6 +327,59 @@ func (c *Config) getRawMap(key string, dtype DataType) (empty map[string]cValue)
return
}
func (c *Config) getRawMapMap(key string, dtype DataType) (empty map[string]map[string]cValue) {
empty = make(map[string]map[string]cValue)
retval := make(map[string]map[string]cValue)
if c.parsed {
key = cleanKey(key)
entries, ok := c.mapMapEnv[key]
if ok {
for k1, v1 := range entries {
retval[k1] = make(map[string]cValue)
for k2, v2 := range v1 {
entry := c.env[v2]
if (entry.dtype.baseType() == dtype.baseType()) && (entry.parsed.err == nil) {
retval[k1][k2] = entry.parsed
} else {
return
}
}
}
return retval
}
}
return
}
func (c *Config) getRawMapMapMap(key string, dtype DataType) (empty map[string]map[string]map[string]cValue) {
empty = make(map[string]map[string]map[string]cValue)
retval := make(map[string]map[string]map[string]cValue)
if c.parsed {
key = cleanKey(key)
entries, ok := c.mapMapMapEnv[key]
if ok {
for k1, v1 := range entries {
retval[k1] = make(map[string]map[string]cValue)
for k2, v2 := range v1 {
retval[k1][k2] = make(map[string]cValue)
for k3, v3 := range v2 {
entry := c.env[v3]
if (entry.dtype.baseType() == dtype.baseType()) && (entry.parsed.err == nil) {
retval[k1][k2][k3] = 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 {
@ -420,6 +473,131 @@ func (c *Config) GetMapHex(key string) (retval map[string][]byte) {
return
}
func (c *Config) GetMapMapInt(key string) (retval map[string]map[string]int64) {
retval = make(map[string]map[string]int64)
for k1, v1 := range c.getRawMapMap(key, TypeInt) {
retval[k1] = make(map[string]int64)
for k2, v2 := range v1 {
retval[k1][k2] = v2.intval
}
}
return
}
func (c *Config) GetMapMapDuration(key string) (retval map[string]map[string]time.Duration) {
retval = make(map[string]map[string]time.Duration)
for k1, v1 := range c.getRawMapMap(key, TypeDuration) {
retval[k1] = make(map[string]time.Duration)
for k2, v2 := range v1 {
retval[k1][k2] = v2.durval
}
}
return
}
func (c *Config) GetMapMapString(key string) (retval map[string]map[string]string) {
retval = make(map[string]map[string]string)
for k1, v1 := range c.getRawMapMap(key, TypeString) {
retval[k1] = make(map[string]string)
for k2, v2 := range v1 {
retval[k1][k2] = v2.strval
}
}
return
}
func (c *Config) GetMapMapBool(key string) (retval map[string]map[string]bool) {
retval = make(map[string]map[string]bool)
for k1, v1 := range c.getRawMapMap(key, TypeBool) {
retval[k1] = make(map[string]bool)
for k2, v2 := range v1 {
retval[k1][k2] = v2.boolval
}
}
return
}
func (c *Config) GetMapMapHex(key string) (retval map[string]map[string][]byte) {
retval = make(map[string]map[string][]byte)
for k1, v1 := range c.getRawMapMap(key, TypeHex) {
retval[k1] = make(map[string][]byte)
for k2, v2 := range v1 {
retval[k1][k2] = v2.binval
}
}
return
}
func (c *Config) GetMapMapMapInt(key string) (retval map[string]map[string]map[string]int64) {
retval = make(map[string]map[string]map[string]int64)
for k1, v1 := range c.getRawMapMapMap(key, TypeInt) {
retval[k1] = make(map[string]map[string]int64)
for k2, v2 := range v1 {
retval[k1][k2] = make(map[string]int64)
for k3, v3 := range v2 {
retval[k1][k2][k3] = v3.intval
}
}
}
return
}
func (c *Config) GetMapMapMapDuration(key string) (retval map[string]map[string]map[string]time.Duration) {
retval = make(map[string]map[string]map[string]time.Duration)
for k1, v1 := range c.getRawMapMapMap(key, TypeDuration) {
retval[k1] = make(map[string]map[string]time.Duration)
for k2, v2 := range v1 {
retval[k1][k2] = make(map[string]time.Duration)
for k3, v3 := range v2 {
retval[k1][k2][k3] = v3.durval
}
}
}
return
}
func (c *Config) GetMapMapMapString(key string) (retval map[string]map[string]map[string]string) {
retval = make(map[string]map[string]map[string]string)
for k1, v1 := range c.getRawMapMapMap(key, TypeString) {
retval[k1] = make(map[string]map[string]string)
for k2, v2 := range v1 {
retval[k1][k2] = make(map[string]string)
for k3, v3 := range v2 {
retval[k1][k2][k3] = v3.strval
}
}
}
return
}
func (c *Config) GetMapMapMapBool(key string) (retval map[string]map[string]map[string]bool) {
retval = make(map[string]map[string]map[string]bool)
for k1, v1 := range c.getRawMapMapMap(key, TypeBool) {
retval[k1] = make(map[string]map[string]bool)
for k2, v2 := range v1 {
retval[k1][k2] = make(map[string]bool)
for k3, v3 := range v2 {
retval[k1][k2][k3] = v3.boolval
}
}
}
return
}
func (c *Config) GetMapMapMapHex(key string) (retval map[string]map[string]map[string][]byte) {
retval = make(map[string]map[string]map[string][]byte)
for k1, v1 := range c.getRawMapMapMap(key, TypeHex) {
retval[k1] = make(map[string]map[string][]byte)
for k2, v2 := range v1 {
retval[k1][k2] = make(map[string][]byte)
for k3, v3 := range v2 {
retval[k1][k2][k3] = v3.binval
}
}
}
return
}
func getFirstRune(str string) rune {
for _, v := range str {
return v

View File

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