diff --git a/datatype.go b/datatype.go index 1bdba01..a94067b 100644 --- a/datatype.go +++ b/datatype.go @@ -15,6 +15,10 @@ const ( TypeDirectory DataType = iota TypeBool DataType = iota TypeHex DataType = iota + TypeHex16 DataType = iota + TypeHex32 DataType = iota + TypeHex64 DataType = iota + TypeHex128 DataType = iota ) type cValue struct { diff --git a/envconf.go b/envconf.go index 6407de4..ba3f150 100644 --- a/envconf.go +++ b/envconf.go @@ -329,6 +329,34 @@ func (c *Config) GetHex(key string) []byte { 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. // If the variable is not defined as envconf.TypeMetric the function will return 0. func (c *Config) GetMetric(key string) int64 { @@ -408,6 +436,38 @@ func (c *Config) GetMapHex(key string) (retval map[string][]byte) { return } +func (c *Config) GetMapHex16(key string) (retval map[string][]byte) { + retval = make(map[string][]byte) + for k, v := range c.getRawMap(key, TypeHex16) { + retval[k] = v.binval + } + return +} + +func (c *Config) GetMapHex32(key string) (retval map[string][]byte) { + retval = make(map[string][]byte) + for k, v := range c.getRawMap(key, TypeHex32) { + retval[k] = v.binval + } + return +} + +func (c *Config) GetMapHex64(key string) (retval map[string][]byte) { + retval = make(map[string][]byte) + for k, v := range c.getRawMap(key, TypeHex64) { + retval[k] = v.binval + } + return +} + +func (c *Config) GetMapHex128(key string) (retval map[string][]byte) { + retval = make(map[string][]byte) + for k, v := range c.getRawMap(key, TypeHex128) { + retval[k] = v.binval + } + return +} + func getFirstRune(str string) rune { for _, v := range str { return v diff --git a/parsers.go b/parsers.go index e7f5cd8..4bf34f8 100644 --- a/parsers.go +++ b/parsers.go @@ -76,6 +76,46 @@ func parseHex(key string, str string) (ret cValue) { return } +func parseHex16(key string, str string) (ret cValue) { + val, err := hex.DecodeString(str) + if err == nil && len(val) == 16 { + ret.binval = val + } else { + ret.err = errors.New(fmt.Sprintf(`Environment variable "%s" is not of type hex16.`, key)) + } + return +} + +func parseHex32(key string, str string) (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() if err == nil { diff --git a/typeinfo.go b/typeinfo.go index 29383dc..bc8cde1 100644 --- a/typeinfo.go +++ b/typeinfo.go @@ -15,6 +15,12 @@ func init() { var strInfo dataTypeInfo var dirInfo dataTypeInfo var hexInfo dataTypeInfo + + var hexInfo16 dataTypeInfo + var hexInfo32 dataTypeInfo + var hexInfo64 dataTypeInfo + var hexInfo128 dataTypeInfo + var boolInfo dataTypeInfo intInfo.name = "int" @@ -30,7 +36,13 @@ func init() { durInfo.parser = parseDuration dirInfo.parser = parseDirectory strInfo.parser = parseString + hexInfo.parser = parseHex + hexInfo16.parser = parseHex16 + hexInfo32.parser = parseHex32 + hexInfo64.parser = parseHex64 + hexInfo128.parser = parseHex128 + boolInfo.parser = parseBool tInfo[TypeInt] = intInfo @@ -39,5 +51,11 @@ func init() { tInfo[TypeString] = strInfo tInfo[TypeDirectory] = dirInfo tInfo[TypeHex] = hexInfo + + tInfo[TypeHex16] = hexInfo16 + tInfo[TypeHex32] = hexInfo32 + tInfo[TypeHex64] = hexInfo64 + tInfo[TypeHex128] = hexInfo128 + tInfo[TypeBool] = boolInfo }