Compare commits

...

7 Commits

Author SHA1 Message Date
Sippakorn Raksakiart 50b4e2d2c3
Merge 64deafa3ae into 487df00934 2024-11-22 14:13:18 +00:00
Steve Francia 487df00934
Merge pull request #213 from skyjerry/fix-uint64
Fix ToUint64 issue with string input exceeding maximum int64
2024-10-18 13:53:17 -04:00
Steve Francia 955c718bf6
Merge pull request #224 from lesichkovm/patch-1
Update README.md
2024-10-18 13:51:17 -04:00
Milan Lesichkov 184982508e
Update README.md
Fixed the workflow status badge to point to the correct test file.
2024-09-01 06:22:58 +01:00
skyjerry 6e9731d50e Title: Fix ToUint64E function in cast library for Go
Detail:
The ToUint6E4 function in the Go cast library was fixed to correctly handle strings that overflow the maximum value of int64.

Previously, the function incorrectly returned zero for these input values because it was using strconv.ParseInt internally, which doesn't handle values greater than the max int64. The fix replaces the use of strconv.ParseInt with strconv.ParseUint.
2024-03-12 22:17:08 +08:00
Sippakorn Raksakiart 64deafa3ae remove debuging code 2019-11-04 19:20:42 +07:00
Sippakorn Raksakiart d0ef62fd94 - add convert any type to ToStringMapE 2019-11-04 19:10:43 +07:00
3 changed files with 25 additions and 6 deletions

View File

@ -1,6 +1,6 @@
# cast
[![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/spf13/cast/ci.yaml?branch=master&style=flat-square)](https://github.com/spf13/cast/actions/workflows/ci.yaml)
[![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/spf13/cast/test.yaml?branch=master&style=flat-square)](https://github.com/spf13/cast/actions/workflows/test.yaml)
[![PkgGoDev](https://pkg.go.dev/badge/mod/github.com/spf13/cast)](https://pkg.go.dev/mod/github.com/spf13/cast)
![Go Version](https://img.shields.io/badge/go%20version-%3E=1.16-61CFDD.svg?style=flat-square)
[![Go Report Card](https://goreportcard.com/badge/github.com/spf13/cast?style=flat-square)](https://goreportcard.com/report/github.com/spf13/cast)

View File

@ -111,6 +111,12 @@ func TestToUintE(t *testing.T) {
func TestToUint64E(t *testing.T) {
tests := createNumberTestSteps(uint64(0), uint64(1), uint64(8), uint64(0), uint64(8), uint64(8))
// Maximum value of uint64
tests = append(tests,
testStep{"18446744073709551615", uint64(18446744073709551615), false},
testStep{"18446744073709551616", uint64(0), true},
)
runNumberTest(
qt.New(t),
tests,
@ -410,10 +416,14 @@ func TestToStringMapE(t *testing.T) {
{map[string]interface{}{"tag": "tags", "group": "groups"}, map[string]interface{}{"tag": "tags", "group": "groups"}, false},
{`{"tag": "tags", "group": "groups"}`, map[string]interface{}{"tag": "tags", "group": "groups"}, false},
{`{"tag": "tags", "group": true}`, map[string]interface{}{"tag": "tags", "group": true}, false},
{struct {
Hello string
}{
Hello: "World",
}, map[string]interface{}{"Hello": "World"}, false},
{testing.T{}, map[string]interface{}{}, false},
// errors
{nil, nil, true},
{testing.T{}, nil, true},
{"", nil, true},
}

View File

@ -613,12 +613,12 @@ 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, 0)
if err == nil {
if v < 0 {
return 0, errNegativeNotAllowed
}
return uint64(v), nil
return v, nil
}
return 0, fmt.Errorf("unable to cast %#v of type %T to uint64", i, i)
case json.Number:
@ -1135,8 +1135,17 @@ func ToStringMapE(i interface{}) (map[string]interface{}, error) {
err := jsonStringToObject(v, &m)
return m, err
default:
vo := reflect.ValueOf(i)
if vo.Kind() == reflect.Invalid {
return m, fmt.Errorf("unable to cast %#v of type %T to map[string]interface{}", i, i)
}
for i := 0; i < vo.NumField(); i++ {
if vo.Field(i).CanInterface() {
m[vo.Type().Field(i).Name] = vo.Field(i).Interface()
}
}
return m, nil
}
}
// ToStringMapIntE casts an interface to a map[string]int{} type.