6 Commits

Author SHA1 Message Date
113ff46fee multi map test 2022-03-27 13:37:04 +02:00
5262d7abb7 remove debug prints 2022-01-29 22:03:33 +01:00
2efc1a155e print mapEnv 2022-01-29 22:00:54 +01:00
45f8b0f918 add test print 2022-01-29 21:59:03 +01:00
2cdcbec364 add test print 2022-01-29 21:51:28 +01:00
f815bff582 test map expand 2022-01-29 21:37:06 +01:00
2 changed files with 50 additions and 36 deletions

View File

@ -23,7 +23,9 @@ type cEntry struct {
type Config struct { type Config struct {
parsed bool parsed bool
env map[string]cEntry env map[string]cEntry
mapEnv map[string]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. // NewConfig returns an envconf.Config that is used to read configuration from environment variables.
@ -33,27 +35,52 @@ func NewConfig() *Config {
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[string]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() { 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])
left, right, mappable := keySplit(key) val := splitted[1]
splitted = strings.Split(key, "_")
if unicode.IsLetter(getFirstRune(key)) { if unicode.IsLetter(getFirstRune(key)) {
var entry cEntry var entry cEntry
entry.value = splitted[1] entry.value = val
entry.dtype = TypeNone entry.dtype = TypeNone
entry.unset = false entry.unset = false
entry.empty = false entry.empty = false
config.env[key] = entry config.env[key] = entry
if mappable { if len(splitted) > 1 {
for count, _ := range splitted {
left := strings.Join(splitted[:count], "_")
right := strings.Join(splitted[count:], "_")
if len(config.mapEnv[left]) == 0 { if len(config.mapEnv[left]) == 0 {
config.mapEnv[left] = make(map[string]cEntry) 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
if count < len(splitted)-2 {
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
}
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.mapEnv[left][right] = entry
} }
} }
} }
@ -84,9 +111,10 @@ func (c *Config) DefineMap(key string, dtype DataType) {
key = cleanKey(key) key = cleanKey(key)
entries, ok := c.mapEnv[key] entries, ok := c.mapEnv[key]
if ok { if ok {
for mapKey, entry := range entries { for _, key = range entries {
entry := c.env[key]
entry.dtype = dtype entry.dtype = dtype
c.mapEnv[key][mapKey] = entry c.env[key] = entry
} }
} }
} }
@ -136,13 +164,6 @@ func (c *Config) Parse() {
} }
} }
for k, v := range c.mapEnv {
for mk, mv := range v {
mv.parsed = mv.dtype.parse(k+"_"+mk, mv.value)
c.mapEnv[k][mk] = mv
}
}
if failed { if failed {
for k, v := range c.env { for k, v := range c.env {
if (v.parsed.err == nil) && v.unset { if (v.parsed.err == nil) && v.unset {
@ -234,17 +255,7 @@ func (c *Config) Status() (ok bool) {
} }
} }
} }
for _, v := range c.mapEnv {
for _, mv := range v {
err := mv.parsed.err
if err != nil {
ok = false
if !mv.empty {
fmt.Fprintln(os.Stderr, err)
}
}
}
}
if !ok { if !ok {
fmt.Fprintln(os.Stderr, "") fmt.Fprintln(os.Stderr, "")
for _, v := range c.env { for _, v := range c.env {
@ -303,8 +314,9 @@ func (c *Config) getRawMap(key string, dtype DataType) (empty map[string]cValue)
if ok { if ok {
for k, v := range entries { for k, v := range entries {
if (v.dtype.baseType() == dtype.baseType()) && (v.parsed.err == nil) { entry := c.env[v]
retval[k] = v.parsed if (entry.dtype.baseType() == dtype.baseType()) && (entry.parsed.err == nil) {
retval[k] = entry.parsed
} else { } else {
return return
} }

View File

@ -7,9 +7,11 @@ import (
func main() { func main() {
conf := envconf.NewConfig() conf := envconf.NewConfig()
conf.Define("this_is_a_map", envconf.FixedHex(22)) conf.Define("this_is_a_map", envconf.TypeInt)
conf.DefineMap("test_map", envconf.TypeInt)
conf.Parse() conf.Parse()
conf.Status() conf.Status()
fmt.Println(conf.GetHex("this_is_a_map")) fmt.Println(conf.GetMapInt("test_map"))
fmt.Println(conf.GetInt("this_is_a_map"))
} }