From 67ba140c7e5b67ee4d47eaab25a9c71e7e100888 Mon Sep 17 00:00:00 2001 From: Roy Olav Purser Date: Tue, 23 Mar 2021 14:37:00 +0100 Subject: [PATCH] test --- datatype.go | 61 +++++++++++++++++++++++++++++++++++++ main.go | 88 +++++++++++++++++++++++++++++++---------------------- 2 files changed, 113 insertions(+), 36 deletions(-) diff --git a/datatype.go b/datatype.go index bd53649..f45db95 100644 --- a/datatype.go +++ b/datatype.go @@ -1,4 +1,9 @@ package envconf +import ("fmt" + "errors" + "time" + "strconv") + type DataType int const ( TypeNone DataType = iota @@ -21,3 +26,59 @@ func (dtype DataType) String()(string) { } 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 +} diff --git a/main.go b/main.go index 38e2b03..fc9605d 100644 --- a/main.go +++ b/main.go @@ -2,9 +2,8 @@ package envconf import ("strings" "unicode" - "strconv" - "time" "errors" + "time" "fmt" "os") @@ -15,14 +14,6 @@ type cEntry struct { unset bool empty bool } - -type cValue struct { - intval int64 - durval time.Duration - strval string - err error -} - type Config struct { parsed bool env map[string]cEntry @@ -81,41 +72,68 @@ func (c *Config) DefineDefault(key string, val string, dtype DataType) { func (c *Config) Parse() { c.parsed = true - parsers := make(map[DataType](func(string)(cValue))) - parsers[TypeInt] = parseInt - parsers[TypeDuration] = parseDuration - for k,v := range c.env { - parser, ok := parsers[v.dtype] - if ok { - if v.empty { - 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)) - c.env[k] = v - } - } else { - v.parsed = parser(v.value) + if v.empty { + 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)) c.env[k] = v } + } else { + v.parsed = v.dtype.parse(k, v.value) + c.env[k] = v } } } -func (c *Config) Show() { - for _,v := range c.env { - fmt.Println(v.parsed.err) +func (c *Config) Status()(ok bool) { + ok = c.parsed + if ok { + for _,v := range c.env { + 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 } -func parseDuration(str string)(ret cValue) { - ret.durval, ret.err = time.ParseDuration(str) - return +func (c *Config) GetInt(key string)(int64) { + if c.parsed { + 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) { @@ -124,5 +142,3 @@ func getFirstRune(str string)(rune) { } return rune(0) } - -