Skip to content

Commit

Permalink
allow ints to be unmarshaled into floats
Browse files Browse the repository at this point in the history
  • Loading branch information
Marty committed Feb 7, 2023
1 parent 090cccf commit 3e5701f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 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"), &m)
require.NoError(t, err)
require.Equal(t, map[string]float64{"a": 42, "b": 1.1}, 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
4 changes: 4 additions & 0 deletions unmarshaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,10 @@ func (d *decoder) unmarshalInteger(value *unstable.Node, v reflect.Value) error
}

r = reflect.ValueOf(uint(i))
case reflect.Float32:
r = reflect.ValueOf(float32(i))
case reflect.Float64:
r = reflect.ValueOf(float64(i))
case reflect.Interface:
r = reflect.ValueOf(i)
default:
Expand Down

0 comments on commit 3e5701f

Please sign in to comment.