Allow integers to be unmarshaled into floats (#841)
Co-authored-by: Marty <martin@windscribe.com>
This commit is contained in:
+8
-1
@@ -7,13 +7,20 @@ import (
|
|||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestFastSimple(t *testing.T) {
|
func TestFastSimpleInt(t *testing.T) {
|
||||||
m := map[string]int64{}
|
m := map[string]int64{}
|
||||||
err := toml.Unmarshal([]byte(`a = 42`), &m)
|
err := toml.Unmarshal([]byte(`a = 42`), &m)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, map[string]int64{"a": 42}, m)
|
require.Equal(t, map[string]int64{"a": 42}, m)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFastSimpleFloat(t *testing.T) {
|
||||||
|
m := map[string]float64{}
|
||||||
|
err := toml.Unmarshal([]byte("a = 42\nb = 1.1\nc = 12341234123412341234123412341234"), &m)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, map[string]float64{"a": 42, "b": 1.1, "c": 1.2341234123412342e+31}, m)
|
||||||
|
}
|
||||||
|
|
||||||
func TestFastSimpleString(t *testing.T) {
|
func TestFastSimpleString(t *testing.T) {
|
||||||
m := map[string]string{}
|
m := map[string]string{}
|
||||||
err := toml.Unmarshal([]byte(`a = "hello"`), &m)
|
err := toml.Unmarshal([]byte(`a = "hello"`), &m)
|
||||||
|
|||||||
@@ -1085,10 +1085,6 @@ func TestUnmarshalCheckConversionFloatInt(t *testing.T) {
|
|||||||
desc: "int",
|
desc: "int",
|
||||||
input: `I = 1e300`,
|
input: `I = 1e300`,
|
||||||
},
|
},
|
||||||
{
|
|
||||||
desc: "float",
|
|
||||||
input: `F = 9223372036854775806`,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range testCases {
|
for _, test := range testCases {
|
||||||
|
|||||||
+6
-1
@@ -887,6 +887,11 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (d *decoder) unmarshalInteger(value *unstable.Node, v reflect.Value) error {
|
func (d *decoder) unmarshalInteger(value *unstable.Node, v reflect.Value) error {
|
||||||
|
kind := v.Kind()
|
||||||
|
if kind == reflect.Float32 || kind == reflect.Float64 {
|
||||||
|
return d.unmarshalFloat(value, v)
|
||||||
|
}
|
||||||
|
|
||||||
i, err := parseInteger(value.Data)
|
i, err := parseInteger(value.Data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -894,7 +899,7 @@ func (d *decoder) unmarshalInteger(value *unstable.Node, v reflect.Value) error
|
|||||||
|
|
||||||
var r reflect.Value
|
var r reflect.Value
|
||||||
|
|
||||||
switch v.Kind() {
|
switch kind {
|
||||||
case reflect.Int64:
|
case reflect.Int64:
|
||||||
v.SetInt(i)
|
v.SetInt(i)
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
Reference in New Issue
Block a user