23 Commits

Author SHA1 Message Date
6fea990cb9 remove empty map 2022-03-27 15:03:39 +02:00
19fdcd7e05 remove empty map 2022-03-27 14:53:46 +02:00
7a72947737 test 2022-03-27 14:42:43 +02:00
3c23ee5473 change map offset 2022-03-27 14:32:28 +02:00
4520530b7f add constructors 2022-03-27 14:14:43 +02:00
113ff46fee multi map test 2022-03-27 13:37:04 +02:00
5262d7abb7 remove debug prints 2022-01-29 22:03:33 +01:00
2efc1a155e print mapEnv 2022-01-29 22:00:54 +01:00
45f8b0f918 add test print 2022-01-29 21:59:03 +01:00
2cdcbec364 add test print 2022-01-29 21:51:28 +01:00
f815bff582 test map expand 2022-01-29 21:37:06 +01:00
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
d1bcc8c4cc add hex type 2022-01-16 18:24:24 +01:00
afa50b4fca remove invalid map 2022-01-15 21:35:37 +01:00
09acd79fe8 remove regex lookaround 2022-01-15 19:57:38 +01:00
0f1586393b fix regex 2022-01-15 19:54:59 +01:00
0d49b3e716 fix regex 2022-01-15 19:39:51 +01:00
8 changed files with 367 additions and 62 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
go fmt *.go
git add *.go
go fmt tests/*.go
git add *.go tests/*.go
git add tests/*.go

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

@ -21,9 +21,11 @@ type cEntry struct {
}
type Config struct {
parsed bool
env map[string]cEntry
mapEnv map[string]map[string]cEntry
parsed bool
env map[string]cEntry
mapEnv map[string]map[string]string
mapMapEnv map[string]map[string]map[string]string
mapMapMapEnv map[string]map[string]map[string]map[string]string
}
// NewConfig returns an envconf.Config that is used to read configuration from environment variables.
@ -33,27 +35,58 @@ func NewConfig() *Config {
config := new(Config)
config.parsed = false
config.env = make(map[string]cEntry)
config.mapEnv = make(map[string]map[string]cEntry)
config.mapEnv = make(map[string]map[string]string)
config.mapMapEnv = make(map[string]map[string]map[string]string)
config.mapMapMapEnv = make(map[string]map[string]map[string]map[string]string)
for _, v := range os.Environ() {
splitted := strings.SplitN(v, "=", 2)
if len(splitted) == 2 {
key := cleanKey(splitted[0])
left, right, mappable := keySplit(key)
val := splitted[1]
splitted = append(strings.Split(key, "_"), "", "", "")
if unicode.IsLetter(getFirstRune(key)) {
var entry cEntry
entry.value = splitted[1]
entry.value = val
entry.dtype = TypeNone
entry.unset = false
entry.empty = false
config.env[key] = entry
if mappable {
if len(config.mapEnv[left]) == 0 {
config.mapEnv[left] = make(map[string]cEntry)
}
if len(splitted) > 1 {
for count, _ := range splitted {
if count < len(splitted)-3 {
left := strings.Trim(strings.Join(splitted[:count], "_"), "_")
right := strings.Trim(strings.Join(splitted[count:], "_"), "_")
if len(config.mapEnv[left]) == 0 {
config.mapEnv[left] = make(map[string]string)
config.mapMapEnv[left] = make(map[string]map[string]string)
config.mapMapMapEnv[left] = make(map[string]map[string]map[string]string)
}
if left != "" && right != "" {
config.mapEnv[left][right] = key
}
config.mapEnv[left][right] = entry
middle := splitted[count]
right = strings.Trim(strings.Join(splitted[count+1:], "_"), "_")
if len(config.mapMapEnv[left][middle]) == 0 {
config.mapMapEnv[left][middle] = make(map[string]string)
config.mapMapMapEnv[left][middle] = make(map[string]map[string]string)
}
if left != "" && middle != "" && right != "" {
config.mapMapEnv[left][middle][right] = key
}
lmiddle := splitted[count]
rmiddle := splitted[count+1]
right = strings.Trim(strings.Join(splitted[count+2:], "_"), "_")
if len(config.mapMapMapEnv[left][lmiddle][rmiddle]) == 0 {
config.mapMapMapEnv[left][lmiddle][rmiddle] = make(map[string]string)
}
if left != "" && lmiddle != "" && rmiddle != "" && right != "" {
config.mapMapMapEnv[left][lmiddle][rmiddle][right] = key
}
}
}
}
}
}
@ -84,9 +117,10 @@ func (c *Config) DefineMap(key string, dtype DataType) {
key = cleanKey(key)
entries, ok := c.mapEnv[key]
if ok {
for mapKey, entry := range entries {
for _, key = range entries {
entry := c.env[key]
entry.dtype = dtype
c.mapEnv[key][mapKey] = entry
c.env[key] = entry
}
}
}
@ -136,13 +170,6 @@ func (c *Config) Parse() {
}
}
for k, v := range c.mapEnv {
for mk, mv := range v {
mv.parsed = mv.dtype.parse(k+"_"+mk, mv.value)
c.mapEnv[k][mk] = mv
}
}
if failed {
for k, v := range c.env {
if (v.parsed.err == nil) && v.unset {
@ -234,17 +261,7 @@ func (c *Config) Status() (ok bool) {
}
}
}
for _, v := range c.mapEnv {
for _, mv := range v {
err := mv.parsed.err
if err != nil {
ok = false
if !mv.empty {
fmt.Fprintln(os.Stderr, err)
}
}
}
}
if !ok {
fmt.Fprintln(os.Stderr, "")
for _, v := range c.env {
@ -259,7 +276,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,7 +284,7 @@ func cleanKey(key string) string {
return -1
}
key = strings.Map(fn, strings.ToUpper(key))
key = strings.Trim(strings.Map(fn, strings.ToUpper(key)), "_")
return expr.ReplaceAllString(key, "_")
}
@ -283,10 +300,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,8 +320,9 @@ func (c *Config) getRawMap(key string, dtype DataType) (empty map[string]cValue)
if ok {
for k, v := range entries {
if v.dtype == dtype {
retval[k] = v.parsed
entry := c.env[v]
if (entry.dtype.baseType() == dtype.baseType()) && (entry.parsed.err == nil) {
retval[k] = entry.parsed
} else {
return
}
@ -314,21 +333,81 @@ func (c *Config) getRawMap(key string, dtype DataType) (empty map[string]cValue)
return
}
// GetInt returns the value of an environment variable.
func (c *Config) getRawMapMap(key string, dtype DataType) (empty map[string]map[string]cValue) {
empty = make(map[string]map[string]cValue)
retval := make(map[string]map[string]cValue)
if c.parsed {
key = cleanKey(key)
entries, ok := c.mapMapEnv[key]
if ok {
for k1, v1 := range entries {
retval[k1] = make(map[string]cValue)
for k2, v2 := range v1 {
entry := c.env[v2]
if (entry.dtype.baseType() == dtype.baseType()) && (entry.parsed.err == nil) {
retval[k1][k2] = entry.parsed
} else {
return
}
}
}
return retval
}
}
return
}
func (c *Config) getRawMapMapMap(key string, dtype DataType) (empty map[string]map[string]map[string]cValue) {
empty = make(map[string]map[string]map[string]cValue)
retval := make(map[string]map[string]map[string]cValue)
if c.parsed {
key = cleanKey(key)
entries, ok := c.mapMapMapEnv[key]
if ok {
for k1, v1 := range entries {
retval[k1] = make(map[string]map[string]cValue)
for k2, v2 := range v1 {
retval[k1][k2] = make(map[string]cValue)
for k3, v3 := range v2 {
entry := c.env[v3]
if (entry.dtype.baseType() == dtype.baseType()) && (entry.parsed.err == nil) {
retval[k1][k2][k3] = entry.parsed
} else {
return
}
}
}
}
return retval
}
}
return
}
// 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 +415,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 +423,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 +431,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 +471,139 @@ 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) GetMapMapInt(key string) (retval map[string]map[string]int64) {
retval = make(map[string]map[string]int64)
for k1, v1 := range c.getRawMapMap(key, TypeInt) {
retval[k1] = make(map[string]int64)
for k2, v2 := range v1 {
retval[k1][k2] = v2.intval
}
}
return
}
func (c *Config) GetMapMapDuration(key string) (retval map[string]map[string]time.Duration) {
retval = make(map[string]map[string]time.Duration)
for k1, v1 := range c.getRawMapMap(key, TypeDuration) {
retval[k1] = make(map[string]time.Duration)
for k2, v2 := range v1 {
retval[k1][k2] = v2.durval
}
}
return
}
func (c *Config) GetMapMapString(key string) (retval map[string]map[string]string) {
retval = make(map[string]map[string]string)
for k1, v1 := range c.getRawMapMap(key, TypeString) {
retval[k1] = make(map[string]string)
for k2, v2 := range v1 {
retval[k1][k2] = v2.strval
}
}
return
}
func (c *Config) GetMapMapBool(key string) (retval map[string]map[string]bool) {
retval = make(map[string]map[string]bool)
for k1, v1 := range c.getRawMapMap(key, TypeBool) {
retval[k1] = make(map[string]bool)
for k2, v2 := range v1 {
retval[k1][k2] = v2.boolval
}
}
return
}
func (c *Config) GetMapMapHex(key string) (retval map[string]map[string][]byte) {
retval = make(map[string]map[string][]byte)
for k1, v1 := range c.getRawMapMap(key, TypeHex) {
retval[k1] = make(map[string][]byte)
for k2, v2 := range v1 {
retval[k1][k2] = v2.binval
}
}
return
}
func (c *Config) GetMapMapMapInt(key string) (retval map[string]map[string]map[string]int64) {
retval = make(map[string]map[string]map[string]int64)
for k1, v1 := range c.getRawMapMapMap(key, TypeInt) {
retval[k1] = make(map[string]map[string]int64)
for k2, v2 := range v1 {
retval[k1][k2] = make(map[string]int64)
for k3, v3 := range v2 {
retval[k1][k2][k3] = v3.intval
}
}
}
return
}
func (c *Config) GetMapMapMapDuration(key string) (retval map[string]map[string]map[string]time.Duration) {
retval = make(map[string]map[string]map[string]time.Duration)
for k1, v1 := range c.getRawMapMapMap(key, TypeDuration) {
retval[k1] = make(map[string]map[string]time.Duration)
for k2, v2 := range v1 {
retval[k1][k2] = make(map[string]time.Duration)
for k3, v3 := range v2 {
retval[k1][k2][k3] = v3.durval
}
}
}
return
}
func (c *Config) GetMapMapMapString(key string) (retval map[string]map[string]map[string]string) {
retval = make(map[string]map[string]map[string]string)
for k1, v1 := range c.getRawMapMapMap(key, TypeString) {
retval[k1] = make(map[string]map[string]string)
for k2, v2 := range v1 {
retval[k1][k2] = make(map[string]string)
for k3, v3 := range v2 {
retval[k1][k2][k3] = v3.strval
}
}
}
return
}
func (c *Config) GetMapMapMapBool(key string) (retval map[string]map[string]map[string]bool) {
retval = make(map[string]map[string]map[string]bool)
for k1, v1 := range c.getRawMapMapMap(key, TypeBool) {
retval[k1] = make(map[string]map[string]bool)
for k2, v2 := range v1 {
retval[k1][k2] = make(map[string]bool)
for k3, v3 := range v2 {
retval[k1][k2][k3] = v3.boolval
}
}
}
return
}
func (c *Config) GetMapMapMapHex(key string) (retval map[string]map[string]map[string][]byte) {
retval = make(map[string]map[string]map[string][]byte)
for k1, v1 := range c.getRawMapMapMap(key, TypeHex) {
retval[k1] = make(map[string]map[string][]byte)
for k2, v2 := range v1 {
retval[k1][k2] = make(map[string][]byte)
for k3, v3 := range v2 {
retval[k1][k2][k3] = v3.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
}

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"

16
tests/main.go Normal file
View File

@ -0,0 +1,16 @@
package main
import (
"fmt"
"git.purser.it/roypur/envconf"
)
func main() {
conf := envconf.NewConfig()
conf.Define("this_is_a_map", envconf.TypeInt)
conf.DefineMap("test_map", envconf.TypeInt)
conf.Parse()
conf.Status()
fmt.Println(conf.GetMapMapInt("test_map"))
fmt.Println(conf.GetInt("this_is_a_map"))
}

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
}