print parse
This commit is contained in:
parent
6979797eaf
commit
614c6000c5
66
datatype.go
66
datatype.go
@ -1,8 +1,5 @@
|
|||||||
package envconf
|
package envconf
|
||||||
import ("fmt"
|
import ("time")
|
||||||
"errors"
|
|
||||||
"time"
|
|
||||||
"strconv")
|
|
||||||
|
|
||||||
type DataType int
|
type DataType int
|
||||||
const (
|
const (
|
||||||
@ -13,20 +10,6 @@ const (
|
|||||||
TypeBool DataType = iota
|
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 {
|
type cValue struct {
|
||||||
intval int64
|
intval int64
|
||||||
durval time.Duration
|
durval time.Duration
|
||||||
@ -36,49 +19,16 @@ type cValue struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (dtype DataType) parse(key string, str string)(ret cValue) {
|
func (dtype DataType) parse(key string, str string)(ret cValue) {
|
||||||
parsers := make(map[DataType](func(string,string)(cValue)))
|
info, ok := tInfo[dtype]
|
||||||
parsers[TypeInt] = parseInt
|
|
||||||
parsers[TypeDuration] = parseDuration
|
|
||||||
parsers[TypeString] = parseString
|
|
||||||
parsers[TypeBool] = parseBool
|
|
||||||
parser, ok := parsers[dtype]
|
|
||||||
if ok {
|
if ok {
|
||||||
return parser(key, str)
|
return info.parser(key, str)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
func (dtype DataType) String()(string) {
|
||||||
func parseInt(key string, str string)(ret cValue) {
|
info, ok := tInfo[dtype]
|
||||||
val, err := strconv.ParseInt(str, 10, 64)
|
if ok {
|
||||||
if err == nil {
|
return info.name
|
||||||
ret.intval = val
|
|
||||||
} else {
|
|
||||||
ret.err = errors.New(fmt.Sprintf(`Environment variable "%s" is not of type int.`, key))
|
|
||||||
}
|
}
|
||||||
return
|
return "invalid"
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
43
parsers.go
Normal file
43
parsers.go
Normal 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
28
typeinfo.go
Normal 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
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user