envconf/datatype.go

62 lines
1.1 KiB
Go

package envconf
import (
"fmt"
"time"
)
type DataType uint
const (
TypeNone DataType = iota
TypeInt DataType = iota
TypeMetric DataType = iota
TypeDuration DataType = iota
TypeString DataType = iota
TypeDirectory DataType = iota
TypeBool DataType = iota
TypeHex DataType = iota
)
func FixedHex(size uint) DataType {
return (DataType)(size<<16) | TypeHex
}
func (dtype DataType) typeAndSize() (DataType, uint) {
return (dtype & 0xffff), uint(dtype >> 16)
}
type cValue struct {
dtype DataType
intval int64
durval time.Duration
boolval bool
binval []byte
strval string
err error
}
func (dtype DataType) parse(key string, str string) (ret cValue) {
rdtype, size := dtype.typeAndSize()
info, ok := tInfo[rdtype]
if ok {
ret = info.parser(key, str, size)
if len(ret.binval) == 0 {
ret.binval = make([]byte, 0, 0)
}
}
return
}
func (dtype DataType) String() string {
rdtype, size := dtype.typeAndSize()
info, ok := tInfo[rdtype]
if ok {
if size > 0 {
return fmt.Sprintf("%s%d", info.name, size)
}
return info.name
}
return "invalid"
}