test map expand
This commit is contained in:
parent
73278439b0
commit
f815bff582
52
envconf.go
52
envconf.go
@ -23,7 +23,7 @@ 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
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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 +33,30 @@ 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)
|
||||||
|
|
||||||
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 {
|
||||||
if len(config.mapEnv[left]) == 0 {
|
for count, _ := range splitted {
|
||||||
config.mapEnv[left] = make(map[string]cEntry)
|
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)
|
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 +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 {
|
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 +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 {
|
if !ok {
|
||||||
fmt.Fprintln(os.Stderr, "")
|
fmt.Fprintln(os.Stderr, "")
|
||||||
for _, v := range c.env {
|
for _, v := range c.env {
|
||||||
@ -303,8 +290,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
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user