envconf/datatype.go
2022-01-29 13:03:17 +01:00

71 lines
1.3 KiB
Go

package envconf
import (
"fmt"
"time"
)
type DataType uint32
const (
sizeBitmask DataType = 0xffff
typeBitmask DataType = sizeBitmask << 16
)
const (
TypeNone DataType = iota << 16
TypeInt DataType = iota << 16
TypeMetric DataType = iota << 16
TypeDuration DataType = iota << 16
TypeString DataType = iota << 16
TypeDirectory DataType = iota << 16
TypeBool DataType = iota << 16
TypeHex DataType = iota << 16
)
func FixedHex(size uint16) DataType {
return TypeHex | DataType(size)
}
func (dtype DataType) baseType() DataType {
return dtype & typeBitmask
}
func (dtype DataType) typeAndSize() (DataType, int) {
return (dtype & typeBitmask), int(dtype & sizeBitmask)
}
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"
}