7 Commits

Author SHA1 Message Date
73278439b0 fix hooks 2022-01-29 13:31:31 +01:00
0a589a2565 add test 2022-01-29 13:29:09 +01:00
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
8 changed files with 98 additions and 25 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
tests/buildenv/*
tests/bin/*

View File

@ -1,3 +1,6 @@
#!/usr/bin/env bash #!/usr/bin/env bash
go fmt *.go go fmt *.go
git add *.go go fmt tests/*.go
git add *.go tests/*.go
git add tests/*.go

View File

@ -1,22 +1,40 @@
package envconf package envconf
import ( import (
"fmt"
"time" "time"
) )
type DataType int type DataType uint32
const ( const (
TypeNone DataType = iota sizeBitmask DataType = 0xffff
TypeInt DataType = iota typeBitmask DataType = sizeBitmask << 16
TypeMetric DataType = iota
TypeDuration DataType = iota
TypeString DataType = iota
TypeDirectory DataType = iota
TypeBool DataType = iota
TypeHex DataType = iota
) )
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 { type cValue struct {
dtype DataType dtype DataType
intval int64 intval int64
@ -28,18 +46,24 @@ 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] rdtype, size := dtype.typeAndSize()
info, ok := tInfo[rdtype]
if ok { if ok {
ret = info.parser(key, str) ret = info.parser(key, str, size)
if len(ret.binval) == 0 { if len(ret.binval) == 0 {
ret.binval = make([]byte, 0, 0) ret.binval = make([]byte, 0, 0)
} }
} }
return return
} }
func (dtype DataType) String() string { func (dtype DataType) String() string {
info, ok := tInfo[dtype] rdtype, size := dtype.typeAndSize()
info, ok := tInfo[rdtype]
if ok { if ok {
if size > 0 {
return fmt.Sprintf("%s%d", info.name, size)
}
return info.name return info.name
} }
return "invalid" 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) { func (c *Config) getRaw(key string, dtype DataType) (val cValue) {
val.dtype = TypeNone val.dtype = TypeNone
val.binval = make([]byte, 0, 0)
if c.parsed { if c.parsed {
key = cleanKey(key) key = cleanKey(key)
entry, ok := c.env[key] entry, ok := c.env[key]
if ok && (entry.dtype == dtype) { if ok && (entry.dtype.baseType() == dtype.baseType()) {
return entry.parsed return entry.parsed
} }
} }
@ -302,7 +303,7 @@ func (c *Config) getRawMap(key string, dtype DataType) (empty map[string]cValue)
if ok { if ok {
for k, v := range entries { 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 retval[k] = v.parsed
} else { } else {
return return

View File

@ -11,7 +11,7 @@ import (
"time" "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) val, err := strconv.ParseInt(str, 10, 64)
if err == nil { if err == nil {
ret.intval = val ret.intval = val
@ -21,7 +21,7 @@ func parseInt(key string, str string) (ret cValue) {
return return
} }
func parseMetric(key string, str string) (ret cValue) { func parseMetric(key string, str string, _ int) (ret cValue) {
mod := int64(1) mod := int64(1)
str = strings.ToUpper(str) str = strings.ToUpper(str)
if strings.HasSuffix(str, "K") { if strings.HasSuffix(str, "K") {
@ -46,7 +46,7 @@ func parseMetric(key string, str string) (ret cValue) {
return return
} }
func parseDuration(key string, str string) (ret cValue) { func parseDuration(key string, str string, _ int) (ret cValue) {
val, err := time.ParseDuration(str) val, err := time.ParseDuration(str)
if err == nil { if err == nil {
ret.durval = val ret.durval = val
@ -56,7 +56,7 @@ func parseDuration(key string, str string) (ret cValue) {
return return
} }
func parseBool(key string, str string) (ret cValue) { func parseBool(key string, str string, _ int) (ret cValue) {
val, err := strconv.ParseBool(str) val, err := strconv.ParseBool(str)
if err == nil { if err == nil {
ret.boolval = val ret.boolval = val
@ -66,17 +66,21 @@ func parseBool(key string, str string) (ret cValue) {
return return
} }
func parseHex(key string, str string) (ret cValue) { func parseHex(key string, str string, size int) (ret cValue) {
val, err := hex.DecodeString(str) val, err := hex.DecodeString(str)
if err == nil { if err == nil && (size == 0 || size == len(val)) {
ret.binval = val ret.binval = val
} else { } 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 return
} }
func parseDirectory(_ string, str string) (ret cValue) { func parseDirectory(_ string, str string, _ int) (ret cValue) {
wd, err := os.Getwd() wd, err := os.Getwd()
if err == nil { if err == nil {
if path.IsAbs(str) { if path.IsAbs(str) {
@ -90,7 +94,7 @@ func parseDirectory(_ string, str string) (ret cValue) {
return return
} }
func parseString(_ string, str string) (ret cValue) { func parseString(_ string, str string, _ int) (ret cValue) {
ret.strval = str ret.strval = str
return return
} }

22
tests/build.sh Executable file
View File

@ -0,0 +1,22 @@
#!/usr/bin/env bash
dir=$(dirname $(realpath $0))
export GO111MODULE=on
export GOPROXY=direct
export GOPATH=${dir}/buildenv/deps
mkdir -p ${dir}/buildenv
chmod -R 755 ${dir}/buildenv
rm -rf ${dir}/buildenv
mkdir -p "${dir}/buildenv/src"
mkdir -p ${GOPATH}/src
mkdir -p ${dir}/bin
cp -r "${dir}/main.go" "${dir}/buildenv/src/main.go"
cd ${dir}/buildenv
go mod init src
go mod tidy
go build -o "${dir}/bin/test" "src/main.go"

15
tests/main.go Normal file
View File

@ -0,0 +1,15 @@
package main
import (
"fmt"
"git.purser.it/roypur/envconf"
)
func main() {
conf := envconf.NewConfig()
conf.Define("this_is_a_map", envconf.FixedHex(22))
conf.Parse()
conf.Status()
fmt.Println(conf.GetHex("this_is_a_map"))
}

View File

@ -1,7 +1,7 @@
package envconf package envconf
type dataTypeInfo struct { type dataTypeInfo struct {
parser func(string, string) cValue parser func(string, string, int) cValue
name string name string
} }
@ -15,6 +15,7 @@ func init() {
var strInfo dataTypeInfo var strInfo dataTypeInfo
var dirInfo dataTypeInfo var dirInfo dataTypeInfo
var hexInfo dataTypeInfo var hexInfo dataTypeInfo
var boolInfo dataTypeInfo var boolInfo dataTypeInfo
intInfo.name = "int" intInfo.name = "int"
@ -30,6 +31,7 @@ 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
boolInfo.parser = parseBool boolInfo.parser = parseBool