Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
02d36e3d69
|
|||
9733bb42a8
|
|||
3d2005a8a2
|
|||
1803501272
|
27
datatype.go
27
datatype.go
@ -1,10 +1,11 @@
|
||||
package envconf
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
type DataType int
|
||||
type DataType uint
|
||||
|
||||
const (
|
||||
TypeNone DataType = iota
|
||||
@ -17,6 +18,18 @@ const (
|
||||
TypeHex DataType = iota
|
||||
)
|
||||
|
||||
func FixedHex(size uint) DataType {
|
||||
return (DataType)(size<<16) | TypeHex
|
||||
}
|
||||
|
||||
func (dtype DataType) baseType() DataType {
|
||||
return dtype & 0xffff
|
||||
}
|
||||
|
||||
func (dtype DataType) typeAndSize() (DataType, uint) {
|
||||
return (dtype & 0xffff), uint(dtype >> 16)
|
||||
}
|
||||
|
||||
type cValue struct {
|
||||
dtype DataType
|
||||
intval int64
|
||||
@ -28,18 +41,24 @@ type cValue struct {
|
||||
}
|
||||
|
||||
func (dtype DataType) parse(key string, str string) (ret cValue) {
|
||||
info, ok := tInfo[dtype]
|
||||
rdtype, size := dtype.typeAndSize()
|
||||
info, ok := tInfo[rdtype]
|
||||
if ok {
|
||||
ret = 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"
|
||||
|
@ -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) && (v.parsed.err == nil) {
|
||||
if (v.dtype.baseType() == dtype.baseType()) && (v.parsed.err == nil) {
|
||||
retval[k] = v.parsed
|
||||
} else {
|
||||
return
|
||||
|
22
parsers.go
22
parsers.go
@ -11,7 +11,7 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
func parseInt(key string, str string) (ret cValue) {
|
||||
func parseInt(key string, str string, _ uint) (ret cValue) {
|
||||
val, err := strconv.ParseInt(str, 10, 64)
|
||||
if err == nil {
|
||||
ret.intval = val
|
||||
@ -21,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, _ uint) (ret cValue) {
|
||||
mod := int64(1)
|
||||
str = strings.ToUpper(str)
|
||||
if strings.HasSuffix(str, "K") {
|
||||
@ -46,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, _ uint) (ret cValue) {
|
||||
val, err := time.ParseDuration(str)
|
||||
if err == nil {
|
||||
ret.durval = val
|
||||
@ -56,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, _ uint) (ret cValue) {
|
||||
val, err := strconv.ParseBool(str)
|
||||
if err == nil {
|
||||
ret.boolval = val
|
||||
@ -66,17 +66,21 @@ func parseBool(key string, str string) (ret cValue) {
|
||||
return
|
||||
}
|
||||
|
||||
func parseHex(key string, str string) (ret cValue) {
|
||||
func parseHex(key string, str string, size uint) (ret cValue) {
|
||||
val, err := hex.DecodeString(str)
|
||||
if err == nil {
|
||||
if err == nil && (size == 0 || size == uint(len(val))) {
|
||||
ret.binval = val
|
||||
} else {
|
||||
ret.err = errors.New(fmt.Sprintf(`Environment variable "%s" is not of type hex.`, key))
|
||||
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) (ret cValue) {
|
||||
func parseDirectory(_ string, str string, _ uint) (ret cValue) {
|
||||
wd, err := os.Getwd()
|
||||
if err == nil {
|
||||
if path.IsAbs(str) {
|
||||
@ -90,7 +94,7 @@ func parseDirectory(_ string, str string) (ret cValue) {
|
||||
return
|
||||
}
|
||||
|
||||
func parseString(_ string, str string) (ret cValue) {
|
||||
func parseString(_ string, str string, _ uint) (ret cValue) {
|
||||
ret.strval = str
|
||||
return
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
package envconf
|
||||
|
||||
type dataTypeInfo struct {
|
||||
parser func(string, string) cValue
|
||||
parser func(string, string, uint) cValue
|
||||
name string
|
||||
}
|
||||
|
||||
@ -15,6 +15,7 @@ func init() {
|
||||
var strInfo dataTypeInfo
|
||||
var dirInfo dataTypeInfo
|
||||
var hexInfo dataTypeInfo
|
||||
|
||||
var boolInfo dataTypeInfo
|
||||
|
||||
intInfo.name = "int"
|
||||
@ -30,6 +31,7 @@ func init() {
|
||||
durInfo.parser = parseDuration
|
||||
dirInfo.parser = parseDirectory
|
||||
strInfo.parser = parseString
|
||||
|
||||
hexInfo.parser = parseHex
|
||||
boolInfo.parser = parseBool
|
||||
|
||||
|
Reference in New Issue
Block a user