From 8d4809951765f97dbfba1ceeffcba044b4123802 Mon Sep 17 00:00:00 2001 From: Roy Olav Purser Date: Thu, 25 Mar 2021 12:06:08 +0100 Subject: [PATCH] add metric --- datatype.go | 1 + envconf.go | 10 ++++++++++ parsers.go | 27 +++++++++++++++++++++++++++ typeinfo.go | 4 ++++ 4 files changed, 42 insertions(+) diff --git a/datatype.go b/datatype.go index b68aa9f..6163de6 100644 --- a/datatype.go +++ b/datatype.go @@ -5,6 +5,7 @@ type DataType int const ( TypeNone DataType = iota TypeInt DataType = iota + TypeMetric DataType = iota TypeDuration DataType = iota TypeString DataType = iota TypeDirectory DataType = iota diff --git a/envconf.go b/envconf.go index 6b101d9..bb85f20 100644 --- a/envconf.go +++ b/envconf.go @@ -114,6 +114,16 @@ func (c *Config) GetInt(key string)(int64) { } return 0 } +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 +} func (c *Config) GetDirectory(key string)(string) { if c.parsed { upper := strings.ToUpper(key) diff --git a/parsers.go b/parsers.go index fc588bc..f3b8eda 100644 --- a/parsers.go +++ b/parsers.go @@ -1,5 +1,6 @@ package envconf import ("strconv" + "strings" "fmt" "errors" "os" @@ -16,6 +17,31 @@ func parseInt(key string, str string)(ret cValue) { return } +func parseMetric(key string, str string)(ret cValue) { + mod := int64(1) + str = strings.ToUpper(str) + if strings.HasSuffix(str, "K") { + mod = 1024 + str = strings.TrimSuffix(str, "K") + } else if strings.HasSuffix(str, "M") { + mod = 1024 * 1024 + str = strings.TrimSuffix(str, "M") + } else if strings.HasSuffix(str, "G") { + mod = 1024 * 1024 * 1024 + str = strings.TrimSuffix(str, "G") + } else if strings.HasSuffix(str, "T") { + mod = 1024 * 1024 * 1024 * 1024 + str = strings.TrimSuffix(str, "T") + } + val, err := strconv.ParseInt(str, 10, 64) + if err == nil { + ret.intval = mod * 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 { @@ -50,6 +76,7 @@ func parseDirectory(_ string, str string)(ret cValue) { return } + func parseString(_ string, str string)(ret cValue) { ret.strval = str return diff --git a/typeinfo.go b/typeinfo.go index 0109222..fde453a 100644 --- a/typeinfo.go +++ b/typeinfo.go @@ -7,24 +7,28 @@ var tInfo map[DataType]dataTypeInfo func init() { tInfo = make(map[DataType]dataTypeInfo) var intInfo dataTypeInfo + var metricInfo dataTypeInfo var durInfo dataTypeInfo var strInfo dataTypeInfo var dirInfo dataTypeInfo var boolInfo dataTypeInfo intInfo.name = "int" + metricInfo.name = "metric" durInfo.name = "duration" dirInfo.name = "directory" strInfo.name = "string" boolInfo.name = "bool" intInfo.parser = parseInt + metricInfo.parser = parseMetric durInfo.parser = parseDuration dirInfo.parser = parseDirectory strInfo.parser = parseString boolInfo.parser = parseBool tInfo[TypeInt] = intInfo + tInfo[TypeMetric] = metricInfo tInfo[TypeDuration] = durInfo tInfo[TypeString] = strInfo tInfo[TypeDirectory] = dirInfo