Initial codec support for int2 and int2[]
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"math"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -452,6 +453,141 @@ func GetAssignToDstType(dst interface{}) (interface{}, bool) {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
func convertToInt64ForEncode(v interface{}) (n int64, valid bool, err error) {
|
||||
if v == nil {
|
||||
return 0, false, nil
|
||||
}
|
||||
|
||||
switch v := v.(type) {
|
||||
case int8:
|
||||
return int64(v), true, nil
|
||||
case uint8:
|
||||
return int64(v), true, nil
|
||||
case int16:
|
||||
return int64(v), true, nil
|
||||
case uint16:
|
||||
return int64(v), true, nil
|
||||
case int32:
|
||||
return int64(v), true, nil
|
||||
case uint32:
|
||||
return int64(v), true, nil
|
||||
case int64:
|
||||
return int64(v), true, nil
|
||||
case uint64:
|
||||
if v > math.MaxInt64 {
|
||||
return 0, false, fmt.Errorf("%d is greater than maximum value for int64", v)
|
||||
}
|
||||
return int64(v), true, nil
|
||||
case int:
|
||||
return int64(v), true, nil
|
||||
case uint:
|
||||
if v > math.MaxInt64 {
|
||||
return 0, false, fmt.Errorf("%d is greater than maximum value for int64", v)
|
||||
}
|
||||
return int64(v), true, nil
|
||||
case string:
|
||||
num, err := strconv.ParseInt(v, 10, 64)
|
||||
if err != nil {
|
||||
return 0, false, err
|
||||
}
|
||||
return num, true, nil
|
||||
case float32:
|
||||
if v > math.MaxInt64 {
|
||||
return 0, false, fmt.Errorf("%f is greater than maximum value for int64", v)
|
||||
}
|
||||
return int64(v), true, nil
|
||||
case float64:
|
||||
if v > math.MaxInt64 {
|
||||
return 0, false, fmt.Errorf("%f is greater than maximum value for int64", v)
|
||||
}
|
||||
return int64(v), true, nil
|
||||
case *int8:
|
||||
if v == nil {
|
||||
return 0, false, nil
|
||||
} else {
|
||||
return convertToInt64ForEncode(*v)
|
||||
}
|
||||
case *uint8:
|
||||
if v == nil {
|
||||
return 0, false, nil
|
||||
} else {
|
||||
return convertToInt64ForEncode(*v)
|
||||
}
|
||||
case *int16:
|
||||
if v == nil {
|
||||
return 0, false, nil
|
||||
} else {
|
||||
return convertToInt64ForEncode(*v)
|
||||
}
|
||||
case *uint16:
|
||||
if v == nil {
|
||||
return 0, false, nil
|
||||
} else {
|
||||
return convertToInt64ForEncode(*v)
|
||||
}
|
||||
case *int32:
|
||||
if v == nil {
|
||||
return 0, false, nil
|
||||
} else {
|
||||
return convertToInt64ForEncode(*v)
|
||||
}
|
||||
case *uint32:
|
||||
if v == nil {
|
||||
return 0, false, nil
|
||||
} else {
|
||||
return convertToInt64ForEncode(*v)
|
||||
}
|
||||
case *int64:
|
||||
if v == nil {
|
||||
return 0, false, nil
|
||||
} else {
|
||||
return convertToInt64ForEncode(*v)
|
||||
}
|
||||
case *uint64:
|
||||
if v == nil {
|
||||
return 0, false, nil
|
||||
} else {
|
||||
return convertToInt64ForEncode(*v)
|
||||
}
|
||||
case *int:
|
||||
if v == nil {
|
||||
return 0, false, nil
|
||||
} else {
|
||||
return convertToInt64ForEncode(*v)
|
||||
}
|
||||
case *uint:
|
||||
if v == nil {
|
||||
return 0, false, nil
|
||||
} else {
|
||||
return convertToInt64ForEncode(*v)
|
||||
}
|
||||
case *string:
|
||||
if v == nil {
|
||||
return 0, false, nil
|
||||
} else {
|
||||
return convertToInt64ForEncode(*v)
|
||||
}
|
||||
case *float32:
|
||||
if v == nil {
|
||||
return 0, false, nil
|
||||
} else {
|
||||
return convertToInt64ForEncode(*v)
|
||||
}
|
||||
case *float64:
|
||||
if v == nil {
|
||||
return 0, false, nil
|
||||
} else {
|
||||
return convertToInt64ForEncode(*v)
|
||||
}
|
||||
|
||||
default:
|
||||
if originalvalue, ok := underlyingNumberType(v); ok {
|
||||
return convertToInt64ForEncode(originalvalue)
|
||||
}
|
||||
return 0, false, fmt.Errorf("cannot convert %v to int64", v)
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
kindTypes = map[reflect.Kind]reflect.Type{
|
||||
reflect.Bool: reflect.TypeOf(false),
|
||||
|
||||
Reference in New Issue
Block a user