4 Commits

Author SHA1 Message Date
7a72947737 test 2022-03-27 14:42:43 +02:00
3c23ee5473 change map offset 2022-03-27 14:32:28 +02:00
4520530b7f add constructors 2022-03-27 14:14:43 +02:00
113ff46fee multi map test 2022-03-27 13:37:04 +02:00
2 changed files with 212 additions and 11 deletions

View File

@ -21,9 +21,11 @@ type cEntry struct {
}
type Config struct {
parsed bool
env map[string]cEntry
mapEnv 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
}
// NewConfig returns an envconf.Config that is used to read configuration from environment variables.
@ -34,13 +36,15 @@ 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)
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
@ -50,12 +54,32 @@ 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)
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 {
config.mapMapEnv[left][middle] = make(map[string]string)
config.mapMapMapEnv[left][middle] = make(map[string]map[string]string)
}
config.mapMapEnv[left][middle][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
}
config.mapEnv[left][right] = key
}
}
}
@ -303,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 {
@ -396,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,6 @@ func main() {
conf.DefineMap("test_map", envconf.TypeInt)
conf.Parse()
conf.Status()
fmt.Println(conf.GetMapInt("test_map"))
fmt.Println(conf.GetMapMapInt("test_map"))
fmt.Println(conf.GetInt("this_is_a_map"))
}