test map expand

This commit is contained in:
Roy Olav Purser 2022-01-29 21:37:06 +01:00
parent 73278439b0
commit f815bff582
Signed by: roypur
GPG Key ID: E14D26A036F21656

View File

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