diff --git a/datatype.go b/datatype.go index bf86e06..31603e7 100644 --- a/datatype.go +++ b/datatype.go @@ -5,29 +5,34 @@ import ( "time" ) -type DataType uint +type DataType uint32 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 + sizeBitmask DataType = 0xffff + typeBitmask DataType = sizeBitmask << 16 ) -func FixedHex(size uint) DataType { - return (DataType)(size<<16) | TypeHex +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 & 0xffff + return dtype & typeBitmask } -func (dtype DataType) typeAndSize() (DataType, uint) { - return (dtype & 0xffff), uint(dtype >> 16) +func (dtype DataType) typeAndSize() (DataType, int) { + return (dtype & typeBitmask), int(dtype & sizeBitmask) } type cValue struct { diff --git a/parsers.go b/parsers.go index 1680095..cb72180 100644 --- a/parsers.go +++ b/parsers.go @@ -11,7 +11,7 @@ import ( "time" ) -func parseInt(key string, str string, _ uint) (ret cValue) { +func parseInt(key string, str string, _ int) (ret cValue) { val, err := strconv.ParseInt(str, 10, 64) if err == nil { ret.intval = val @@ -21,7 +21,7 @@ func parseInt(key string, str string, _ uint) (ret cValue) { return } -func parseMetric(key string, str string, _ uint) (ret cValue) { +func parseMetric(key string, str string, _ int) (ret cValue) { mod := int64(1) str = strings.ToUpper(str) if strings.HasSuffix(str, "K") { @@ -46,7 +46,7 @@ func parseMetric(key string, str string, _ uint) (ret cValue) { return } -func parseDuration(key string, str string, _ uint) (ret cValue) { +func parseDuration(key string, str string, _ int) (ret cValue) { val, err := time.ParseDuration(str) if err == nil { ret.durval = val @@ -56,7 +56,7 @@ func parseDuration(key string, str string, _ uint) (ret cValue) { return } -func parseBool(key string, str string, _ uint) (ret cValue) { +func parseBool(key string, str string, _ int) (ret cValue) { val, err := strconv.ParseBool(str) if err == nil { ret.boolval = val @@ -66,9 +66,9 @@ func parseBool(key string, str string, _ uint) (ret cValue) { return } -func parseHex(key string, str string, size uint) (ret cValue) { +func parseHex(key string, str string, size int) (ret cValue) { val, err := hex.DecodeString(str) - if err == nil && (size == 0 || size == uint(len(val))) { + if err == nil && (size == 0 || size == len(val)) { ret.binval = val } else { if size == 0 { @@ -80,7 +80,7 @@ func parseHex(key string, str string, size uint) (ret cValue) { return } -func parseDirectory(_ string, str string, _ uint) (ret cValue) { +func parseDirectory(_ string, str string, _ int) (ret cValue) { wd, err := os.Getwd() if err == nil { if path.IsAbs(str) { @@ -94,7 +94,7 @@ func parseDirectory(_ string, str string, _ uint) (ret cValue) { return } -func parseString(_ string, str string, _ uint) (ret cValue) { +func parseString(_ string, str string, _ int) (ret cValue) { ret.strval = str return } diff --git a/typeinfo.go b/typeinfo.go index 0648ce8..3154934 100644 --- a/typeinfo.go +++ b/typeinfo.go @@ -1,7 +1,7 @@ package envconf type dataTypeInfo struct { - parser func(string, string, uint) cValue + parser func(string, string, int) cValue name string }