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 {
dtype DataType
intval int64
durval time.Duration
boolval bool

View File

@ -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) {