add sized hex

This commit is contained in:
Roy Olav Purser 2022-01-16 21:48:29 +01:00
parent 1803501272
commit 3d2005a8a2
Signed by: roypur
GPG Key ID: E14D26A036F21656
4 changed files with 122 additions and 0 deletions

View File

@ -15,6 +15,10 @@ const (
TypeDirectory DataType = iota TypeDirectory DataType = iota
TypeBool DataType = iota TypeBool DataType = iota
TypeHex DataType = iota TypeHex DataType = iota
TypeHex16 DataType = iota
TypeHex32 DataType = iota
TypeHex64 DataType = iota
TypeHex128 DataType = iota
) )
type cValue struct { type cValue struct {

View File

@ -329,6 +329,34 @@ 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 {
@ -408,6 +436,38 @@ func (c *Config) GetMapHex(key string) (retval map[string][]byte) {
return 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 { func getFirstRune(str string) rune {
for _, v := range str { for _, v := range str {
return v return v

View File

@ -76,6 +76,46 @@ func parseHex(key string, str string) (ret cValue) {
return 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) { func parseDirectory(_ string, str string) (ret cValue) {
wd, err := os.Getwd() wd, err := os.Getwd()
if err == nil { if err == nil {

View File

@ -15,6 +15,12 @@ func init() {
var strInfo dataTypeInfo var strInfo dataTypeInfo
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"
@ -30,7 +36,13 @@ func init() {
durInfo.parser = parseDuration durInfo.parser = parseDuration
dirInfo.parser = parseDirectory dirInfo.parser = parseDirectory
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
@ -39,5 +51,11 @@ 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
} }