Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
113ff46fee
|
|||
5262d7abb7
|
33
envconf.go
33
envconf.go
@ -21,9 +21,11 @@ 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]string
|
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.
|
||||||
@ -34,6 +36,8 @@ func NewConfig() *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]string)
|
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)
|
||||||
@ -54,9 +58,28 @@ func NewConfig() *Config {
|
|||||||
right := 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]string)
|
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)
|
||||||
}
|
}
|
||||||
fmt.Printf("left=(%s), right=(%s), key=(%s)\n", left, right, key)
|
|
||||||
config.mapEnv[left][right] = key
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -86,11 +109,9 @@ func (c *Config) Define(key string, dtype DataType) {
|
|||||||
// Variables without a defined type will be ignored by Parse.
|
// Variables without a defined type will be ignored by Parse.
|
||||||
func (c *Config) DefineMap(key string, dtype DataType) {
|
func (c *Config) DefineMap(key string, dtype DataType) {
|
||||||
key = cleanKey(key)
|
key = cleanKey(key)
|
||||||
fmt.Println(c.mapEnv)
|
|
||||||
entries, ok := c.mapEnv[key]
|
entries, ok := c.mapEnv[key]
|
||||||
if ok {
|
if ok {
|
||||||
for _, key = range entries {
|
for _, key = range entries {
|
||||||
fmt.Printf("key-from-define-map (%s)\n", key)
|
|
||||||
entry := c.env[key]
|
entry := c.env[key]
|
||||||
entry.dtype = dtype
|
entry.dtype = dtype
|
||||||
c.env[key] = entry
|
c.env[key] = entry
|
||||||
|
@ -8,7 +8,7 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
conf := envconf.NewConfig()
|
conf := envconf.NewConfig()
|
||||||
conf.Define("this_is_a_map", envconf.TypeInt)
|
conf.Define("this_is_a_map", envconf.TypeInt)
|
||||||
conf.Define("test_map", envconf.TypeInt)
|
conf.DefineMap("test_map", envconf.TypeInt)
|
||||||
conf.Parse()
|
conf.Parse()
|
||||||
conf.Status()
|
conf.Status()
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user