18 Commits

Author SHA1 Message Date
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
8 changed files with 588 additions and 162 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 #!/usr/bin/env bash
go fmt *.go go fmt *.go
git add *.go go fmt tests/*.go
git add *.go tests/*.go
git add tests/*.go

View File

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

View File

@ -23,7 +23,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]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
mapMapMapMapEnv map[string]map[string]map[string]map[string]map[string]string
mapMapMapMapMapEnv map[string]map[string]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 +37,85 @@ 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)
config.mapMapMapMapEnv = make(map[string]map[string]map[string]map[string]map[string]string)
config.mapMapMapMapMapEnv = make(map[string]map[string]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 = append(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) if count < len(splitted)-6 {
var p0, p1, p2, p3, p4, p5 string
p0 = strings.Trim(strings.Join(splitted[:count], "_"), "_")
p1 = strings.Trim(strings.Join(splitted[count:], "_"), "_")
if len(config.mapEnv[p0]) == 0 {
config.mapEnv[p0] = make(map[string]string)
config.mapMapEnv[p0] = make(map[string]map[string]string)
config.mapMapMapEnv[p0] = make(map[string]map[string]map[string]string)
config.mapMapMapMapEnv[p0] = make(map[string]map[string]map[string]map[string]string)
config.mapMapMapMapMapEnv[p0] = make(map[string]map[string]map[string]map[string]map[string]string)
}
if p0 != "" && p1 != "" {
config.mapEnv[p0][p1] = key
} }
config.mapEnv[left][right] = entry p1 = splitted[count]
p2 = strings.Trim(strings.Join(splitted[count+1:], "_"), "_")
if len(config.mapMapEnv[p0][p1]) == 0 {
config.mapMapEnv[p0][p1] = make(map[string]string)
config.mapMapMapEnv[p0][p1] = make(map[string]map[string]string)
config.mapMapMapMapEnv[p0][p1] = make(map[string]map[string]map[string]string)
config.mapMapMapMapMapEnv[p0][p1] = make(map[string]map[string]map[string]map[string]string)
}
if p0 != "" && p1 != "" && p2 != "" {
config.mapMapEnv[p0][p1][p2] = key
}
p2 = splitted[count+1]
p3 = strings.Trim(strings.Join(splitted[count+2:], "_"), "_")
if len(config.mapMapMapEnv[p0][p1][p2]) == 0 {
config.mapMapMapEnv[p0][p1][p2] = make(map[string]string)
config.mapMapMapMapEnv[p0][p1][p2] = make(map[string]map[string]string)
config.mapMapMapMapMapEnv[p0][p1][p2] = make(map[string]map[string]map[string]string)
}
if p0 != "" && p1 != "" && p2 != "" && p3 != "" {
config.mapMapMapEnv[p0][p1][p2][p3] = key
}
p3 = splitted[count+2]
p4 = strings.Trim(strings.Join(splitted[count+3:], "_"), "_")
if len(config.mapMapMapMapEnv[p0][p1][p2][p3]) == 0 {
config.mapMapMapMapEnv[p0][p1][p2][p3] = make(map[string]string)
config.mapMapMapMapMapEnv[p0][p1][p2][p3] = make(map[string]map[string]string)
}
if p0 != "" && p1 != "" && p2 != "" && p3 != "" && p4 != "" {
config.mapMapMapMapEnv[p0][p1][p2][p3][p4] = key
}
p4 = splitted[count+3]
p5 = strings.Trim(strings.Join(splitted[count+4:], "_"), "_")
if len(config.mapMapMapMapMapEnv[p0][p1][p2][p3][p4]) == 0 {
config.mapMapMapMapMapEnv[p0][p1][p2][p3][p4] = make(map[string]string)
}
if p0 != "" && p1 != "" && p2 != "" && p3 != "" && p4 != "" && p5 != "" {
config.mapMapMapMapMapEnv[p0][p1][p2][p3][p4][p5] = key
}
}
}
} }
} }
} }
@ -84,9 +146,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 +199,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 +290,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 {
@ -287,7 +333,7 @@ func (c *Config) getRaw(key string, dtype DataType) (val cValue) {
if c.parsed { if c.parsed {
key = cleanKey(key) key = cleanKey(key)
entry, ok := c.env[key] entry, ok := c.env[key]
if ok && (entry.dtype == dtype) { if ok && (entry.dtype.baseType() == dtype.baseType()) {
return entry.parsed return entry.parsed
} }
} }
@ -303,8 +349,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 == dtype) && (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
} }
@ -315,6 +362,124 @@ func (c *Config) getRawMap(key string, dtype DataType) (empty map[string]cValue)
return return
} }
func (c *Config) getRawMapMap(key string, dtype DataType) (empty map[string]map[string]cValue) {
empty = make(map[string]map[string]cValue)
retval := make(map[string]map[string]cValue)
if c.parsed {
key = cleanKey(key)
entries, ok := c.mapMapEnv[key]
if ok {
for k1, v1 := range entries {
retval[k1] = make(map[string]cValue)
for k2, v2 := range v1 {
entry := c.env[v2]
if (entry.dtype.baseType() == dtype.baseType()) && (entry.parsed.err == nil) {
retval[k1][k2] = entry.parsed
} else {
return
}
}
}
return retval
}
}
return
}
func (c *Config) getRawMapMapMap(key string, dtype DataType) (empty map[string]map[string]map[string]cValue) {
empty = make(map[string]map[string]map[string]cValue)
retval := make(map[string]map[string]map[string]cValue)
if c.parsed {
key = cleanKey(key)
entries, ok := c.mapMapMapEnv[key]
if ok {
for k1, v1 := range entries {
retval[k1] = make(map[string]map[string]cValue)
for k2, v2 := range v1 {
retval[k1][k2] = make(map[string]cValue)
for k3, v3 := range v2 {
entry := c.env[v3]
if (entry.dtype.baseType() == dtype.baseType()) && (entry.parsed.err == nil) {
retval[k1][k2][k3] = entry.parsed
} else {
return
}
}
}
}
return retval
}
}
return
}
func (c *Config) getRawMapMapMapMap(key string, dtype DataType) (empty map[string]map[string]map[string]map[string]cValue) {
empty = make(map[string]map[string]map[string]map[string]cValue)
retval := make(map[string]map[string]map[string]map[string]cValue)
if c.parsed {
key = cleanKey(key)
entries, ok := c.mapMapMapMapEnv[key]
if ok {
for k1, v1 := range entries {
retval[k1] = make(map[string]map[string]map[string]cValue)
for k2, v2 := range v1 {
retval[k1][k2] = make(map[string]map[string]cValue)
for k3, v3 := range v2 {
retval[k1][k2][k3] = make(map[string]cValue)
for k4, v4 := range v3 {
entry := c.env[v4]
if (entry.dtype.baseType() == dtype.baseType()) && (entry.parsed.err == nil) {
retval[k1][k2][k3][k4] = entry.parsed
} else {
return
}
}
}
}
}
return retval
}
}
return
}
func (c *Config) getRawMapMapMapMapMap(key string, dtype DataType) (empty map[string]map[string]map[string]map[string]map[string]cValue) {
empty = make(map[string]map[string]map[string]map[string]map[string]cValue)
retval := make(map[string]map[string]map[string]map[string]map[string]cValue)
if c.parsed {
key = cleanKey(key)
entries, ok := c.mapMapMapMapMapEnv[key]
if ok {
for k1, v1 := range entries {
retval[k1] = make(map[string]map[string]map[string]map[string]cValue)
for k2, v2 := range v1 {
retval[k1][k2] = make(map[string]map[string]map[string]cValue)
for k3, v3 := range v2 {
retval[k1][k2][k3] = make(map[string]map[string]cValue)
for k4, v4 := range v3 {
retval[k1][k2][k3][k4] = make(map[string]cValue)
for k5, v5 := range v4 {
entry := c.env[v5]
if (entry.dtype.baseType() == dtype.baseType()) && (entry.parsed.err == nil) {
retval[k1][k2][k3][k4][k5] = entry.parsed
} else {
return
}
}
}
}
}
}
return retval
}
}
return
}
// Returns the value of an environment variable. // Returns the value of an environment variable.
// If the variable is not defined as envconf.TypeInt the function will return 0. // If the variable is not defined as envconf.TypeInt the function will return 0.
func (c *Config) GetInt(key string) int64 { func (c *Config) GetInt(key string) int64 {
@ -329,34 +494,6 @@ func (c *Config) GetHex(key string) []byte {
return val.binval return val.binval
} }
// Returns the value of an environment variable.
// If the variable is not defined as envconf.TypeHex16 the function will return []byte{}.
func (c *Config) GetHex16(key string) []byte {
val := c.getRaw(key, TypeHex16)
return val.binval
}
// Returns the value of an environment variable.
// If the variable is not defined as envconf.TypeHex32 the function will return []byte{}.
func (c *Config) GetHex32(key string) []byte {
val := c.getRaw(key, TypeHex32)
return val.binval
}
// Returns the value of an environment variable.
// If the variable is not defined as envconf.TypeHex64 the function will return []byte{}.
func (c *Config) GetHex64(key string) []byte {
val := c.getRaw(key, TypeHex64)
return val.binval
}
// Returns the value of an environment variable.
// If the variable is not defined as envconf.TypeHex128 the function will return []byte{}.
func (c *Config) GetHex128(key string) []byte {
val := c.getRaw(key, TypeHex128)
return val.binval
}
// Returns the value of an environment variable. // Returns the value of an environment variable.
// If the variable is not defined as envconf.TypeMetric the function will return 0. // If the variable is not defined as envconf.TypeMetric the function will return 0.
func (c *Config) GetMetric(key string) int64 { func (c *Config) GetMetric(key string) int64 {
@ -436,34 +573,312 @@ func (c *Config) GetMapHex(key string) (retval map[string][]byte) {
return return
} }
func (c *Config) GetMapHex16(key string) (retval map[string][]byte) { func (c *Config) GetMapMapInt(key string) (retval map[string]map[string]int64) {
retval = make(map[string][]byte) retval = make(map[string]map[string]int64)
for k, v := range c.getRawMap(key, TypeHex16) { for k1, v1 := range c.getRawMapMap(key, TypeInt) {
retval[k] = v.binval retval[k1] = make(map[string]int64)
for k2, v2 := range v1 {
retval[k1][k2] = v2.intval
}
} }
return return
} }
func (c *Config) GetMapHex32(key string) (retval map[string][]byte) { func (c *Config) GetMapMapDuration(key string) (retval map[string]map[string]time.Duration) {
retval = make(map[string][]byte) retval = make(map[string]map[string]time.Duration)
for k, v := range c.getRawMap(key, TypeHex32) { for k1, v1 := range c.getRawMapMap(key, TypeDuration) {
retval[k] = v.binval retval[k1] = make(map[string]time.Duration)
for k2, v2 := range v1 {
retval[k1][k2] = v2.durval
}
} }
return return
} }
func (c *Config) GetMapHex64(key string) (retval map[string][]byte) { func (c *Config) GetMapMapString(key string) (retval map[string]map[string]string) {
retval = make(map[string][]byte) retval = make(map[string]map[string]string)
for k, v := range c.getRawMap(key, TypeHex64) { for k1, v1 := range c.getRawMapMap(key, TypeString) {
retval[k] = v.binval retval[k1] = make(map[string]string)
for k2, v2 := range v1 {
retval[k1][k2] = v2.strval
}
} }
return return
} }
func (c *Config) GetMapHex128(key string) (retval map[string][]byte) { func (c *Config) GetMapMapBool(key string) (retval map[string]map[string]bool) {
retval = make(map[string][]byte) retval = make(map[string]map[string]bool)
for k, v := range c.getRawMap(key, TypeHex128) { for k1, v1 := range c.getRawMapMap(key, TypeBool) {
retval[k] = v.binval retval[k1] = make(map[string]bool)
for k2, v2 := range v1 {
retval[k1][k2] = v2.boolval
}
}
return
}
func (c *Config) GetMapMapHex(key string) (retval map[string]map[string][]byte) {
retval = make(map[string]map[string][]byte)
for k1, v1 := range c.getRawMapMap(key, TypeHex) {
retval[k1] = make(map[string][]byte)
for k2, v2 := range v1 {
retval[k1][k2] = v2.binval
}
}
return
}
func (c *Config) GetMapMapMapInt(key string) (retval map[string]map[string]map[string]int64) {
retval = make(map[string]map[string]map[string]int64)
for k1, v1 := range c.getRawMapMapMap(key, TypeInt) {
retval[k1] = make(map[string]map[string]int64)
for k2, v2 := range v1 {
retval[k1][k2] = make(map[string]int64)
for k3, v3 := range v2 {
retval[k1][k2][k3] = v3.intval
}
}
}
return
}
func (c *Config) GetMapMapMapDuration(key string) (retval map[string]map[string]map[string]time.Duration) {
retval = make(map[string]map[string]map[string]time.Duration)
for k1, v1 := range c.getRawMapMapMap(key, TypeDuration) {
retval[k1] = make(map[string]map[string]time.Duration)
for k2, v2 := range v1 {
retval[k1][k2] = make(map[string]time.Duration)
for k3, v3 := range v2 {
retval[k1][k2][k3] = v3.durval
}
}
}
return
}
func (c *Config) GetMapMapMapString(key string) (retval map[string]map[string]map[string]string) {
retval = make(map[string]map[string]map[string]string)
for k1, v1 := range c.getRawMapMapMap(key, TypeString) {
retval[k1] = make(map[string]map[string]string)
for k2, v2 := range v1 {
retval[k1][k2] = make(map[string]string)
for k3, v3 := range v2 {
retval[k1][k2][k3] = v3.strval
}
}
}
return
}
func (c *Config) GetMapMapMapBool(key string) (retval map[string]map[string]map[string]bool) {
retval = make(map[string]map[string]map[string]bool)
for k1, v1 := range c.getRawMapMapMap(key, TypeBool) {
retval[k1] = make(map[string]map[string]bool)
for k2, v2 := range v1 {
retval[k1][k2] = make(map[string]bool)
for k3, v3 := range v2 {
retval[k1][k2][k3] = v3.boolval
}
}
}
return
}
func (c *Config) GetMapMapMapHex(key string) (retval map[string]map[string]map[string][]byte) {
retval = make(map[string]map[string]map[string][]byte)
for k1, v1 := range c.getRawMapMapMap(key, TypeHex) {
retval[k1] = make(map[string]map[string][]byte)
for k2, v2 := range v1 {
retval[k1][k2] = make(map[string][]byte)
for k3, v3 := range v2 {
retval[k1][k2][k3] = v3.binval
}
}
}
return
}
func (c *Config) GetMapMapMapMapInt(key string) (retval map[string]map[string]map[string]map[string]int64) {
retval = make(map[string]map[string]map[string]map[string]int64)
for k1, v1 := range c.getRawMapMapMapMap(key, TypeInt) {
retval[k1] = make(map[string]map[string]map[string]int64)
for k2, v2 := range v1 {
retval[k1][k2] = make(map[string]map[string]int64)
for k3, v3 := range v2 {
retval[k1][k2][k3] = make(map[string]int64)
for k4, v4 := range v3 {
retval[k1][k2][k3][k4] = v4.intval
}
}
}
}
return
}
func (c *Config) GetMapMapMapMapDuration(key string) (retval map[string]map[string]map[string]map[string]time.Duration) {
retval = make(map[string]map[string]map[string]map[string]time.Duration)
for k1, v1 := range c.getRawMapMapMapMap(key, TypeDuration) {
retval[k1] = make(map[string]map[string]map[string]time.Duration)
for k2, v2 := range v1 {
retval[k1][k2] = make(map[string]map[string]time.Duration)
for k3, v3 := range v2 {
retval[k1][k2][k3] = make(map[string]time.Duration)
for k4, v4 := range v3 {
retval[k1][k2][k3][k4] = v4.durval
}
}
}
}
return
}
func (c *Config) GetMapMapMapMapString(key string) (retval map[string]map[string]map[string]map[string]string) {
retval = make(map[string]map[string]map[string]map[string]string)
for k1, v1 := range c.getRawMapMapMapMap(key, TypeString) {
retval[k1] = make(map[string]map[string]map[string]string)
for k2, v2 := range v1 {
retval[k1][k2] = make(map[string]map[string]string)
for k3, v3 := range v2 {
retval[k1][k2][k3] = make(map[string]string)
for k4, v4 := range v3 {
retval[k1][k2][k3][k4] = v4.strval
}
}
}
}
return
}
func (c *Config) GetMapMapMapMapBool(key string) (retval map[string]map[string]map[string]map[string]bool) {
retval = make(map[string]map[string]map[string]map[string]bool)
for k1, v1 := range c.getRawMapMapMapMap(key, TypeBool) {
retval[k1] = make(map[string]map[string]map[string]bool)
for k2, v2 := range v1 {
retval[k1][k2] = make(map[string]map[string]bool)
for k3, v3 := range v2 {
retval[k1][k2][k3] = make(map[string]bool)
for k4, v4 := range v3 {
retval[k1][k2][k3][k4] = v4.boolval
}
}
}
}
return
}
func (c *Config) GetMapMapMapMapHex(key string) (retval map[string]map[string]map[string]map[string][]byte) {
retval = make(map[string]map[string]map[string]map[string][]byte)
for k1, v1 := range c.getRawMapMapMapMap(key, TypeHex) {
retval[k1] = make(map[string]map[string]map[string][]byte)
for k2, v2 := range v1 {
retval[k1][k2] = make(map[string]map[string][]byte)
for k3, v3 := range v2 {
retval[k1][k2][k3] = make(map[string][]byte)
for k4, v4 := range v3 {
retval[k1][k2][k3][k4] = v4.binval
}
}
}
}
return
}
func (c *Config) GetMapMapMapMapMapInt(key string) (retval map[string]map[string]map[string]map[string]map[string]int64) {
retval = make(map[string]map[string]map[string]map[string]map[string]int64)
for k1, v1 := range c.getRawMapMapMapMapMap(key, TypeInt) {
retval[k1] = make(map[string]map[string]map[string]map[string]int64)
for k2, v2 := range v1 {
retval[k1][k2] = make(map[string]map[string]map[string]int64)
for k3, v3 := range v2 {
retval[k1][k2][k3] = make(map[string]map[string]int64)
for k4, v4 := range v3 {
retval[k1][k2][k3][k4] = make(map[string]int64)
for k5, v5 := range v4 {
retval[k1][k2][k3][k4][k5] = v5.intval
}
}
}
}
}
return
}
func (c *Config) GetMapMapMapMapMapDuration(key string) (retval 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)
for k1, v1 := range c.getRawMapMapMapMapMap(key, TypeDuration) {
retval[k1] = make(map[string]map[string]map[string]map[string]time.Duration)
for k2, v2 := range v1 {
retval[k1][k2] = make(map[string]map[string]map[string]time.Duration)
for k3, v3 := range v2 {
retval[k1][k2][k3] = make(map[string]map[string]time.Duration)
for k4, v4 := range v3 {
retval[k1][k2][k3][k4] = make(map[string]time.Duration)
for k5, v5 := range v4 {
retval[k1][k2][k3][k4][k5] = v5.durval
}
}
}
}
}
return
}
func (c *Config) GetMapMapMapMapMapString(key string) (retval map[string]map[string]map[string]map[string]map[string]string) {
retval = make(map[string]map[string]map[string]map[string]map[string]string)
for k1, v1 := range c.getRawMapMapMapMapMap(key, TypeString) {
retval[k1] = make(map[string]map[string]map[string]map[string]string)
for k2, v2 := range v1 {
retval[k1][k2] = make(map[string]map[string]map[string]string)
for k3, v3 := range v2 {
retval[k1][k2][k3] = make(map[string]map[string]string)
for k4, v4 := range v3 {
retval[k1][k2][k3][k4] = make(map[string]string)
for k5, v5 := range v4 {
retval[k1][k2][k3][k4][k5] = v5.strval
}
}
}
}
}
return
}
func (c *Config) GetMapMapMapMapMapBool(key string) (retval map[string]map[string]map[string]map[string]map[string]bool) {
retval = make(map[string]map[string]map[string]map[string]map[string]bool)
for k1, v1 := range c.getRawMapMapMapMapMap(key, TypeBool) {
retval[k1] = make(map[string]map[string]map[string]map[string]bool)
for k2, v2 := range v1 {
retval[k1][k2] = make(map[string]map[string]map[string]bool)
for k3, v3 := range v2 {
retval[k1][k2][k3] = make(map[string]map[string]bool)
for k4, v4 := range v3 {
retval[k1][k2][k3][k4] = make(map[string]bool)
for k5, v5 := range v4 {
retval[k1][k2][k3][k4][k5] = v5.boolval
}
}
}
}
}
return
}
func (c *Config) GetMapMapMapMapMapHex(key string) (retval map[string]map[string]map[string]map[string]map[string][]byte) {
retval = make(map[string]map[string]map[string]map[string]map[string][]byte)
for k1, v1 := range c.getRawMapMapMapMapMap(key, TypeHex) {
retval[k1] = make(map[string]map[string]map[string]map[string][]byte)
for k2, v2 := range v1 {
retval[k1][k2] = make(map[string]map[string]map[string][]byte)
for k3, v3 := range v2 {
retval[k1][k2][k3] = make(map[string]map[string][]byte)
for k4, v4 := range v3 {
retval[k1][k2][k3][k4] = make(map[string][]byte)
for k5, v5 := range v4 {
retval[k1][k2][k3][k4][k5] = v5.binval
}
}
}
}
} }
return return
} }

View File

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

22
tests/build.sh Executable file
View File

@ -0,0 +1,22 @@
#!/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
go build -o "${dir}/bin/test" "src/main.go"

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.GetMapMapMapInt("test_map"))
fmt.Println(conf.GetInt("this_is_a_map"))
}

View File

@ -1,7 +1,7 @@
package envconf package envconf
type dataTypeInfo struct { type dataTypeInfo struct {
parser func(string, string) cValue parser func(string, string, int) cValue
name string name string
} }
@ -16,11 +16,6 @@ func init() {
var dirInfo dataTypeInfo var dirInfo dataTypeInfo
var hexInfo dataTypeInfo var hexInfo dataTypeInfo
var hexInfo16 dataTypeInfo
var hexInfo32 dataTypeInfo
var hexInfo64 dataTypeInfo
var hexInfo128 dataTypeInfo
var boolInfo dataTypeInfo var boolInfo dataTypeInfo
intInfo.name = "int" intInfo.name = "int"
@ -38,11 +33,6 @@ func init() {
strInfo.parser = parseString strInfo.parser = parseString
hexInfo.parser = parseHex hexInfo.parser = parseHex
hexInfo16.parser = parseHex16
hexInfo32.parser = parseHex32
hexInfo64.parser = parseHex64
hexInfo128.parser = parseHex128
boolInfo.parser = parseBool boolInfo.parser = parseBool
tInfo[TypeInt] = intInfo tInfo[TypeInt] = intInfo
@ -51,11 +41,5 @@ func init() {
tInfo[TypeString] = strInfo tInfo[TypeString] = strInfo
tInfo[TypeDirectory] = dirInfo tInfo[TypeDirectory] = dirInfo
tInfo[TypeHex] = hexInfo tInfo[TypeHex] = hexInfo
tInfo[TypeHex16] = hexInfo16
tInfo[TypeHex32] = hexInfo32
tInfo[TypeHex64] = hexInfo64
tInfo[TypeHex128] = hexInfo128
tInfo[TypeBool] = boolInfo tInfo[TypeBool] = boolInfo
} }