Compare commits

..

No commits in common. "9badf9a54a3468409ebcf78d7638cf2aba73e43a" and "6fea990cb956d2eb9212e4d2304de7887b438243" have entirely different histories.

2 changed files with 30 additions and 309 deletions

View File

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

View File

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