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-28 18:38:27 +00:00
|
|
|
type DataType uint
|
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
|
|
|
)
|
|
|
|
|
2022-01-28 18:38:27 +00:00
|
|
|
func FixedHex(size uint) DataType {
|
|
|
|
return (DataType)(size<<16) | TypeHex
|
|
|
|
}
|
|
|
|
|
2022-01-28 18:46:16 +00:00
|
|
|
func (dtype DataType) baseType() DataType {
|
|
|
|
return dtype & 0xffff
|
|
|
|
}
|
|
|
|
|
2022-01-28 18:38:27 +00:00
|
|
|
func (dtype DataType) typeAndSize() (DataType, uint) {
|
|
|
|
return (dtype & 0xffff), uint(dtype >> 16)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|