Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
3d2005a8a2
|
|||
1803501272
|
|||
d1bcc8c4cc
|
|||
afa50b4fca
|
|||
09acd79fe8
|
11
datatype.go
11
datatype.go
@ -14,6 +14,11 @@ const (
|
||||
TypeString DataType = iota
|
||||
TypeDirectory DataType = iota
|
||||
TypeBool DataType = iota
|
||||
TypeHex DataType = iota
|
||||
TypeHex16 DataType = iota
|
||||
TypeHex32 DataType = iota
|
||||
TypeHex64 DataType = iota
|
||||
TypeHex128 DataType = iota
|
||||
)
|
||||
|
||||
type cValue struct {
|
||||
@ -21,6 +26,7 @@ type cValue struct {
|
||||
intval int64
|
||||
durval time.Duration
|
||||
boolval bool
|
||||
binval []byte
|
||||
strval string
|
||||
err error
|
||||
}
|
||||
@ -28,7 +34,10 @@ type cValue struct {
|
||||
func (dtype DataType) parse(key string, str string) (ret cValue) {
|
||||
info, ok := tInfo[dtype]
|
||||
if ok {
|
||||
return info.parser(key, str)
|
||||
ret = info.parser(key, str)
|
||||
if len(ret.binval) == 0 {
|
||||
ret.binval = make([]byte, 0, 0)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
96
envconf.go
96
envconf.go
@ -259,7 +259,7 @@ func (c *Config) Status() (ok bool) {
|
||||
}
|
||||
|
||||
func cleanKey(key string) string {
|
||||
expr := regexp.MustCompile("(?:^_+)|(?:_+(?=_))|(?:_$)")
|
||||
expr := regexp.MustCompile("__+")
|
||||
fn := func(r rune) rune {
|
||||
if (r >= '0' && r <= '9') || (r >= 'A' && r <= 'Z') || r == '_' {
|
||||
return r
|
||||
@ -267,8 +267,8 @@ func cleanKey(key string) string {
|
||||
return -1
|
||||
}
|
||||
|
||||
key = strings.Map(fn, strings.ToUpper(key))
|
||||
return expr.ReplaceAllString(key, "")
|
||||
key = strings.Trim(strings.Map(fn, strings.ToUpper(key)), "_")
|
||||
return expr.ReplaceAllString(key, "_")
|
||||
}
|
||||
|
||||
func keySplit(key string) (left string, right string, ok bool) {
|
||||
@ -283,6 +283,7 @@ 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]
|
||||
@ -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 == dtype) && (v.parsed.err == nil) {
|
||||
retval[k] = v.parsed
|
||||
} else {
|
||||
return
|
||||
@ -314,21 +315,56 @@ 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.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 {
|
||||
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 +372,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 +380,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 +388,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 +428,46 @@ 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 (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
|
||||
|
51
parsers.go
51
parsers.go
@ -1,6 +1,7 @@
|
||||
package envconf
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
@ -65,6 +66,56 @@ func parseBool(key string, str string) (ret cValue) {
|
||||
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 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 {
|
||||
|
22
typeinfo.go
22
typeinfo.go
@ -14,6 +14,13 @@ func init() {
|
||||
var durInfo dataTypeInfo
|
||||
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"
|
||||
@ -21,6 +28,7 @@ func init() {
|
||||
durInfo.name = "duration"
|
||||
dirInfo.name = "directory"
|
||||
strInfo.name = "string"
|
||||
hexInfo.name = "hex"
|
||||
boolInfo.name = "bool"
|
||||
|
||||
intInfo.parser = parseInt
|
||||
@ -28,6 +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
|
||||
@ -35,5 +50,12 @@ func init() {
|
||||
tInfo[TypeDuration] = durInfo
|
||||
tInfo[TypeString] = strInfo
|
||||
tInfo[TypeDirectory] = dirInfo
|
||||
tInfo[TypeHex] = hexInfo
|
||||
|
||||
tInfo[TypeHex16] = hexInfo16
|
||||
tInfo[TypeHex32] = hexInfo32
|
||||
tInfo[TypeHex64] = hexInfo64
|
||||
tInfo[TypeHex128] = hexInfo128
|
||||
|
||||
tInfo[TypeBool] = boolInfo
|
||||
}
|
||||
|
Reference in New Issue
Block a user