print parse

This commit is contained in:
Roy Olav Purser 2021-03-24 11:51:09 +01:00
parent 6979797eaf
commit 614c6000c5
No known key found for this signature in database
GPG Key ID: 0BA77797F072BC52
3 changed files with 79 additions and 58 deletions

View File

@ -1,8 +1,5 @@
package envconf
import ("fmt"
"errors"
"time"
"strconv")
import ("time")
type DataType int
const (
@ -13,20 +10,6 @@ const (
TypeBool DataType = iota
)
func (dtype DataType) String()(string) {
types := make(map[DataType]string)
types[TypeNone] = "none"
types[TypeInt] = "int"
types[TypeDuration] = "duration"
types[TypeString] = "string"
types[TypeBool] = "bool"
str, ok := types[dtype]
if ok {
return str
}
return "invalid"
}
type cValue struct {
intval int64
durval time.Duration
@ -36,49 +19,16 @@ type cValue struct {
}
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]
info, ok := tInfo[dtype]
if ok {
return parser(key, str)
return info.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))
func (dtype DataType) String()(string) {
info, ok := tInfo[dtype]
if ok {
return info.name
}
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
return "invalid"
}

43
parsers.go Normal file
View File

@ -0,0 +1,43 @@
package envconf
import ("strconv"
"fmt"
"errors"
"time")
func parseInt(key string, str string)(ret cValue) {
val, err := strconv.ParseInt(str, 10, 64)
fmt.Println(val)
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)
fmt.Println(val)
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)
fmt.Println(val)
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
}

28
typeinfo.go Normal file
View File

@ -0,0 +1,28 @@
package envconf
type dataTypeInfo struct {
parser func(string,string)(cValue)
name string
}
var tInfo map[DataType]dataTypeInfo
func init() {
tInfo = make(map[DataType]dataTypeInfo)
var intInfo dataTypeInfo
var durInfo dataTypeInfo
var strInfo dataTypeInfo
var boolInfo dataTypeInfo
intInfo.name = "int"
durInfo.name = "duration"
strInfo.name = "string"
boolInfo.name = "bool"
intInfo.parser = parseInt
durInfo.parser = parseDuration
strInfo.parser = parseString
boolInfo.parser = parseBool
tInfo[TypeInt] = intInfo
tInfo[TypeDuration] = durInfo
tInfo[TypeString] = strInfo
tInfo[TypeBool] = boolInfo
}