5 Commits

Author SHA1 Message Date
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
5 changed files with 67 additions and 33 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

@ -23,7 +23,7 @@ type cEntry struct {
type Config struct {
parsed bool
env map[string]cEntry
mapEnv map[string]map[string]cEntry
mapEnv map[string]map[string]string
}
// NewConfig returns an envconf.Config that is used to read configuration from environment variables.
@ -33,27 +33,31 @@ 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)
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 = 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 {
left := strings.Join(splitted[:count], "_")
right := strings.Join(splitted[count:], "_")
if len(config.mapEnv[left]) == 0 {
config.mapEnv[left] = make(map[string]string)
}
fmt.Printf("left=(%s), right=(%s), key=(%s)\n", left, right, key)
config.mapEnv[left][right] = key
}
config.mapEnv[left][right] = entry
}
}
}
@ -84,9 +88,11 @@ 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 {
fmt.Printf("key-from-define-map (%s)\n", key)
entry := c.env[key]
entry.dtype = dtype
c.mapEnv[key][mapKey] = entry
c.env[key] = entry
}
}
}
@ -136,13 +142,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 +233,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 {
@ -303,8 +292,9 @@ func (c *Config) getRawMap(key string, dtype DataType) (empty map[string]cValue)
if ok {
for k, v := range entries {
if (v.dtype.baseType() == dtype.baseType()) && (v.parsed.err == nil) {
retval[k] = v.parsed
entry := c.env[v]
if (entry.dtype.baseType() == dtype.baseType()) && (entry.parsed.err == nil) {
retval[k] = entry.parsed
} else {
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"

17
tests/main.go Normal file
View File

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