Compare commits

...

24 Commits

Author SHA1 Message Date
b901036467
auto generated nested mapping 2022-09-23 20:22:24 +02:00
eef48c1d0a
initial work on auto generated definitions 2022-09-23 14:44:08 +02:00
27d793492c
recursive map generation 2022-09-17 21:02:57 +02:00
4a84dcdabe
use local files for test 2022-09-17 13:07:12 +02:00
9badf9a54a
add 5 dimensional map 2022-04-02 13:15:55 +02:00
62ff297952
add 4 dimensional map 2022-04-02 13:09:01 +02:00
6fea990cb9
remove empty map 2022-03-27 15:03:39 +02:00
19fdcd7e05
remove empty map 2022-03-27 14:53:46 +02:00
7a72947737
test 2022-03-27 14:42:43 +02:00
3c23ee5473
change map offset 2022-03-27 14:32:28 +02:00
4520530b7f
add constructors 2022-03-27 14:14:43 +02:00
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
73278439b0
fix hooks 2022-01-29 13:31:31 +01:00
0a589a2565
add test 2022-01-29 13:29:09 +01:00
179666c8f9
fix constants 2022-01-29 13:03:17 +01:00
02d36e3d69
variable length hex 2022-01-28 19:46:16 +01:00
9733bb42a8
variable length hex 2022-01-28 19:38:27 +01:00
3d2005a8a2
add sized hex 2022-01-16 21:48:29 +01:00
1803501272
fix nil slice 2022-01-16 18:29:42 +01:00
10 changed files with 1246 additions and 116 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
tests/buildenv/*
tests/bin/*

View File

@ -1,3 +1,6 @@
#!/usr/bin/env bash
go fmt *.go
git add *.go
go fmt tests/*.go
git add *.go tests/*.go
git add tests/*.go

916
auto_generated.go Normal file
View File

@ -0,0 +1,916 @@
package envconf
import "time"
type mapEnvType [5]*keyLookupType
func (c *Config) GetMapInt(key string) (empty map[string]int64) {
empty = make(map[string]int64)
retval := make(map[string]int64)
if c.parsed {
key = cleanKey(key)
v10, ok := c.mapEnv[0].next[key]
if ok {
for k11, v11 := range v10.next {
entry := c.env[v11.key]
if (entry.dtype.baseType() == TypeInt.baseType()) && (entry.parsed.err == nil) {
retval[k11] = entry.parsed.intval
} else {
return
}
}
return retval
}
}
return
}
func (c *Config) GetMapMetric(key string) (empty map[string]int64) {
empty = make(map[string]int64)
retval := make(map[string]int64)
if c.parsed {
key = cleanKey(key)
v10, ok := c.mapEnv[0].next[key]
if ok {
for k11, v11 := range v10.next {
entry := c.env[v11.key]
if (entry.dtype.baseType() == TypeMetric.baseType()) && (entry.parsed.err == nil) {
retval[k11] = entry.parsed.intval
} else {
return
}
}
return retval
}
}
return
}
func (c *Config) GetMapDuration(key string) (empty map[string]time.Duration) {
empty = make(map[string]time.Duration)
retval := make(map[string]time.Duration)
if c.parsed {
key = cleanKey(key)
v10, ok := c.mapEnv[0].next[key]
if ok {
for k11, v11 := range v10.next {
entry := c.env[v11.key]
if (entry.dtype.baseType() == TypeDuration.baseType()) && (entry.parsed.err == nil) {
retval[k11] = entry.parsed.durval
} else {
return
}
}
return retval
}
}
return
}
func (c *Config) GetMapString(key string) (empty map[string]string) {
empty = make(map[string]string)
retval := make(map[string]string)
if c.parsed {
key = cleanKey(key)
v10, ok := c.mapEnv[0].next[key]
if ok {
for k11, v11 := range v10.next {
entry := c.env[v11.key]
if (entry.dtype.baseType() == TypeString.baseType()) && (entry.parsed.err == nil) {
retval[k11] = entry.parsed.strval
} else {
return
}
}
return retval
}
}
return
}
func (c *Config) GetMapDirectory(key string) (empty map[string]string) {
empty = make(map[string]string)
retval := make(map[string]string)
if c.parsed {
key = cleanKey(key)
v10, ok := c.mapEnv[0].next[key]
if ok {
for k11, v11 := range v10.next {
entry := c.env[v11.key]
if (entry.dtype.baseType() == TypeDirectory.baseType()) && (entry.parsed.err == nil) {
retval[k11] = entry.parsed.strval
} else {
return
}
}
return retval
}
}
return
}
func (c *Config) GetMapBool(key string) (empty map[string]bool) {
empty = make(map[string]bool)
retval := make(map[string]bool)
if c.parsed {
key = cleanKey(key)
v10, ok := c.mapEnv[0].next[key]
if ok {
for k11, v11 := range v10.next {
entry := c.env[v11.key]
if (entry.dtype.baseType() == TypeBool.baseType()) && (entry.parsed.err == nil) {
retval[k11] = entry.parsed.boolval
} else {
return
}
}
return retval
}
}
return
}
func (c *Config) GetMapHex(key string) (empty map[string][]byte) {
empty = make(map[string][]byte)
retval := make(map[string][]byte)
if c.parsed {
key = cleanKey(key)
v10, ok := c.mapEnv[0].next[key]
if ok {
for k11, v11 := range v10.next {
entry := c.env[v11.key]
if (entry.dtype.baseType() == TypeHex.baseType()) && (entry.parsed.err == nil) {
retval[k11] = entry.parsed.binval
} else {
return
}
}
return retval
}
}
return
}
func (c *Config) GetMapMapInt(key string) (empty map[string]map[string]int64) {
empty = make(map[string]map[string]int64)
retval := make(map[string]map[string]int64)
if c.parsed {
key = cleanKey(key)
v10, ok := c.mapEnv[1].next[key]
if ok {
for k11, v11 := range v10.next {
retval[k11] = make(map[string]int64)
for k12, v12 := range v11.next {
entry := c.env[v12.key]
if (entry.dtype.baseType() == TypeInt.baseType()) && (entry.parsed.err == nil) {
retval[k11][k12] = entry.parsed.intval
} else {
return
}
}
}
return retval
}
}
return
}
func (c *Config) GetMapMapMetric(key string) (empty map[string]map[string]int64) {
empty = make(map[string]map[string]int64)
retval := make(map[string]map[string]int64)
if c.parsed {
key = cleanKey(key)
v10, ok := c.mapEnv[1].next[key]
if ok {
for k11, v11 := range v10.next {
retval[k11] = make(map[string]int64)
for k12, v12 := range v11.next {
entry := c.env[v12.key]
if (entry.dtype.baseType() == TypeMetric.baseType()) && (entry.parsed.err == nil) {
retval[k11][k12] = entry.parsed.intval
} else {
return
}
}
}
return retval
}
}
return
}
func (c *Config) GetMapMapDuration(key string) (empty map[string]map[string]time.Duration) {
empty = make(map[string]map[string]time.Duration)
retval := make(map[string]map[string]time.Duration)
if c.parsed {
key = cleanKey(key)
v10, ok := c.mapEnv[1].next[key]
if ok {
for k11, v11 := range v10.next {
retval[k11] = make(map[string]time.Duration)
for k12, v12 := range v11.next {
entry := c.env[v12.key]
if (entry.dtype.baseType() == TypeDuration.baseType()) && (entry.parsed.err == nil) {
retval[k11][k12] = entry.parsed.durval
} else {
return
}
}
}
return retval
}
}
return
}
func (c *Config) GetMapMapString(key string) (empty map[string]map[string]string) {
empty = make(map[string]map[string]string)
retval := make(map[string]map[string]string)
if c.parsed {
key = cleanKey(key)
v10, ok := c.mapEnv[1].next[key]
if ok {
for k11, v11 := range v10.next {
retval[k11] = make(map[string]string)
for k12, v12 := range v11.next {
entry := c.env[v12.key]
if (entry.dtype.baseType() == TypeString.baseType()) && (entry.parsed.err == nil) {
retval[k11][k12] = entry.parsed.strval
} else {
return
}
}
}
return retval
}
}
return
}
func (c *Config) GetMapMapDirectory(key string) (empty map[string]map[string]string) {
empty = make(map[string]map[string]string)
retval := make(map[string]map[string]string)
if c.parsed {
key = cleanKey(key)
v10, ok := c.mapEnv[1].next[key]
if ok {
for k11, v11 := range v10.next {
retval[k11] = make(map[string]string)
for k12, v12 := range v11.next {
entry := c.env[v12.key]
if (entry.dtype.baseType() == TypeDirectory.baseType()) && (entry.parsed.err == nil) {
retval[k11][k12] = entry.parsed.strval
} else {
return
}
}
}
return retval
}
}
return
}
func (c *Config) GetMapMapBool(key string) (empty map[string]map[string]bool) {
empty = make(map[string]map[string]bool)
retval := make(map[string]map[string]bool)
if c.parsed {
key = cleanKey(key)
v10, ok := c.mapEnv[1].next[key]
if ok {
for k11, v11 := range v10.next {
retval[k11] = make(map[string]bool)
for k12, v12 := range v11.next {
entry := c.env[v12.key]
if (entry.dtype.baseType() == TypeBool.baseType()) && (entry.parsed.err == nil) {
retval[k11][k12] = entry.parsed.boolval
} else {
return
}
}
}
return retval
}
}
return
}
func (c *Config) GetMapMapHex(key string) (empty map[string]map[string][]byte) {
empty = make(map[string]map[string][]byte)
retval := make(map[string]map[string][]byte)
if c.parsed {
key = cleanKey(key)
v10, ok := c.mapEnv[1].next[key]
if ok {
for k11, v11 := range v10.next {
retval[k11] = make(map[string][]byte)
for k12, v12 := range v11.next {
entry := c.env[v12.key]
if (entry.dtype.baseType() == TypeHex.baseType()) && (entry.parsed.err == nil) {
retval[k11][k12] = entry.parsed.binval
} else {
return
}
}
}
return retval
}
}
return
}
func (c *Config) GetMapMapMapInt(key string) (empty map[string]map[string]map[string]int64) {
empty = make(map[string]map[string]map[string]int64)
retval := make(map[string]map[string]map[string]int64)
if c.parsed {
key = cleanKey(key)
v10, ok := c.mapEnv[2].next[key]
if ok {
for k11, v11 := range v10.next {
retval[k11] = make(map[string]map[string]int64)
for k12, v12 := range v11.next {
retval[k11][k12] = make(map[string]int64)
for k13, v13 := range v12.next {
entry := c.env[v13.key]
if (entry.dtype.baseType() == TypeInt.baseType()) && (entry.parsed.err == nil) {
retval[k11][k12][k13] = entry.parsed.intval
} else {
return
}
}
}
}
return retval
}
}
return
}
func (c *Config) GetMapMapMapMetric(key string) (empty map[string]map[string]map[string]int64) {
empty = make(map[string]map[string]map[string]int64)
retval := make(map[string]map[string]map[string]int64)
if c.parsed {
key = cleanKey(key)
v10, ok := c.mapEnv[2].next[key]
if ok {
for k11, v11 := range v10.next {
retval[k11] = make(map[string]map[string]int64)
for k12, v12 := range v11.next {
retval[k11][k12] = make(map[string]int64)
for k13, v13 := range v12.next {
entry := c.env[v13.key]
if (entry.dtype.baseType() == TypeMetric.baseType()) && (entry.parsed.err == nil) {
retval[k11][k12][k13] = entry.parsed.intval
} else {
return
}
}
}
}
return retval
}
}
return
}
func (c *Config) GetMapMapMapDuration(key string) (empty map[string]map[string]map[string]time.Duration) {
empty = make(map[string]map[string]map[string]time.Duration)
retval := make(map[string]map[string]map[string]time.Duration)
if c.parsed {
key = cleanKey(key)
v10, ok := c.mapEnv[2].next[key]
if ok {
for k11, v11 := range v10.next {
retval[k11] = make(map[string]map[string]time.Duration)
for k12, v12 := range v11.next {
retval[k11][k12] = make(map[string]time.Duration)
for k13, v13 := range v12.next {
entry := c.env[v13.key]
if (entry.dtype.baseType() == TypeDuration.baseType()) && (entry.parsed.err == nil) {
retval[k11][k12][k13] = entry.parsed.durval
} else {
return
}
}
}
}
return retval
}
}
return
}
func (c *Config) GetMapMapMapString(key string) (empty map[string]map[string]map[string]string) {
empty = make(map[string]map[string]map[string]string)
retval := make(map[string]map[string]map[string]string)
if c.parsed {
key = cleanKey(key)
v10, ok := c.mapEnv[2].next[key]
if ok {
for k11, v11 := range v10.next {
retval[k11] = make(map[string]map[string]string)
for k12, v12 := range v11.next {
retval[k11][k12] = make(map[string]string)
for k13, v13 := range v12.next {
entry := c.env[v13.key]
if (entry.dtype.baseType() == TypeString.baseType()) && (entry.parsed.err == nil) {
retval[k11][k12][k13] = entry.parsed.strval
} else {
return
}
}
}
}
return retval
}
}
return
}
func (c *Config) GetMapMapMapDirectory(key string) (empty map[string]map[string]map[string]string) {
empty = make(map[string]map[string]map[string]string)
retval := make(map[string]map[string]map[string]string)
if c.parsed {
key = cleanKey(key)
v10, ok := c.mapEnv[2].next[key]
if ok {
for k11, v11 := range v10.next {
retval[k11] = make(map[string]map[string]string)
for k12, v12 := range v11.next {
retval[k11][k12] = make(map[string]string)
for k13, v13 := range v12.next {
entry := c.env[v13.key]
if (entry.dtype.baseType() == TypeDirectory.baseType()) && (entry.parsed.err == nil) {
retval[k11][k12][k13] = entry.parsed.strval
} else {
return
}
}
}
}
return retval
}
}
return
}
func (c *Config) GetMapMapMapBool(key string) (empty map[string]map[string]map[string]bool) {
empty = make(map[string]map[string]map[string]bool)
retval := make(map[string]map[string]map[string]bool)
if c.parsed {
key = cleanKey(key)
v10, ok := c.mapEnv[2].next[key]
if ok {
for k11, v11 := range v10.next {
retval[k11] = make(map[string]map[string]bool)
for k12, v12 := range v11.next {
retval[k11][k12] = make(map[string]bool)
for k13, v13 := range v12.next {
entry := c.env[v13.key]
if (entry.dtype.baseType() == TypeBool.baseType()) && (entry.parsed.err == nil) {
retval[k11][k12][k13] = entry.parsed.boolval
} else {
return
}
}
}
}
return retval
}
}
return
}
func (c *Config) GetMapMapMapHex(key string) (empty map[string]map[string]map[string][]byte) {
empty = make(map[string]map[string]map[string][]byte)
retval := make(map[string]map[string]map[string][]byte)
if c.parsed {
key = cleanKey(key)
v10, ok := c.mapEnv[2].next[key]
if ok {
for k11, v11 := range v10.next {
retval[k11] = make(map[string]map[string][]byte)
for k12, v12 := range v11.next {
retval[k11][k12] = make(map[string][]byte)
for k13, v13 := range v12.next {
entry := c.env[v13.key]
if (entry.dtype.baseType() == TypeHex.baseType()) && (entry.parsed.err == nil) {
retval[k11][k12][k13] = entry.parsed.binval
} else {
return
}
}
}
}
return retval
}
}
return
}
func (c *Config) GetMapMapMapMapInt(key string) (empty map[string]map[string]map[string]map[string]int64) {
empty = make(map[string]map[string]map[string]map[string]int64)
retval := make(map[string]map[string]map[string]map[string]int64)
if c.parsed {
key = cleanKey(key)
v10, ok := c.mapEnv[3].next[key]
if ok {
for k11, v11 := range v10.next {
retval[k11] = make(map[string]map[string]map[string]int64)
for k12, v12 := range v11.next {
retval[k11][k12] = make(map[string]map[string]int64)
for k13, v13 := range v12.next {
retval[k11][k12][k13] = make(map[string]int64)
for k14, v14 := range v13.next {
entry := c.env[v14.key]
if (entry.dtype.baseType() == TypeInt.baseType()) && (entry.parsed.err == nil) {
retval[k11][k12][k13][k14] = entry.parsed.intval
} else {
return
}
}
}
}
}
return retval
}
}
return
}
func (c *Config) GetMapMapMapMapMetric(key string) (empty map[string]map[string]map[string]map[string]int64) {
empty = make(map[string]map[string]map[string]map[string]int64)
retval := make(map[string]map[string]map[string]map[string]int64)
if c.parsed {
key = cleanKey(key)
v10, ok := c.mapEnv[3].next[key]
if ok {
for k11, v11 := range v10.next {
retval[k11] = make(map[string]map[string]map[string]int64)
for k12, v12 := range v11.next {
retval[k11][k12] = make(map[string]map[string]int64)
for k13, v13 := range v12.next {
retval[k11][k12][k13] = make(map[string]int64)
for k14, v14 := range v13.next {
entry := c.env[v14.key]
if (entry.dtype.baseType() == TypeMetric.baseType()) && (entry.parsed.err == nil) {
retval[k11][k12][k13][k14] = entry.parsed.intval
} else {
return
}
}
}
}
}
return retval
}
}
return
}
func (c *Config) GetMapMapMapMapDuration(key string) (empty map[string]map[string]map[string]map[string]time.Duration) {
empty = make(map[string]map[string]map[string]map[string]time.Duration)
retval := make(map[string]map[string]map[string]map[string]time.Duration)
if c.parsed {
key = cleanKey(key)
v10, ok := c.mapEnv[3].next[key]
if ok {
for k11, v11 := range v10.next {
retval[k11] = make(map[string]map[string]map[string]time.Duration)
for k12, v12 := range v11.next {
retval[k11][k12] = make(map[string]map[string]time.Duration)
for k13, v13 := range v12.next {
retval[k11][k12][k13] = make(map[string]time.Duration)
for k14, v14 := range v13.next {
entry := c.env[v14.key]
if (entry.dtype.baseType() == TypeDuration.baseType()) && (entry.parsed.err == nil) {
retval[k11][k12][k13][k14] = entry.parsed.durval
} else {
return
}
}
}
}
}
return retval
}
}
return
}
func (c *Config) GetMapMapMapMapString(key string) (empty map[string]map[string]map[string]map[string]string) {
empty = make(map[string]map[string]map[string]map[string]string)
retval := make(map[string]map[string]map[string]map[string]string)
if c.parsed {
key = cleanKey(key)
v10, ok := c.mapEnv[3].next[key]
if ok {
for k11, v11 := range v10.next {
retval[k11] = make(map[string]map[string]map[string]string)
for k12, v12 := range v11.next {
retval[k11][k12] = make(map[string]map[string]string)
for k13, v13 := range v12.next {
retval[k11][k12][k13] = make(map[string]string)
for k14, v14 := range v13.next {
entry := c.env[v14.key]
if (entry.dtype.baseType() == TypeString.baseType()) && (entry.parsed.err == nil) {
retval[k11][k12][k13][k14] = entry.parsed.strval
} else {
return
}
}
}
}
}
return retval
}
}
return
}
func (c *Config) GetMapMapMapMapDirectory(key string) (empty map[string]map[string]map[string]map[string]string) {
empty = make(map[string]map[string]map[string]map[string]string)
retval := make(map[string]map[string]map[string]map[string]string)
if c.parsed {
key = cleanKey(key)
v10, ok := c.mapEnv[3].next[key]
if ok {
for k11, v11 := range v10.next {
retval[k11] = make(map[string]map[string]map[string]string)
for k12, v12 := range v11.next {
retval[k11][k12] = make(map[string]map[string]string)
for k13, v13 := range v12.next {
retval[k11][k12][k13] = make(map[string]string)
for k14, v14 := range v13.next {
entry := c.env[v14.key]
if (entry.dtype.baseType() == TypeDirectory.baseType()) && (entry.parsed.err == nil) {
retval[k11][k12][k13][k14] = entry.parsed.strval
} else {
return
}
}
}
}
}
return retval
}
}
return
}
func (c *Config) GetMapMapMapMapBool(key string) (empty map[string]map[string]map[string]map[string]bool) {
empty = make(map[string]map[string]map[string]map[string]bool)
retval := make(map[string]map[string]map[string]map[string]bool)
if c.parsed {
key = cleanKey(key)
v10, ok := c.mapEnv[3].next[key]
if ok {
for k11, v11 := range v10.next {
retval[k11] = make(map[string]map[string]map[string]bool)
for k12, v12 := range v11.next {
retval[k11][k12] = make(map[string]map[string]bool)
for k13, v13 := range v12.next {
retval[k11][k12][k13] = make(map[string]bool)
for k14, v14 := range v13.next {
entry := c.env[v14.key]
if (entry.dtype.baseType() == TypeBool.baseType()) && (entry.parsed.err == nil) {
retval[k11][k12][k13][k14] = entry.parsed.boolval
} else {
return
}
}
}
}
}
return retval
}
}
return
}
func (c *Config) GetMapMapMapMapHex(key string) (empty map[string]map[string]map[string]map[string][]byte) {
empty = make(map[string]map[string]map[string]map[string][]byte)
retval := make(map[string]map[string]map[string]map[string][]byte)
if c.parsed {
key = cleanKey(key)
v10, ok := c.mapEnv[3].next[key]
if ok {
for k11, v11 := range v10.next {
retval[k11] = make(map[string]map[string]map[string][]byte)
for k12, v12 := range v11.next {
retval[k11][k12] = make(map[string]map[string][]byte)
for k13, v13 := range v12.next {
retval[k11][k12][k13] = make(map[string][]byte)
for k14, v14 := range v13.next {
entry := c.env[v14.key]
if (entry.dtype.baseType() == TypeHex.baseType()) && (entry.parsed.err == nil) {
retval[k11][k12][k13][k14] = entry.parsed.binval
} else {
return
}
}
}
}
}
return retval
}
}
return
}
func (c *Config) GetMapMapMapMapMapInt(key string) (empty map[string]map[string]map[string]map[string]map[string]int64) {
empty = make(map[string]map[string]map[string]map[string]map[string]int64)
retval := make(map[string]map[string]map[string]map[string]map[string]int64)
if c.parsed {
key = cleanKey(key)
v10, ok := c.mapEnv[4].next[key]
if ok {
for k11, v11 := range v10.next {
retval[k11] = make(map[string]map[string]map[string]map[string]int64)
for k12, v12 := range v11.next {
retval[k11][k12] = make(map[string]map[string]map[string]int64)
for k13, v13 := range v12.next {
retval[k11][k12][k13] = make(map[string]map[string]int64)
for k14, v14 := range v13.next {
retval[k11][k12][k13][k14] = make(map[string]int64)
for k15, v15 := range v14.next {
entry := c.env[v15.key]
if (entry.dtype.baseType() == TypeInt.baseType()) && (entry.parsed.err == nil) {
retval[k11][k12][k13][k14][k15] = entry.parsed.intval
} else {
return
}
}
}
}
}
}
return retval
}
}
return
}
func (c *Config) GetMapMapMapMapMapMetric(key string) (empty map[string]map[string]map[string]map[string]map[string]int64) {
empty = make(map[string]map[string]map[string]map[string]map[string]int64)
retval := make(map[string]map[string]map[string]map[string]map[string]int64)
if c.parsed {
key = cleanKey(key)
v10, ok := c.mapEnv[4].next[key]
if ok {
for k11, v11 := range v10.next {
retval[k11] = make(map[string]map[string]map[string]map[string]int64)
for k12, v12 := range v11.next {
retval[k11][k12] = make(map[string]map[string]map[string]int64)
for k13, v13 := range v12.next {
retval[k11][k12][k13] = make(map[string]map[string]int64)
for k14, v14 := range v13.next {
retval[k11][k12][k13][k14] = make(map[string]int64)
for k15, v15 := range v14.next {
entry := c.env[v15.key]
if (entry.dtype.baseType() == TypeMetric.baseType()) && (entry.parsed.err == nil) {
retval[k11][k12][k13][k14][k15] = entry.parsed.intval
} else {
return
}
}
}
}
}
}
return retval
}
}
return
}
func (c *Config) GetMapMapMapMapMapDuration(key string) (empty map[string]map[string]map[string]map[string]map[string]time.Duration) {
empty = make(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)
if c.parsed {
key = cleanKey(key)
v10, ok := c.mapEnv[4].next[key]
if ok {
for k11, v11 := range v10.next {
retval[k11] = make(map[string]map[string]map[string]map[string]time.Duration)
for k12, v12 := range v11.next {
retval[k11][k12] = make(map[string]map[string]map[string]time.Duration)
for k13, v13 := range v12.next {
retval[k11][k12][k13] = make(map[string]map[string]time.Duration)
for k14, v14 := range v13.next {
retval[k11][k12][k13][k14] = make(map[string]time.Duration)
for k15, v15 := range v14.next {
entry := c.env[v15.key]
if (entry.dtype.baseType() == TypeDuration.baseType()) && (entry.parsed.err == nil) {
retval[k11][k12][k13][k14][k15] = entry.parsed.durval
} else {
return
}
}
}
}
}
}
return retval
}
}
return
}
func (c *Config) GetMapMapMapMapMapString(key string) (empty map[string]map[string]map[string]map[string]map[string]string) {
empty = make(map[string]map[string]map[string]map[string]map[string]string)
retval := make(map[string]map[string]map[string]map[string]map[string]string)
if c.parsed {
key = cleanKey(key)
v10, ok := c.mapEnv[4].next[key]
if ok {
for k11, v11 := range v10.next {
retval[k11] = make(map[string]map[string]map[string]map[string]string)
for k12, v12 := range v11.next {
retval[k11][k12] = make(map[string]map[string]map[string]string)
for k13, v13 := range v12.next {
retval[k11][k12][k13] = make(map[string]map[string]string)
for k14, v14 := range v13.next {
retval[k11][k12][k13][k14] = make(map[string]string)
for k15, v15 := range v14.next {
entry := c.env[v15.key]
if (entry.dtype.baseType() == TypeString.baseType()) && (entry.parsed.err == nil) {
retval[k11][k12][k13][k14][k15] = entry.parsed.strval
} else {
return
}
}
}
}
}
}
return retval
}
}
return
}
func (c *Config) GetMapMapMapMapMapDirectory(key string) (empty map[string]map[string]map[string]map[string]map[string]string) {
empty = make(map[string]map[string]map[string]map[string]map[string]string)
retval := make(map[string]map[string]map[string]map[string]map[string]string)
if c.parsed {
key = cleanKey(key)
v10, ok := c.mapEnv[4].next[key]
if ok {
for k11, v11 := range v10.next {
retval[k11] = make(map[string]map[string]map[string]map[string]string)
for k12, v12 := range v11.next {
retval[k11][k12] = make(map[string]map[string]map[string]string)
for k13, v13 := range v12.next {
retval[k11][k12][k13] = make(map[string]map[string]string)
for k14, v14 := range v13.next {
retval[k11][k12][k13][k14] = make(map[string]string)
for k15, v15 := range v14.next {
entry := c.env[v15.key]
if (entry.dtype.baseType() == TypeDirectory.baseType()) && (entry.parsed.err == nil) {
retval[k11][k12][k13][k14][k15] = entry.parsed.strval
} else {
return
}
}
}
}
}
}
return retval
}
}
return
}
func (c *Config) GetMapMapMapMapMapBool(key string) (empty map[string]map[string]map[string]map[string]map[string]bool) {
empty = make(map[string]map[string]map[string]map[string]map[string]bool)
retval := make(map[string]map[string]map[string]map[string]map[string]bool)
if c.parsed {
key = cleanKey(key)
v10, ok := c.mapEnv[4].next[key]
if ok {
for k11, v11 := range v10.next {
retval[k11] = make(map[string]map[string]map[string]map[string]bool)
for k12, v12 := range v11.next {
retval[k11][k12] = make(map[string]map[string]map[string]bool)
for k13, v13 := range v12.next {
retval[k11][k12][k13] = make(map[string]map[string]bool)
for k14, v14 := range v13.next {
retval[k11][k12][k13][k14] = make(map[string]bool)
for k15, v15 := range v14.next {
entry := c.env[v15.key]
if (entry.dtype.baseType() == TypeBool.baseType()) && (entry.parsed.err == nil) {
retval[k11][k12][k13][k14][k15] = entry.parsed.boolval
} else {
return
}
}
}
}
}
}
return retval
}
}
return
}
func (c *Config) GetMapMapMapMapMapHex(key string) (empty map[string]map[string]map[string]map[string]map[string][]byte) {
empty = make(map[string]map[string]map[string]map[string]map[string][]byte)
retval := make(map[string]map[string]map[string]map[string]map[string][]byte)
if c.parsed {
key = cleanKey(key)
v10, ok := c.mapEnv[4].next[key]
if ok {
for k11, v11 := range v10.next {
retval[k11] = make(map[string]map[string]map[string]map[string][]byte)
for k12, v12 := range v11.next {
retval[k11][k12] = make(map[string]map[string]map[string][]byte)
for k13, v13 := range v12.next {
retval[k11][k12][k13] = make(map[string]map[string][]byte)
for k14, v14 := range v13.next {
retval[k11][k12][k13][k14] = make(map[string][]byte)
for k15, v15 := range v14.next {
entry := c.env[v15.key]
if (entry.dtype.baseType() == TypeHex.baseType()) && (entry.parsed.err == nil) {
retval[k11][k12][k13][k14][k15] = entry.parsed.binval
} else {
return
}
}
}
}
}
}
return retval
}
}
return
}

View File

@ -1,22 +1,40 @@
package envconf
import (
"fmt"
"time"
)
type DataType int
type DataType uint32
const (
TypeNone DataType = iota
TypeInt DataType = iota
TypeMetric DataType = iota
TypeDuration DataType = iota
TypeString DataType = iota
TypeDirectory DataType = iota
TypeBool DataType = iota
TypeHex DataType = iota
sizeBitmask DataType = 0xffff
typeBitmask DataType = sizeBitmask << 16
)
const (
TypeNone DataType = iota << 16
TypeInt DataType = iota << 16
TypeMetric DataType = iota << 16
TypeDuration DataType = iota << 16
TypeString DataType = iota << 16
TypeDirectory DataType = iota << 16
TypeBool DataType = iota << 16
TypeHex DataType = iota << 16
)
func FixedHex(size uint16) DataType {
return TypeHex | DataType(size)
}
func (dtype DataType) baseType() DataType {
return dtype & typeBitmask
}
func (dtype DataType) typeAndSize() (DataType, int) {
return (dtype & typeBitmask), int(dtype & sizeBitmask)
}
type cValue struct {
dtype DataType
intval int64
@ -28,18 +46,24 @@ type cValue struct {
}
func (dtype DataType) parse(key string, str string) (ret cValue) {
info, ok := tInfo[dtype]
rdtype, size := dtype.typeAndSize()
info, ok := tInfo[rdtype]
if ok {
ret = info.parser(key, str)
ret = info.parser(key, str, size)
if len(ret.binval) == 0 {
ret.binval = make([]byte, 0, 0)
}
}
return
}
func (dtype DataType) String() string {
info, ok := tInfo[dtype]
rdtype, size := dtype.typeAndSize()
info, ok := tInfo[rdtype]
if ok {
if size > 0 {
return fmt.Sprintf("%s%d", info.name, size)
}
return info.name
}
return "invalid"

View File

@ -20,10 +20,48 @@ type cEntry struct {
hasdef bool // Default value is defined
}
type keyLookupType struct {
key string
end bool
next map[string]*keyLookupType
}
func (kl *keyLookupType) setKey(parts []string, key string) {
if len(kl.next) == 0 {
kl.next = make(map[string]*keyLookupType)
}
if len(parts) > 0 {
firstPart := parts[0]
nextLookup, ok := kl.next[firstPart]
if ok {
nextLookup.setKey(parts[1:], key)
} else {
nextLookup := new(keyLookupType)
nextLookup.setKey(parts[1:], key)
kl.next[firstPart] = nextLookup
}
} else {
kl.key = key
kl.end = true
}
}
func (kl *keyLookupType) print() {
if kl.end {
fmt.Println(kl.key)
fmt.Println("-----------------")
return
}
for key, val := range kl.next {
fmt.Println(key)
val.print()
}
}
type Config struct {
parsed bool
env map[string]cEntry
mapEnv map[string]map[string]cEntry
mapEnv *mapEnvType
}
// NewConfig returns an envconf.Config that is used to read configuration from environment variables.
@ -33,27 +71,42 @@ 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 = new(mapEnvType)
for level, _ := range config.mapEnv {
config.mapEnv[level] = new(keyLookupType)
}
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, "_")
maxParts := len(splitted)
splitted = append(splitted, make([]string, len(config.mapEnv), len(config.mapEnv))...)
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 level := 0; level < len(config.mapEnv); level++ {
for count := 0; count < maxParts-1-level; count++ {
parts := make([]string, level+2, level+2)
lastSplit := len(parts) - 1 + count
config.mapEnv[left][right] = entry
for partPos, _ := range parts {
parts[partPos] = splitted[partPos+count]
}
parts[0] = strings.Trim(strings.Join(splitted[:count+1], "_"), "_")
parts[len(parts)-1] = strings.Trim(strings.Join(splitted[lastSplit:], "_"), "_")
config.mapEnv[level].setKey(parts, key)
}
}
}
}
}
@ -82,11 +135,12 @@ func (c *Config) Define(key string, dtype DataType) {
// Variables without a defined type will be ignored by Parse.
func (c *Config) DefineMap(key string, dtype DataType) {
key = cleanKey(key)
entries, ok := c.mapEnv[key]
entries, ok := c.mapEnv[0].next[key]
if ok {
for mapKey, entry := range entries {
entry.dtype = dtype
c.mapEnv[key][mapKey] = entry
for _, entry := range entries.next {
defEntry := c.env[entry.key]
defEntry.dtype = dtype
c.env[entry.key] = defEntry
}
}
}
@ -136,13 +190,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 +281,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 {
@ -283,37 +320,17 @@ func keySplit(key string) (left string, right string, ok bool) {
func (c *Config) getRaw(key string, dtype DataType) (val cValue) {
val.dtype = TypeNone
val.binval = make([]byte, 0, 0)
if c.parsed {
key = cleanKey(key)
entry, ok := c.env[key]
if ok && (entry.dtype == dtype) {
if ok && (entry.dtype.baseType() == dtype.baseType()) {
return entry.parsed
}
}
return
}
func (c *Config) getRawMap(key string, dtype DataType) (empty map[string]cValue) {
empty = make(map[string]cValue)
retval := make(map[string]cValue)
if c.parsed {
key = cleanKey(key)
entries, ok := c.mapEnv[key]
if ok {
for k, v := range entries {
if (v.dtype == dtype) && (v.parsed.err == nil) {
retval[k] = v.parsed
} else {
return
}
}
return retval
}
}
return
}
// Returns the value of an environment variable.
// If the variable is not defined as envconf.TypeInt the function will return 0.
func (c *Config) GetInt(key string) int64 {
@ -367,46 +384,6 @@ func (c *Config) GetBool(key string) bool {
return val.boolval
}
func (c *Config) GetMapInt(key string) (retval map[string]int64) {
retval = make(map[string]int64)
for k, v := range c.getRawMap(key, TypeInt) {
retval[k] = v.intval
}
return
}
func (c *Config) GetMapDuration(key string) (retval map[string]time.Duration) {
retval = make(map[string]time.Duration)
for k, v := range c.getRawMap(key, TypeDuration) {
retval[k] = v.durval
}
return
}
func (c *Config) GetMapString(key string) (retval map[string]string) {
retval = make(map[string]string)
for k, v := range c.getRawMap(key, TypeString) {
retval[k] = v.strval
}
return
}
func (c *Config) GetMapBool(key string) (retval map[string]bool) {
retval = make(map[string]bool)
for k, v := range c.getRawMap(key, TypeBool) {
retval[k] = v.boolval
}
return
}
func (c *Config) GetMapHex(key string) (retval map[string][]byte) {
retval = make(map[string][]byte)
for k, v := range c.getRawMap(key, TypeHex) {
retval[k] = v.binval
}
return
}
func getFirstRune(str string) rune {
for _, v := range str {
return v

155
generate.py Executable file
View File

@ -0,0 +1,155 @@
#!/usr/bin/env python3
import typing
import tempfile
import os.path
import os
import shutil
import subprocess
from dataclasses import dataclass
@dataclass
class DataType:
const_type: str
go_type: str
sig_type: str
value_type: str
def get_return_type(levels: int, dtype: str) -> str:
return ("map[string]" * levels) + dtype
def get_signature(levels: int, data_type: DataType) -> str:
return (
"func (c *Config) Get"
+ ("Map" * levels)
+ data_type.sig_type
+ "(key string) (empty "
+ get_return_type(levels, data_type.go_type)
+ ") {"
)
def get_empty_def(levels: int, data_type: DataType) -> str:
return "empty = make(" + get_return_type(levels, data_type.go_type) + ")"
def get_nested_keys(levels: int) -> str:
txt = ""
for count in range(levels):
txt += "[k" + str(count + 11) + "]"
return txt
def get_retval_def(levels: int, current_level: int, data_type: DataType) -> str:
base = "retval := make("
if current_level > 0:
base = "retval" + get_nested_keys(current_level) + " = make("
return base + get_return_type(levels - current_level, data_type.go_type) + ")"
def get_entries(levels: int) -> str:
return f"v10, ok := c.mapEnv[{levels-1}].next[key]"
def get_entry(levels: int) -> str:
return f"entry := c.env[v{levels + 11}.key]"
def set_value(levels: int, data_type: DataType) -> str:
return f"retval{get_nested_keys(levels + 1)} = entry.parsed.{data_type.value_type}"
def get_loop(levels: int) -> str:
return f"for k{levels + 11}, v{levels + 11} := range v{levels + 10}.next {{"
def get_function(file: typing.TextIO, levels: int, data_type: DataType):
print(get_signature(levels, data_type), file=file)
print(get_empty_def(levels, data_type), file=file)
print(get_retval_def(levels, 0, data_type), file=file)
print("if c.parsed {", file=file)
print("key = cleanKey(key)", file=file)
print(get_entries(levels), file=file)
print("if ok {", file=file)
for level in range(levels - 1):
print(get_loop(level), file=file)
print(get_retval_def(levels, level + 1, data_type), file=file)
print(get_loop(levels - 1), file=file)
print(get_entry(levels - 1), file=file)
print(
f"if (entry.dtype.baseType() == {data_type.const_type}.baseType()) && (entry.parsed.err == nil) {{",
file=file,
)
print(set_value(levels - 1, data_type), file=file)
print("} else {", file=file)
print("return", file=file)
print("};" * (levels + 1), file=file)
print("return retval", file=file)
print("}", file=file)
print("}", file=file)
print("return", file=file)
print("}", file=file)
def main():
data_types = [
DataType(
const_type="TypeInt", go_type="int64", sig_type="Int", value_type="intval"
),
DataType(
const_type="TypeMetric",
go_type="int64",
sig_type="Metric",
value_type="intval",
),
DataType(
const_type="TypeDuration",
go_type="time.Duration",
sig_type="Duration",
value_type="durval",
),
DataType(
const_type="TypeString",
go_type="string",
sig_type="String",
value_type="strval",
),
DataType(
const_type="TypeDirectory",
go_type="string",
sig_type="Directory",
value_type="strval",
),
DataType(
const_type="TypeBool", go_type="bool", sig_type="Bool", value_type="boolval"
),
DataType(
const_type="TypeHex", go_type="[]byte", sig_type="Hex", value_type="binval"
),
]
levels = 5
with tempfile.TemporaryDirectory() as pathname:
code_file = os.path.join(pathname, "code.go")
with open(code_file, mode="w", encoding="utf-8") as f:
print("package envconf", file=f)
print('import "time"', file=f)
print(f"type mapEnvType [{levels}]*keyLookupType", file=f)
for level in range(levels):
for data_type in data_types:
get_function(f, level + 1, data_type)
if subprocess.run(["go", "fmt", code_file], check=True).returncode == 0:
shutil.copyfile(
code_file,
os.path.join(
os.path.dirname(os.path.realpath(__file__)), "auto_generated.go"
),
follow_symlinks=False,
)
if __name__ == "__main__":
main()

View File

@ -11,7 +11,7 @@ import (
"time"
)
func parseInt(key string, str string) (ret cValue) {
func parseInt(key string, str string, _ int) (ret cValue) {
val, err := strconv.ParseInt(str, 10, 64)
if err == nil {
ret.intval = val
@ -21,7 +21,7 @@ func parseInt(key string, str string) (ret cValue) {
return
}
func parseMetric(key string, str string) (ret cValue) {
func parseMetric(key string, str string, _ int) (ret cValue) {
mod := int64(1)
str = strings.ToUpper(str)
if strings.HasSuffix(str, "K") {
@ -46,7 +46,7 @@ func parseMetric(key string, str string) (ret cValue) {
return
}
func parseDuration(key string, str string) (ret cValue) {
func parseDuration(key string, str string, _ int) (ret cValue) {
val, err := time.ParseDuration(str)
if err == nil {
ret.durval = val
@ -56,7 +56,7 @@ func parseDuration(key string, str string) (ret cValue) {
return
}
func parseBool(key string, str string) (ret cValue) {
func parseBool(key string, str string, _ int) (ret cValue) {
val, err := strconv.ParseBool(str)
if err == nil {
ret.boolval = val
@ -66,17 +66,21 @@ func parseBool(key string, str string) (ret cValue) {
return
}
func parseHex(key string, str string) (ret cValue) {
func parseHex(key string, str string, size int) (ret cValue) {
val, err := hex.DecodeString(str)
if err == nil {
if err == nil && (size == 0 || size == len(val)) {
ret.binval = val
} else {
ret.err = errors.New(fmt.Sprintf(`Environment variable "%s" is not of type hex.`, key))
if size == 0 {
ret.err = errors.New(fmt.Sprintf(`Environment variable "%s" is not of type hex.`, key))
} else {
ret.err = errors.New(fmt.Sprintf(`Environment variable "%s" is not of type hex%d.`, key, size))
}
}
return
}
func parseDirectory(_ string, str string) (ret cValue) {
func parseDirectory(_ string, str string, _ int) (ret cValue) {
wd, err := os.Getwd()
if err == nil {
if path.IsAbs(str) {
@ -90,7 +94,7 @@ func parseDirectory(_ string, str string) (ret cValue) {
return
}
func parseString(_ string, str string) (ret cValue) {
func parseString(_ string, str string, _ int) (ret cValue) {
ret.strval = str
return
}

31
tests/build.sh Executable file
View File

@ -0,0 +1,31 @@
#!/usr/bin/env bash
dir=$(dirname $(realpath $0))
export GO111MODULE=on
export GOPROXY=direct
export GOPATH=${dir}/buildenv/deps
mkdir -p ${dir}/buildenv
chmod -R 755 ${dir}/buildenv
rm -rf ${dir}/buildenv
mkdir -p "${dir}/buildenv/src"
mkdir -p ${GOPATH}/src
mkdir -p ${dir}/bin
cp -r "${dir}/main.go" "${dir}/buildenv/src/main.go"
cd ${dir}/buildenv
go mod init src
go mod tidy
package_name=$(ls ${dir}/buildenv/deps/pkg/mod/git.purser.it/roypur | tr -d "\n")
package_dir="${dir}/buildenv/deps/pkg/mod/git.purser.it/roypur/${package_name}"
chmod -R 777 "${dir}/buildenv/deps"
rm -R "${package_dir}"
mkdir -p "${package_dir}"
cp $(dirname ${dir})/*.go ${package_dir}
cd "${dir}"
go build -o "${dir}/bin/test"

16
tests/main.go Normal file
View File

@ -0,0 +1,16 @@
package main
import (
"fmt"
"git.purser.it/roypur/envconf"
)
func main() {
conf := envconf.NewConfig()
conf.Define("this_is_a_map", envconf.TypeInt)
conf.DefineMap("test_map", envconf.TypeInt)
conf.Parse()
conf.Status()
fmt.Println(conf.GetMapMapMapMapMapInt("test_map"))
fmt.Println(conf.GetInt("this_is_a_map"))
}

View File

@ -1,7 +1,7 @@
package envconf
type dataTypeInfo struct {
parser func(string, string) cValue
parser func(string, string, int) cValue
name string
}
@ -15,6 +15,7 @@ func init() {
var strInfo dataTypeInfo
var dirInfo dataTypeInfo
var hexInfo dataTypeInfo
var boolInfo dataTypeInfo
intInfo.name = "int"
@ -30,6 +31,7 @@ func init() {
durInfo.parser = parseDuration
dirInfo.parser = parseDirectory
strInfo.parser = parseString
hexInfo.parser = parseHex
boolInfo.parser = parseBool