Skip to content

Commit

Permalink
string float act like float when converted to int*
Browse files Browse the repository at this point in the history
  • Loading branch information
yveshield committed Jan 9, 2024
1 parent 48ddde5 commit 12ae575
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 13 deletions.
14 changes: 11 additions & 3 deletions cast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ func createNumberTestSteps(zero, one, eight, eightnegative, eightpoint31, eightp
eightpoint31negative_32 = float64(float32(eightpoint31negative.(float64)))
}

streight := eight
streightnegative := eightnegative
if kind == reflect.Float32 || kind == reflect.Float64 {
streight = eightpoint31
streightnegative = eightpoint31negative
}

return []testStep{
{int(8), eight, false},
{int8(8), eight, false},
Expand All @@ -59,6 +66,7 @@ func createNumberTestSteps(zero, one, eight, eightnegative, eightpoint31, eightp
{true, one, false},
{false, zero, false},
{"8", eight, false},
{"8.31", streight, false},
{nil, zero, false},
{int(-8), eightnegative, isUint},
{int8(-8), eightnegative, isUint},
Expand All @@ -68,6 +76,7 @@ func createNumberTestSteps(zero, one, eight, eightnegative, eightpoint31, eightp
{float32(-8.31), eightpoint31negative_32, isUint},
{float64(-8.31), eightpoint31negative, isUint},
{"-8", eightnegative, isUint},
{"-8.31", streightnegative, isUint},
{jeight, eight, false},
{jminuseight, eightnegative, isUint},
{jfloateight, eight, false},
Expand Down Expand Up @@ -923,9 +932,8 @@ func TestIndirectPointers(t *testing.T) {
func TestToTime(t *testing.T) {
c := qt.New(t)

var jntime, jnetime json.Number
var jntime json.Number
_ = json.Unmarshal([]byte("1234567890"), &jntime)
_ = json.Unmarshal([]byte("123.4567890"), &jnetime)
tests := []struct {
input interface{}
expect time.Time
Expand Down Expand Up @@ -968,7 +976,7 @@ func TestToTime(t *testing.T) {
{time.Date(2009, 2, 13, 23, 31, 30, 0, time.UTC), time.Date(2009, 2, 13, 23, 31, 30, 0, time.UTC), false},
// errors
{"2006", time.Time{}, true},
{jnetime, time.Time{}, true},
{nil, time.Time{}, true},
{testing.T{}, time.Time{}, true},
}

Expand Down
38 changes: 28 additions & 10 deletions caste.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"strconv"
"strings"
"time"
"unicode"
)

var errNegativeNotAllowed = errors.New("unable to cast negative value")
Expand Down Expand Up @@ -281,7 +282,7 @@ func ToInt64E(i interface{}) (int64, error) {
case float32:
return int64(s), nil
case string:
v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0)
v, err := strconv.ParseInt(truncateDecimals(trimZeroDecimal(s)), 0, 0)
if err == nil {
return v, nil
}
Expand Down Expand Up @@ -333,7 +334,7 @@ func ToInt32E(i interface{}) (int32, error) {
case float32:
return int32(s), nil
case string:
v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0)
v, err := strconv.ParseInt(truncateDecimals(trimZeroDecimal(s)), 0, 0)
if err == nil {
return int32(v), nil
}
Expand Down Expand Up @@ -385,7 +386,7 @@ func ToInt16E(i interface{}) (int16, error) {
case float32:
return int16(s), nil
case string:
v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0)
v, err := strconv.ParseInt(truncateDecimals(trimZeroDecimal(s)), 0, 0)
if err == nil {
return int16(v), nil
}
Expand Down Expand Up @@ -437,7 +438,7 @@ func ToInt8E(i interface{}) (int8, error) {
case float32:
return int8(s), nil
case string:
v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0)
v, err := strconv.ParseInt(truncateDecimals(trimZeroDecimal(s)), 0, 0)
if err == nil {
return int8(v), nil
}
Expand Down Expand Up @@ -489,7 +490,7 @@ func ToIntE(i interface{}) (int, error) {
case float32:
return int(s), nil
case string:
v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0)
v, err := strconv.ParseInt(truncateDecimals(trimZeroDecimal(s)), 0, 0)
if err == nil {
return int(v), nil
}
Expand Down Expand Up @@ -522,7 +523,7 @@ func ToUintE(i interface{}) (uint, error) {

switch s := i.(type) {
case string:
v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0)
v, err := strconv.ParseInt(truncateDecimals(trimZeroDecimal(s)), 0, 0)
if err == nil {
if v < 0 {
return 0, errNegativeNotAllowed
Expand Down Expand Up @@ -598,7 +599,7 @@ func ToUint64E(i interface{}) (uint64, error) {

switch s := i.(type) {
case string:
v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0)
v, err := strconv.ParseInt(truncateDecimals(trimZeroDecimal(s)), 0, 0)
if err == nil {
if v < 0 {
return 0, errNegativeNotAllowed
Expand Down Expand Up @@ -674,7 +675,7 @@ func ToUint32E(i interface{}) (uint32, error) {

switch s := i.(type) {
case string:
v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0)
v, err := strconv.ParseInt(truncateDecimals(trimZeroDecimal(s)), 0, 0)
if err == nil {
if v < 0 {
return 0, errNegativeNotAllowed
Expand Down Expand Up @@ -750,7 +751,7 @@ func ToUint16E(i interface{}) (uint16, error) {

switch s := i.(type) {
case string:
v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0)
v, err := strconv.ParseInt(truncateDecimals(trimZeroDecimal(s)), 0, 0)
if err == nil {
if v < 0 {
return 0, errNegativeNotAllowed
Expand Down Expand Up @@ -826,7 +827,7 @@ func ToUint8E(i interface{}) (uint8, error) {

switch s := i.(type) {
case string:
v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0)
v, err := strconv.ParseInt(truncateDecimals(trimZeroDecimal(s)), 0, 0)
if err == nil {
if v < 0 {
return 0, errNegativeNotAllowed
Expand Down Expand Up @@ -1496,3 +1497,20 @@ func trimZeroDecimal(s string) string {
}
return s
}

func truncateDecimals(s string) string {
_, err := strconv.ParseFloat(s, 64)
if err != nil {
return s
}
v := strings.Index(s, ".")
if v == -1 {
return s
}
for i := v + 1; i < len(s); i++ {
if !unicode.IsDigit(rune(s[i])) {
return s
}
}
return s[:v]
}

0 comments on commit 12ae575

Please sign in to comment.