Skip to content

Commit

Permalink
Allow integers to be unmarshaled into floats (#841)
Browse files Browse the repository at this point in the history
Co-authored-by: Marty <martin@windscribe.com>
  • Loading branch information
PotatoesFall and Marty authored Feb 9, 2023
1 parent c4a2eef commit 9f57260
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
9 changes: 8 additions & 1 deletion fast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,20 @@ import (
"github.com/stretchr/testify/require"
)

func TestFastSimple(t *testing.T) {
func TestFastSimpleInt(t *testing.T) {
m := map[string]int64{}
err := toml.Unmarshal([]byte(`a = 42`), &m)
require.NoError(t, err)
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) {
m := map[string]string{}
err := toml.Unmarshal([]byte(`a = "hello"`), &m)
Expand Down
4 changes: 0 additions & 4 deletions internal/imported_tests/unmarshal_imported_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1085,10 +1085,6 @@ func TestUnmarshalCheckConversionFloatInt(t *testing.T) {
desc: "int",
input: `I = 1e300`,
},
{
desc: "float",
input: `F = 9223372036854775806`,
},
}

for _, test := range testCases {
Expand Down
7 changes: 6 additions & 1 deletion unmarshaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -887,14 +887,19 @@ func init() {
}

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)
if err != nil {
return err
}

var r reflect.Value

switch v.Kind() {
switch kind {
case reflect.Int64:
v.SetInt(i)
return nil
Expand Down

0 comments on commit 9f57260

Please sign in to comment.