2
0

Builtin types are automatically wrapped if necessary

This commit is contained in:
Jack Christensen
2022-01-08 23:44:53 -06:00
parent 8aaf235595
commit ad79dccd99
6 changed files with 725 additions and 266 deletions
-136
View File
@@ -5,7 +5,6 @@ import (
"fmt"
"math"
"reflect"
"strconv"
"time"
)
@@ -453,141 +452,6 @@ 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),