diff --git a/datatype.go b/datatype.go index 6163de6..2b5373e 100644 --- a/datatype.go +++ b/datatype.go @@ -13,6 +13,7 @@ const ( ) type cValue struct { + dtype DataType intval int64 durval time.Duration boolval bool diff --git a/envconf.go b/envconf.go index bb85f20..d468015 100644 --- a/envconf.go +++ b/envconf.go @@ -104,65 +104,41 @@ func (c *Config) Status()(ok bool) { return } -func (c *Config) GetInt(key string)(int64) { +func (c *Config) getRaw(key string, dtype DataType)(val cValue) { + val.dtype = TypeNone if c.parsed { upper := strings.ToUpper(key) entry, ok := c.env[upper] - if ok { - return entry.parsed.intval + if ok && (entry.dtype == dtype) { + return entry.parsed } } - return 0 + return +} + +func (c *Config) GetInt(key string)(int64) { + val := c.getRaw(key, TypeInt) + return val.intval } func (c *Config) GetMetric(key string)(int64) { - if c.parsed { - upper := strings.ToUpper(key) - entry, ok := c.env[upper] - if ok { - return entry.parsed.intval - } - } - return 0 + val := c.getRaw(key, TypeMetric) + return val.intval } func (c *Config) GetDirectory(key string)(string) { - if c.parsed { - upper := strings.ToUpper(key) - entry, ok := c.env[upper] - if ok { - return entry.parsed.strval - } - } - return "" + val := c.getRaw(key, TypeDirectory) + return val.strval } func (c *Config) GetString(key string)(string) { - if c.parsed { - upper := strings.ToUpper(key) - entry, ok := c.env[upper] - if ok { - return entry.parsed.strval - } - } - return "" + val := c.getRaw(key, TypeString) + return val.strval } func (c *Config) GetDuration(key string)(time.Duration) { - if c.parsed { - upper := strings.ToUpper(key) - entry, ok := c.env[upper] - if ok { - return entry.parsed.durval - } - } - return time.Duration(0) + val := c.getRaw(key, TypeDuration) + return val.durval } func (c *Config) GetBool(key string)(bool) { - if c.parsed { - upper := strings.ToUpper(key) - entry, ok := c.env[upper] - if ok { - return entry.parsed.boolval - } - } - return false + val := c.getRaw(key, TypeBool) + return val.boolval } func getFirstRune(str string)(rune) {