verify data type for get

This commit is contained in:
Roy Olav Purser 2021-03-25 14:18:26 +01:00
parent 8d48099517
commit 4cd02d04a3
No known key found for this signature in database
GPG Key ID: 0BA77797F072BC52
2 changed files with 21 additions and 44 deletions

View File

@ -13,6 +13,7 @@ const (
) )
type cValue struct { type cValue struct {
dtype DataType
intval int64 intval int64
durval time.Duration durval time.Duration
boolval bool boolval bool

View File

@ -104,65 +104,41 @@ func (c *Config) Status()(ok bool) {
return return
} }
func (c *Config) GetInt(key string)(int64) { func (c *Config) getRaw(key string, dtype DataType)(val cValue) {
val.dtype = TypeNone
if c.parsed { if c.parsed {
upper := strings.ToUpper(key) upper := strings.ToUpper(key)
entry, ok := c.env[upper] entry, ok := c.env[upper]
if ok { if ok && (entry.dtype == dtype) {
return entry.parsed.intval 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) { func (c *Config) GetMetric(key string)(int64) {
if c.parsed { val := c.getRaw(key, TypeMetric)
upper := strings.ToUpper(key) return val.intval
entry, ok := c.env[upper]
if ok {
return entry.parsed.intval
}
}
return 0
} }
func (c *Config) GetDirectory(key string)(string) { func (c *Config) GetDirectory(key string)(string) {
if c.parsed { val := c.getRaw(key, TypeDirectory)
upper := strings.ToUpper(key) return val.strval
entry, ok := c.env[upper]
if ok {
return entry.parsed.strval
}
}
return ""
} }
func (c *Config) GetString(key string)(string) { func (c *Config) GetString(key string)(string) {
if c.parsed { val := c.getRaw(key, TypeString)
upper := strings.ToUpper(key) return val.strval
entry, ok := c.env[upper]
if ok {
return entry.parsed.strval
}
}
return ""
} }
func (c *Config) GetDuration(key string)(time.Duration) { func (c *Config) GetDuration(key string)(time.Duration) {
if c.parsed { val := c.getRaw(key, TypeDuration)
upper := strings.ToUpper(key) return val.durval
entry, ok := c.env[upper]
if ok {
return entry.parsed.durval
}
}
return time.Duration(0)
} }
func (c *Config) GetBool(key string)(bool) { func (c *Config) GetBool(key string)(bool) {
if c.parsed { val := c.getRaw(key, TypeBool)
upper := strings.ToUpper(key) return val.boolval
entry, ok := c.env[upper]
if ok {
return entry.parsed.boolval
}
}
return false
} }
func getFirstRune(str string)(rune) { func getFirstRune(str string)(rune) {