Add cases to support casting from json.Number values.
This commit is contained in:
parent
8965335b8c
commit
78b34312c4
25
cast_test.go
25
cast_test.go
@ -6,6 +6,7 @@
|
||||
package cast
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"testing"
|
||||
@ -30,6 +31,7 @@ func TestToUintE(t *testing.T) {
|
||||
{uint16(8), 8, false},
|
||||
{uint32(8), 8, false},
|
||||
{uint64(8), 8, false},
|
||||
{json.Number("8"), 8, false},
|
||||
{float32(8.31), 8, false},
|
||||
{float64(8.31), 8, false},
|
||||
{true, 1, false},
|
||||
@ -45,6 +47,7 @@ func TestToUintE(t *testing.T) {
|
||||
{float32(-8.31), 0, true},
|
||||
{float64(-8.31), 0, true},
|
||||
{"-8", 0, true},
|
||||
{json.Number("-8"), 0, true},
|
||||
{"test", 0, true},
|
||||
{testing.T{}, 0, true},
|
||||
}
|
||||
@ -88,6 +91,7 @@ func TestToUint64E(t *testing.T) {
|
||||
{true, 1, false},
|
||||
{false, 0, false},
|
||||
{"8", 8, false},
|
||||
{json.Number("8"), 8, false},
|
||||
{nil, 0, false},
|
||||
// errors
|
||||
{int(-8), 0, true},
|
||||
@ -98,6 +102,7 @@ func TestToUint64E(t *testing.T) {
|
||||
{float32(-8.31), 0, true},
|
||||
{float64(-8.31), 0, true},
|
||||
{"-8", 0, true},
|
||||
{json.Number("-8"), 0, true},
|
||||
{"test", 0, true},
|
||||
{testing.T{}, 0, true},
|
||||
}
|
||||
@ -141,6 +146,7 @@ func TestToUint32E(t *testing.T) {
|
||||
{true, 1, false},
|
||||
{false, 0, false},
|
||||
{"8", 8, false},
|
||||
{json.Number("8"), 8, false},
|
||||
{nil, 0, false},
|
||||
{int(-8), 0, true},
|
||||
{int8(-8), 0, true},
|
||||
@ -150,6 +156,7 @@ func TestToUint32E(t *testing.T) {
|
||||
{float32(-8.31), 0, true},
|
||||
{float64(-8.31), 0, true},
|
||||
{"-8", 0, true},
|
||||
{json.Number("-8"), 0, true},
|
||||
// errors
|
||||
{"test", 0, true},
|
||||
{testing.T{}, 0, true},
|
||||
@ -194,6 +201,7 @@ func TestToUint16E(t *testing.T) {
|
||||
{true, 1, false},
|
||||
{false, 0, false},
|
||||
{"8", 8, false},
|
||||
{json.Number("8"), 8, false},
|
||||
{nil, 0, false},
|
||||
// errors
|
||||
{int(-8), 0, true},
|
||||
@ -204,6 +212,7 @@ func TestToUint16E(t *testing.T) {
|
||||
{float32(-8.31), 0, true},
|
||||
{float64(-8.31), 0, true},
|
||||
{"-8", 0, true},
|
||||
{json.Number("-8"), 0, true},
|
||||
{"test", 0, true},
|
||||
{testing.T{}, 0, true},
|
||||
}
|
||||
@ -247,6 +256,7 @@ func TestToUint8E(t *testing.T) {
|
||||
{true, 1, false},
|
||||
{false, 0, false},
|
||||
{"8", 8, false},
|
||||
{json.Number("8"), 8, false},
|
||||
{nil, 0, false},
|
||||
// errors
|
||||
{int(-8), 0, true},
|
||||
@ -257,6 +267,7 @@ func TestToUint8E(t *testing.T) {
|
||||
{float32(-8.31), 0, true},
|
||||
{float64(-8.31), 0, true},
|
||||
{"-8", 0, true},
|
||||
{json.Number("-8"), 0, true},
|
||||
{"test", 0, true},
|
||||
{testing.T{}, 0, true},
|
||||
}
|
||||
@ -300,9 +311,11 @@ func TestToIntE(t *testing.T) {
|
||||
{true, 1, false},
|
||||
{false, 0, false},
|
||||
{"8", 8, false},
|
||||
{json.Number("8"), 8, false},
|
||||
{nil, 0, false},
|
||||
// errors
|
||||
{"test", 0, true},
|
||||
{json.Number("test"), 0, true},
|
||||
{testing.T{}, 0, true},
|
||||
}
|
||||
|
||||
@ -345,9 +358,11 @@ func TestToInt64E(t *testing.T) {
|
||||
{true, 1, false},
|
||||
{false, 0, false},
|
||||
{"8", 8, false},
|
||||
{json.Number("8"), 8, false},
|
||||
{nil, 0, false},
|
||||
// errors
|
||||
{"test", 0, true},
|
||||
{json.Number("test"), 0, true},
|
||||
{testing.T{}, 0, true},
|
||||
}
|
||||
|
||||
@ -390,9 +405,11 @@ func TestToInt32E(t *testing.T) {
|
||||
{true, 1, false},
|
||||
{false, 0, false},
|
||||
{"8", 8, false},
|
||||
{json.Number("8"), 8, false},
|
||||
{nil, 0, false},
|
||||
// errors
|
||||
{"test", 0, true},
|
||||
{json.Number("test"), 0, true},
|
||||
{testing.T{}, 0, true},
|
||||
}
|
||||
|
||||
@ -435,9 +452,11 @@ func TestToInt16E(t *testing.T) {
|
||||
{true, 1, false},
|
||||
{false, 0, false},
|
||||
{"8", 8, false},
|
||||
{json.Number("8"), 8, false},
|
||||
{nil, 0, false},
|
||||
// errors
|
||||
{"test", 0, true},
|
||||
{json.Number("test"), 0, true},
|
||||
{testing.T{}, 0, true},
|
||||
}
|
||||
|
||||
@ -480,9 +499,11 @@ func TestToInt8E(t *testing.T) {
|
||||
{true, 1, false},
|
||||
{false, 0, false},
|
||||
{"8", 8, false},
|
||||
{json.Number("8"), 8, false},
|
||||
{nil, 0, false},
|
||||
// errors
|
||||
{"test", 0, true},
|
||||
{json.Number("test"), 0, true},
|
||||
{testing.T{}, 0, true},
|
||||
}
|
||||
|
||||
@ -523,10 +544,12 @@ func TestToFloat64E(t *testing.T) {
|
||||
{float32(8), 8, false},
|
||||
{float64(8.31), 8.31, false},
|
||||
{"8", 8, false},
|
||||
{json.Number("8.31"), 8.31, false},
|
||||
{true, 1, false},
|
||||
{false, 0, false},
|
||||
// errors
|
||||
{"test", 0, true},
|
||||
{json.Number("test"), 0, true},
|
||||
{testing.T{}, 0, true},
|
||||
}
|
||||
|
||||
@ -567,10 +590,12 @@ func TestToFloat32E(t *testing.T) {
|
||||
{float32(8.31), 8.31, false},
|
||||
{float64(8.31), 8.31, false},
|
||||
{"8", 8, false},
|
||||
{json.Number("8.31"), 8.31, false},
|
||||
{true, 1, false},
|
||||
{false, 0, false},
|
||||
// errors
|
||||
{"test", 0, true},
|
||||
{json.Number("test"), 0, true},
|
||||
{testing.T{}, 0, true},
|
||||
}
|
||||
|
||||
|
64
caste.go
64
caste.go
@ -131,6 +131,8 @@ func ToFloat64E(i interface{}) (float64, error) {
|
||||
return 1, nil
|
||||
}
|
||||
return 0, nil
|
||||
case json.Number:
|
||||
return s.Float64()
|
||||
default:
|
||||
return 0, fmt.Errorf("unable to cast %#v of type %T to float64", i, i)
|
||||
}
|
||||
@ -176,6 +178,12 @@ func ToFloat32E(i interface{}) (float32, error) {
|
||||
return 1, nil
|
||||
}
|
||||
return 0, nil
|
||||
case json.Number:
|
||||
v, err := s.Float64()
|
||||
if err == nil {
|
||||
return float32(v), nil
|
||||
}
|
||||
return 0, fmt.Errorf("unable to cast %#v of type %T to float32", i, i)
|
||||
default:
|
||||
return 0, fmt.Errorf("unable to cast %#v of type %T to float32", i, i)
|
||||
}
|
||||
@ -221,6 +229,8 @@ func ToInt64E(i interface{}) (int64, error) {
|
||||
return 1, nil
|
||||
}
|
||||
return 0, nil
|
||||
case json.Number:
|
||||
return s.Int64()
|
||||
case nil:
|
||||
return 0, nil
|
||||
default:
|
||||
@ -268,6 +278,12 @@ func ToInt32E(i interface{}) (int32, error) {
|
||||
return 1, nil
|
||||
}
|
||||
return 0, nil
|
||||
case json.Number:
|
||||
v, err := s.Int64()
|
||||
if err == nil {
|
||||
return int32(v), nil
|
||||
}
|
||||
return 0, fmt.Errorf("unable to cast %#v of type %T to int32", i, i)
|
||||
case nil:
|
||||
return 0, nil
|
||||
default:
|
||||
@ -315,6 +331,12 @@ func ToInt16E(i interface{}) (int16, error) {
|
||||
return 1, nil
|
||||
}
|
||||
return 0, nil
|
||||
case json.Number:
|
||||
v, err := s.Int64()
|
||||
if err == nil {
|
||||
return int16(v), nil
|
||||
}
|
||||
return 0, fmt.Errorf("unable to cast %#v of type %T to int16", i, i)
|
||||
case nil:
|
||||
return 0, nil
|
||||
default:
|
||||
@ -362,6 +384,12 @@ func ToInt8E(i interface{}) (int8, error) {
|
||||
return 1, nil
|
||||
}
|
||||
return 0, nil
|
||||
case json.Number:
|
||||
v, err := s.Int64()
|
||||
if err == nil {
|
||||
return int8(v), nil
|
||||
}
|
||||
return 0, fmt.Errorf("unable to cast %#v of type %T to int8", i, i)
|
||||
case nil:
|
||||
return 0, nil
|
||||
default:
|
||||
@ -409,6 +437,12 @@ func ToIntE(i interface{}) (int, error) {
|
||||
return 1, nil
|
||||
}
|
||||
return 0, nil
|
||||
case json.Number:
|
||||
v, err := s.Int64()
|
||||
if err == nil {
|
||||
return int(v), nil
|
||||
}
|
||||
return 0, fmt.Errorf("unable to cast %#v of type %T to int", i, i)
|
||||
case nil:
|
||||
return 0, nil
|
||||
default:
|
||||
@ -477,6 +511,12 @@ func ToUintE(i interface{}) (uint, error) {
|
||||
return 1, nil
|
||||
}
|
||||
return 0, nil
|
||||
case json.Number:
|
||||
v, err := strconv.ParseUint(s.String(), 10, 64)
|
||||
if err == nil {
|
||||
return uint(v), nil
|
||||
}
|
||||
return 0, fmt.Errorf("unable to cast %#v of type %T to uint", i, i)
|
||||
case nil:
|
||||
return 0, nil
|
||||
default:
|
||||
@ -545,6 +585,12 @@ func ToUint64E(i interface{}) (uint64, error) {
|
||||
return 1, nil
|
||||
}
|
||||
return 0, nil
|
||||
case json.Number:
|
||||
v, err := strconv.ParseUint(s.String(), 10, 64)
|
||||
if err == nil {
|
||||
return v, nil
|
||||
}
|
||||
return 0, fmt.Errorf("unable to cast %#v of type %T to uint64", i, i)
|
||||
case nil:
|
||||
return 0, nil
|
||||
default:
|
||||
@ -613,6 +659,12 @@ func ToUint32E(i interface{}) (uint32, error) {
|
||||
return 1, nil
|
||||
}
|
||||
return 0, nil
|
||||
case json.Number:
|
||||
v, err := strconv.ParseUint(s.String(), 10, 64)
|
||||
if err == nil {
|
||||
return uint32(v), nil
|
||||
}
|
||||
return 0, fmt.Errorf("unable to cast %#v of type %T to uint32", i, i)
|
||||
case nil:
|
||||
return 0, nil
|
||||
default:
|
||||
@ -681,6 +733,12 @@ func ToUint16E(i interface{}) (uint16, error) {
|
||||
return 1, nil
|
||||
}
|
||||
return 0, nil
|
||||
case json.Number:
|
||||
v, err := strconv.ParseUint(s.String(), 10, 64)
|
||||
if err == nil {
|
||||
return uint16(v), nil
|
||||
}
|
||||
return 0, fmt.Errorf("unable to cast %#v of type %T to uint16", i, i)
|
||||
case nil:
|
||||
return 0, nil
|
||||
default:
|
||||
@ -749,6 +807,12 @@ func ToUint8E(i interface{}) (uint8, error) {
|
||||
return 1, nil
|
||||
}
|
||||
return 0, nil
|
||||
case json.Number:
|
||||
v, err := strconv.ParseUint(s.String(), 10, 64)
|
||||
if err == nil {
|
||||
return uint8(v), nil
|
||||
}
|
||||
return 0, fmt.Errorf("unable to cast %#v of type %T to uint8", i, i)
|
||||
case nil:
|
||||
return 0, nil
|
||||
default:
|
||||
|
Loading…
Reference in New Issue
Block a user