2021-03-23 11:57:09 +00:00
|
|
|
package envconf
|
2021-03-26 16:17:52 +00:00
|
|
|
|
|
|
|
import (
|
2022-01-28 18:38:27 +00:00
|
|
|
"fmt"
|
2021-03-26 16:17:52 +00:00
|
|
|
"time"
|
|
|
|
)
|
2021-03-23 13:37:00 +00:00
|
|
|
|
2022-01-29 12:03:17 +00:00
|
|
|
type DataType uint32
|
2021-03-26 16:17:52 +00:00
|
|
|
|
2021-03-23 11:57:09 +00:00
|
|
|
const (
|
2022-01-29 12:03:17 +00:00
|
|
|
sizeBitmask DataType = 0xffff
|
|
|
|
typeBitmask DataType = sizeBitmask << 16
|
2021-03-23 11:57:09 +00:00
|
|
|
)
|
|
|
|
|
2022-01-29 12:03:17 +00:00
|
|
|
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)
|
2022-01-28 18:38:27 +00:00
|
|
|
}
|
|
|
|
|
2022-01-28 18:46:16 +00:00
|
|
|
func (dtype DataType) baseType() DataType {
|
2022-01-29 12:03:17 +00:00
|
|
|
return dtype & typeBitmask
|
2022-01-28 18:46:16 +00:00
|
|
|
}
|
|
|
|
|
2022-01-29 12:03:17 +00:00
|
|
|
func (dtype DataType) typeAndSize() (DataType, int) {
|
|
|
|
return (dtype & typeBitmask), int(dtype & sizeBitmask)
|
2022-01-28 18:38:27 +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) {
|
2022-01-28 18:38:27 +00:00
|
|
|
rdtype, size := dtype.typeAndSize()
|
|
|
|
info, ok := tInfo[rdtype]
|
2021-03-26 16:17:52 +00:00
|
|
|
if ok {
|
2022-01-28 18:38:27 +00:00
|
|
|
ret = info.parser(key, str, size)
|
2022-01-16 17:24:24 +00:00
|
|
|
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
|
|
|
}
|
2022-01-28 18:38:27 +00:00
|
|
|
|
2021-03-26 16:17:52 +00:00
|
|
|
func (dtype DataType) String() string {
|
2022-01-28 18:38:27 +00:00
|
|
|
rdtype, size := dtype.typeAndSize()
|
|
|
|
info, ok := tInfo[rdtype]
|
2021-03-26 16:17:52 +00:00
|
|
|
if ok {
|
2022-01-28 18:38:27 +00:00
|
|
|
if size > 0 {
|
|
|
|
return fmt.Sprintf("%s%d", info.name, size)
|
|
|
|
}
|
2021-03-26 16:17:52 +00:00
|
|
|
return info.name
|
|
|
|
}
|
|
|
|
return "invalid"
|
2021-03-23 13:37:00 +00:00
|
|
|
}
|