add hex type

This commit is contained in:
Roy Olav Purser 2022-01-16 18:24:24 +01:00
parent afa50b4fca
commit d1bcc8c4cc
Signed by: roypur
GPG Key ID: E14D26A036F21656
4 changed files with 42 additions and 7 deletions

View File

@ -14,6 +14,7 @@ const (
TypeString DataType = iota TypeString DataType = iota
TypeDirectory DataType = iota TypeDirectory DataType = iota
TypeBool DataType = iota TypeBool DataType = iota
TypeHex DataType = iota
) )
type cValue struct { type cValue struct {
@ -21,6 +22,7 @@ type cValue struct {
intval int64 intval int64
durval time.Duration durval time.Duration
boolval bool boolval bool
binval []byte
strval string strval string
err error err error
} }
@ -28,7 +30,10 @@ 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] info, ok := tInfo[dtype]
if ok { if ok {
return info.parser(key, str) ret = info.parser(key, str)
if len(ret.binval) == 0 {
ret.binval = make([]byte, 0, 0)
}
} }
return return
} }

View File

@ -314,21 +314,28 @@ func (c *Config) getRawMap(key string, dtype DataType) (empty map[string]cValue)
return return
} }
// GetInt 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 {
val := c.getRaw(key, TypeInt) val := c.getRaw(key, TypeInt)
return val.intval return val.intval
} }
// GetMetric returns the value of an environment variable. // Returns the value of an environment variable.
// If the variable is not defined as envconf.TypeHex the function will return []byte{}.
func (c *Config) GetHex(key string) []byte {
val := c.getRaw(key, TypeHex)
return val.binval
}
// 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 {
val := c.getRaw(key, TypeMetric) val := c.getRaw(key, TypeMetric)
return val.intval return val.intval
} }
// GetDirectory returns the value of an environment variable. // Returns the value of an environment variable.
// If the variable is not defined as envconf.TypeDirectory the // If the variable is not defined as envconf.TypeDirectory the
// function will return the empty string. // function will return the empty string.
func (c *Config) GetDirectory(key string) string { func (c *Config) GetDirectory(key string) string {
@ -336,7 +343,7 @@ func (c *Config) GetDirectory(key string) string {
return val.strval return val.strval
} }
// GetString returns the value of an environment variable. // Returns the value of an environment variable.
// If the variable is not defined as envconf.TypeString the // If the variable is not defined as envconf.TypeString the
// function will return the empty string. // function will return the empty string.
func (c *Config) GetString(key string) string { func (c *Config) GetString(key string) string {
@ -344,7 +351,7 @@ func (c *Config) GetString(key string) string {
return val.strval return val.strval
} }
// GetDuration returns the value of an environment variable. // Returns the value of an environment variable.
// If the variable is not defined as envconf.TypeDuration the // If the variable is not defined as envconf.TypeDuration the
// function will return time.Duration(0). // function will return time.Duration(0).
func (c *Config) GetDuration(key string) time.Duration { func (c *Config) GetDuration(key string) time.Duration {
@ -352,7 +359,7 @@ func (c *Config) GetDuration(key string) time.Duration {
return val.durval return val.durval
} }
// GetBool returns the value of an environment variable. // Returns the value of an environment variable.
// If the variable is not defined as envconf.TypeBool the // If the variable is not defined as envconf.TypeBool the
// function will return false. // function will return false.
func (c *Config) GetBool(key string) bool { func (c *Config) GetBool(key string) bool {
@ -392,6 +399,14 @@ func (c *Config) GetMapBool(key string) (retval map[string]bool) {
return 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 { func getFirstRune(str string) rune {
for _, v := range str { for _, v := range str {
return v return v

View File

@ -1,6 +1,7 @@
package envconf package envconf
import ( import (
"encoding/hex"
"errors" "errors"
"fmt" "fmt"
"os" "os"
@ -65,6 +66,16 @@ func parseBool(key string, str string) (ret cValue) {
return return
} }
func parseHex(key string, str string) (ret cValue) {
val, err := hex.DecodeString(str)
if err == nil {
ret.binval = val
} else {
ret.err = errors.New(fmt.Sprintf(`Environment variable "%s" is not of type hex.`, key))
}
return
}
func parseDirectory(_ string, str string) (ret cValue) { func parseDirectory(_ string, str string) (ret cValue) {
wd, err := os.Getwd() wd, err := os.Getwd()
if err == nil { if err == nil {

View File

@ -14,6 +14,7 @@ func init() {
var durInfo dataTypeInfo var durInfo dataTypeInfo
var strInfo dataTypeInfo var strInfo dataTypeInfo
var dirInfo dataTypeInfo var dirInfo dataTypeInfo
var hexInfo dataTypeInfo
var boolInfo dataTypeInfo var boolInfo dataTypeInfo
intInfo.name = "int" intInfo.name = "int"
@ -21,6 +22,7 @@ func init() {
durInfo.name = "duration" durInfo.name = "duration"
dirInfo.name = "directory" dirInfo.name = "directory"
strInfo.name = "string" strInfo.name = "string"
hexInfo.name = "hex"
boolInfo.name = "bool" boolInfo.name = "bool"
intInfo.parser = parseInt intInfo.parser = parseInt
@ -28,6 +30,7 @@ func init() {
durInfo.parser = parseDuration durInfo.parser = parseDuration
dirInfo.parser = parseDirectory dirInfo.parser = parseDirectory
strInfo.parser = parseString strInfo.parser = parseString
hexInfo.parser = parseHex
boolInfo.parser = parseBool boolInfo.parser = parseBool
tInfo[TypeInt] = intInfo tInfo[TypeInt] = intInfo
@ -35,5 +38,6 @@ func init() {
tInfo[TypeDuration] = durInfo tInfo[TypeDuration] = durInfo
tInfo[TypeString] = strInfo tInfo[TypeString] = strInfo
tInfo[TypeDirectory] = dirInfo tInfo[TypeDirectory] = dirInfo
tInfo[TypeHex] = hexInfo
tInfo[TypeBool] = boolInfo tInfo[TypeBool] = boolInfo
} }