7 Commits

Author SHA1 Message Date
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
d1bcc8c4cc add hex type 2022-01-16 18:24:24 +01:00
afa50b4fca remove invalid map 2022-01-15 21:35:37 +01:00
4 changed files with 92 additions and 26 deletions

View File

@ -1,40 +1,69 @@
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
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
durval time.Duration
boolval bool
binval []byte
strval string
err error
}
func (dtype DataType) parse(key string, str string) (ret cValue) {
info, ok := tInfo[dtype]
rdtype, size := dtype.typeAndSize()
info, ok := tInfo[rdtype]
if ok {
return 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

@ -283,10 +283,11 @@ 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
}
}
@ -302,7 +303,7 @@ func (c *Config) getRawMap(key string, dtype DataType) (empty map[string]cValue)
if ok {
for k, v := range entries {
if v.dtype == dtype {
if (v.dtype.baseType() == dtype.baseType()) && (v.parsed.err == nil) {
retval[k] = v.parsed
} else {
return
@ -314,21 +315,28 @@ func (c *Config) getRawMap(key string, dtype DataType) (empty map[string]cValue)
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.
func (c *Config) GetInt(key string) int64 {
val := c.getRaw(key, TypeInt)
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.
func (c *Config) GetMetric(key string) int64 {
val := c.getRaw(key, TypeMetric)
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
// function will return the empty string.
func (c *Config) GetDirectory(key string) string {
@ -336,7 +344,7 @@ func (c *Config) GetDirectory(key string) string {
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
// function will return the empty string.
func (c *Config) GetString(key string) string {
@ -344,7 +352,7 @@ func (c *Config) GetString(key string) string {
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
// function will return time.Duration(0).
func (c *Config) GetDuration(key string) time.Duration {
@ -352,7 +360,7 @@ func (c *Config) GetDuration(key string) time.Duration {
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
// function will return false.
func (c *Config) GetBool(key string) bool {
@ -392,6 +400,14 @@ func (c *Config) GetMapBool(key string) (retval map[string]bool) {
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

View File

@ -1,6 +1,7 @@
package envconf
import (
"encoding/hex"
"errors"
"fmt"
"os"
@ -10,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
@ -20,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") {
@ -45,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
@ -55,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
@ -65,7 +66,21 @@ func parseBool(key string, str string) (ret cValue) {
return
}
func parseDirectory(_ string, str string) (ret cValue) {
func parseHex(key string, str string, size int) (ret cValue) {
val, err := hex.DecodeString(str)
if err == nil && (size == 0 || size == len(val)) {
ret.binval = val
} else {
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, _ int) (ret cValue) {
wd, err := os.Getwd()
if err == nil {
if path.IsAbs(str) {
@ -79,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
}

View File

@ -1,7 +1,7 @@
package envconf
type dataTypeInfo struct {
parser func(string, string) cValue
parser func(string, string, int) cValue
name string
}
@ -14,6 +14,8 @@ func init() {
var durInfo dataTypeInfo
var strInfo dataTypeInfo
var dirInfo dataTypeInfo
var hexInfo dataTypeInfo
var boolInfo dataTypeInfo
intInfo.name = "int"
@ -21,6 +23,7 @@ func init() {
durInfo.name = "duration"
dirInfo.name = "directory"
strInfo.name = "string"
hexInfo.name = "hex"
boolInfo.name = "bool"
intInfo.parser = parseInt
@ -28,6 +31,8 @@ func init() {
durInfo.parser = parseDuration
dirInfo.parser = parseDirectory
strInfo.parser = parseString
hexInfo.parser = parseHex
boolInfo.parser = parseBool
tInfo[TypeInt] = intInfo
@ -35,5 +40,6 @@ func init() {
tInfo[TypeDuration] = durInfo
tInfo[TypeString] = strInfo
tInfo[TypeDirectory] = dirInfo
tInfo[TypeHex] = hexInfo
tInfo[TypeBool] = boolInfo
}