mirror of https://github.com/spf13/cast.git
Compare commits
7 Commits
57f714178e
...
50b4e2d2c3
Author | SHA1 | Date |
---|---|---|
Sippakorn Raksakiart | 50b4e2d2c3 | |
Steve Francia | 487df00934 | |
Steve Francia | 955c718bf6 | |
Milan Lesichkov | 184982508e | |
skyjerry | 6e9731d50e | |
Sippakorn Raksakiart | 64deafa3ae | |
Sippakorn Raksakiart | d0ef62fd94 |
|
@ -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)
|
||||
|
|
14
cast_test.go
14
cast_test.go
|
@ -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},
|
||||
}
|
||||
|
||||
|
|
13
caste.go
13
caste.go
|
@ -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.
|
||||
|
|
Loading…
Reference in New Issue