This commit is contained in:
Roy Olav Purser 2021-03-23 14:37:00 +01:00
parent cb18e15022
commit 67ba140c7e
No known key found for this signature in database
GPG Key ID: 0BA77797F072BC52
2 changed files with 113 additions and 36 deletions

View File

@ -1,4 +1,9 @@
package envconf package envconf
import ("fmt"
"errors"
"time"
"strconv")
type DataType int type DataType int
const ( const (
TypeNone DataType = iota TypeNone DataType = iota
@ -21,3 +26,59 @@ func (dtype DataType) String()(string) {
} }
return "invalid" return "invalid"
} }
type cValue struct {
intval int64
durval time.Duration
boolval bool
strval string
err error
}
func (dtype DataType) parse(key string, str string)(ret cValue) {
parsers := make(map[DataType](func(string,string)(cValue)))
parsers[TypeInt] = parseInt
parsers[TypeDuration] = parseDuration
parsers[TypeString] = parseString
parsers[TypeBool] = parseBool
parser, ok := parsers[dtype]
if ok {
return parser(key, str)
}
return
}
func parseInt(key string, str string)(ret cValue) {
val, err := strconv.ParseInt(str, 10, 64)
if err == nil {
ret.intval = val
} else {
ret.err = errors.New(fmt.Sprintf(`Environment variable "%s" is not of type int.`, key))
}
return
}
func parseDuration(key string, str string)(ret cValue) {
val, err := time.ParseDuration(str)
if err == nil {
ret.durval = val
} else {
ret.err = errors.New(fmt.Sprintf(`Environment variable "%s" is not of type duration.`, key))
}
return
}
func parseBool(key string, str string)(ret cValue) {
val, err := strconv.ParseBool(str)
if err == nil {
ret.boolval = val
} else {
ret.err = errors.New(fmt.Sprintf(`Environment variable "%s" is not of type bool.`, key))
}
return
}
func parseString(_ string, str string)(ret cValue) {
ret.strval = str
return
}

76
main.go
View File

@ -2,9 +2,8 @@ package envconf
import ("strings" import ("strings"
"unicode" "unicode"
"strconv"
"time"
"errors" "errors"
"time"
"fmt" "fmt"
"os") "os")
@ -15,14 +14,6 @@ type cEntry struct {
unset bool unset bool
empty bool empty bool
} }
type cValue struct {
intval int64
durval time.Duration
strval string
err error
}
type Config struct { type Config struct {
parsed bool parsed bool
env map[string]cEntry env map[string]cEntry
@ -81,41 +72,68 @@ func (c *Config) DefineDefault(key string, val string, dtype DataType) {
func (c *Config) Parse() { func (c *Config) Parse() {
c.parsed = true c.parsed = true
parsers := make(map[DataType](func(string)(cValue)))
parsers[TypeInt] = parseInt
parsers[TypeDuration] = parseDuration
for k,v := range c.env { for k,v := range c.env {
parser, ok := parsers[v.dtype]
if ok {
if v.empty { if v.empty {
if v.unset { if v.unset {
v.parsed.err = errors.New(fmt.Sprintf("Environment variable %s not found. It should have been of type %s", k, v.dtype)) v.parsed.err = errors.New(fmt.Sprintf(`Environment variable "%s" not found. It should have been of type %s.`, k, v.dtype))
c.env[k] = v c.env[k] = v
} }
} else { } else {
v.parsed = parser(v.value) v.parsed = v.dtype.parse(k, v.value)
c.env[k] = v c.env[k] = v
} }
} }
} }
}
func (c *Config) Show() { func (c *Config) Status()(ok bool) {
ok = c.parsed
if ok {
for _,v := range c.env { for _,v := range c.env {
fmt.Println(v.parsed.err) err := v.parsed.err
if err != nil {
ok = false
fmt.Println(err)
}
} }
} }
func parseInt(str string)(ret cValue) {
ret.intval, ret.err = strconv.ParseInt(str, 10, 64)
return return
} }
func parseDuration(str string)(ret cValue) { func (c *Config) GetInt(key string)(int64) {
ret.durval, ret.err = time.ParseDuration(str) if c.parsed {
return entry, ok := c.env[key]
if ok {
return entry.parsed.intval
}
}
return 0
}
func (c *Config) GetString(key string)(string) {
if c.parsed {
entry, ok := c.env[key]
if ok {
return entry.parsed.strval
}
}
return ""
}
func (c *Config) GetDuration(key string)(time.Duration) {
if c.parsed {
entry, ok := c.env[key]
if ok {
return entry.parsed.durval
}
}
return time.Duration(0)
}
func (c *Config) GetBool(key string)(bool) {
if c.parsed {
entry, ok := c.env[key]
if ok {
return entry.parsed.boolval
}
}
return false
} }
func getFirstRune(str string)(rune) { func getFirstRune(str string)(rune) {
@ -124,5 +142,3 @@ func getFirstRune(str string)(rune) {
} }
return rune(0) return rune(0)
} }