Skip to content

Fix issue where ToUintE() fails to parse uint bigger than math.MaxInt64 #155

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions cast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"errors"
"fmt"
"html/template"
"math"
"path"
"reflect"
"testing"
Expand Down Expand Up @@ -109,7 +110,7 @@ func TestToUintE(t *testing.T) {
}

func TestToUint64E(t *testing.T) {
tests := createNumberTestSteps(uint64(0), uint64(1), uint64(8), uint64(0), uint64(8), uint64(8))
tests := createNumberTestSteps(uint64(0), uint64(1), uint64(8), uint64(0), uint64(8), uint64(math.MaxUint64))

runNumberTest(
qt.New(t),
Expand All @@ -120,7 +121,7 @@ func TestToUint64E(t *testing.T) {
}

func TestToUint32E(t *testing.T) {
tests := createNumberTestSteps(uint32(0), uint32(1), uint32(8), uint32(0), uint32(8), uint32(8))
tests := createNumberTestSteps(uint32(0), uint32(1), uint32(8), uint32(0), uint32(8), uint32(math.MaxUint32))

runNumberTest(
qt.New(t),
Expand All @@ -131,7 +132,7 @@ func TestToUint32E(t *testing.T) {
}

func TestToUint16E(t *testing.T) {
tests := createNumberTestSteps(uint16(0), uint16(1), uint16(8), uint16(0), uint16(8), uint16(8))
tests := createNumberTestSteps(uint16(0), uint16(1), uint16(8), uint16(0), uint16(8), uint16(math.MaxUint16))

runNumberTest(
qt.New(t),
Expand All @@ -142,7 +143,7 @@ func TestToUint16E(t *testing.T) {
}

func TestToUint8E(t *testing.T) {
tests := createNumberTestSteps(uint8(0), uint8(1), uint8(8), uint8(0), uint8(8), uint8(8))
tests := createNumberTestSteps(uint8(0), uint8(1), uint8(8), uint8(0), uint8(8), uint8(math.MaxUint8))

runNumberTest(
qt.New(t),
Expand All @@ -163,7 +164,7 @@ func TestToIntE(t *testing.T) {
}

func TestToInt64E(t *testing.T) {
tests := createNumberTestSteps(int64(0), int64(1), int64(8), int64(-8), int64(8), int64(-8))
tests := createNumberTestSteps(int64(0), int64(1), int64(8), int64(-8), int64(math.MaxInt64), int64(math.MinInt64))

runNumberTest(
qt.New(t),
Expand All @@ -174,7 +175,7 @@ func TestToInt64E(t *testing.T) {
}

func TestToInt32E(t *testing.T) {
tests := createNumberTestSteps(int32(0), int32(1), int32(8), int32(-8), int32(8), int32(-8))
tests := createNumberTestSteps(int32(0), int32(1), int32(8), int32(-8), int32(math.MaxInt32), int32(math.MinInt32))

runNumberTest(
qt.New(t),
Expand All @@ -185,7 +186,7 @@ func TestToInt32E(t *testing.T) {
}

func TestToInt16E(t *testing.T) {
tests := createNumberTestSteps(int16(0), int16(1), int16(8), int16(-8), int16(8), int16(-8))
tests := createNumberTestSteps(int16(0), int16(1), int16(8), int16(-8), int16(math.MaxInt16), int16(math.MinInt16))

runNumberTest(
qt.New(t),
Expand All @@ -196,7 +197,7 @@ func TestToInt16E(t *testing.T) {
}

func TestToInt8E(t *testing.T) {
tests := createNumberTestSteps(int8(0), int8(1), int8(8), int8(-8), int8(8), int8(-8))
tests := createNumberTestSteps(int8(0), int8(1), int8(8), int8(-8), int8(math.MaxInt8), int8(math.MinInt8))

runNumberTest(
qt.New(t),
Expand Down
25 changes: 5 additions & 20 deletions caste.go
Original file line number Diff line number Diff line change
Expand Up @@ -522,11 +522,8 @@ func ToUintE(i interface{}) (uint, error) {

switch s := i.(type) {
case string:
v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0)
v, err := strconv.ParseUint(trimZeroDecimal(s), 0, 0)
if err == nil {
if v < 0 {
return 0, errNegativeNotAllowed
}
return uint(v), nil
}
return 0, fmt.Errorf("unable to cast %#v of type %T to uint", i, i)
Expand Down Expand Up @@ -598,11 +595,8 @@ func ToUint64E(i interface{}) (uint64, error) {

switch s := i.(type) {
case string:
v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0)
v, err := strconv.ParseUint(trimZeroDecimal(s), 0, 64)
if err == nil {
if v < 0 {
return 0, errNegativeNotAllowed
}
return uint64(v), nil
}
return 0, fmt.Errorf("unable to cast %#v of type %T to uint64", i, i)
Expand Down Expand Up @@ -674,11 +668,8 @@ func ToUint32E(i interface{}) (uint32, error) {

switch s := i.(type) {
case string:
v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0)
v, err := strconv.ParseUint(trimZeroDecimal(s), 0, 32)
if err == nil {
if v < 0 {
return 0, errNegativeNotAllowed
}
return uint32(v), nil
}
return 0, fmt.Errorf("unable to cast %#v of type %T to uint32", i, i)
Expand Down Expand Up @@ -750,11 +741,8 @@ func ToUint16E(i interface{}) (uint16, error) {

switch s := i.(type) {
case string:
v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0)
v, err := strconv.ParseUint(trimZeroDecimal(s), 0, 16)
if err == nil {
if v < 0 {
return 0, errNegativeNotAllowed
}
return uint16(v), nil
}
return 0, fmt.Errorf("unable to cast %#v of type %T to uint16", i, i)
Expand Down Expand Up @@ -826,11 +814,8 @@ func ToUint8E(i interface{}) (uint8, error) {

switch s := i.(type) {
case string:
v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0)
v, err := strconv.ParseUint(trimZeroDecimal(s), 0, 8)
if err == nil {
if v < 0 {
return 0, errNegativeNotAllowed
}
return uint8(v), nil
}
return 0, fmt.Errorf("unable to cast %#v of type %T to uint8", i, i)
Expand Down
Loading