envconf/datatype.go

47 lines
785 B
Go
Raw Normal View History

2021-03-23 11:57:09 +00:00
package envconf
2021-03-26 16:17:52 +00:00
import (
"time"
)
2021-03-23 13:37:00 +00:00
2021-03-23 11:57:09 +00:00
type DataType int
2021-03-26 16:17:52 +00:00
2021-03-23 11:57:09 +00:00
const (
2021-03-26 16:17:52 +00:00
TypeNone DataType = iota
TypeInt DataType = iota
TypeMetric DataType = iota
TypeDuration DataType = iota
TypeString DataType = iota
TypeDirectory DataType = iota
TypeBool DataType = iota
2022-01-16 17:24:24 +00:00
TypeHex DataType = iota
2021-03-23 11:57:09 +00:00
)
2021-03-23 13:37:00 +00:00
type cValue struct {
2021-03-26 16:17:52 +00:00
dtype DataType
intval int64
durval time.Duration
boolval bool
2022-01-16 17:24:24 +00:00
binval []byte
2021-03-26 16:17:52 +00:00
strval string
err error
2021-03-23 13:37:00 +00:00
}
2021-03-26 16:17:52 +00:00
func (dtype DataType) parse(key string, str string) (ret cValue) {
info, ok := tInfo[dtype]
if ok {
2022-01-16 17:24:24 +00:00
ret = info.parser(key, str)
if len(ret.binval) == 0 {
ret.binval = make([]byte, 0, 0)
}
2021-03-26 16:17:52 +00:00
}
return
2021-03-23 13:37:00 +00:00
}
2021-03-26 16:17:52 +00:00
func (dtype DataType) String() string {
info, ok := tInfo[dtype]
if ok {
return info.name
}
return "invalid"
2021-03-23 13:37:00 +00:00
}