From 3e5701fbbe4456c161554120da38b8cc11157b88 Mon Sep 17 00:00:00 2001 From: Marty Date: Tue, 7 Feb 2023 16:13:48 +0100 Subject: [PATCH 1/2] allow ints to be unmarshaled into floats --- fast_test.go | 9 ++++++++- internal/imported_tests/unmarshal_imported_test.go | 4 ---- unmarshaler.go | 4 ++++ 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/fast_test.go b/fast_test.go index 02910bb2..a9363b0f 100644 --- a/fast_test.go +++ b/fast_test.go @@ -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) diff --git a/internal/imported_tests/unmarshal_imported_test.go b/internal/imported_tests/unmarshal_imported_test.go index 7a396e21..ead9bfb0 100644 --- a/internal/imported_tests/unmarshal_imported_test.go +++ b/internal/imported_tests/unmarshal_imported_test.go @@ -1085,10 +1085,6 @@ func TestUnmarshalCheckConversionFloatInt(t *testing.T) { desc: "int", input: `I = 1e300`, }, - { - desc: "float", - input: `F = 9223372036854775806`, - }, } for _, test := range testCases { diff --git a/unmarshaler.go b/unmarshaler.go index 70f6ec57..58427ba1 100644 --- a/unmarshaler.go +++ b/unmarshaler.go @@ -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: From 9efce1ff49be16bd458a5a1f495db06ddc55ffb7 Mon Sep 17 00:00:00 2001 From: Marty Date: Thu, 9 Feb 2023 15:14:28 +0100 Subject: [PATCH 2/2] allow parsing of int values that don't fit into int64 --- fast_test.go | 4 ++-- unmarshaler.go | 11 ++++++----- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/fast_test.go b/fast_test.go index a9363b0f..14d1def0 100644 --- a/fast_test.go +++ b/fast_test.go @@ -16,9 +16,9 @@ func TestFastSimpleInt(t *testing.T) { func TestFastSimpleFloat(t *testing.T) { m := map[string]float64{} - err := toml.Unmarshal([]byte("a = 42\nb = 1.1"), &m) + 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}, m) + require.Equal(t, map[string]float64{"a": 42, "b": 1.1, "c": 1.2341234123412342e+31}, m) } func TestFastSimpleString(t *testing.T) { diff --git a/unmarshaler.go b/unmarshaler.go index 58427ba1..25ec05fe 100644 --- a/unmarshaler.go +++ b/unmarshaler.go @@ -887,6 +887,11 @@ 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 @@ -894,7 +899,7 @@ func (d *decoder) unmarshalInteger(value *unstable.Node, v reflect.Value) error var r reflect.Value - switch v.Kind() { + switch kind { case reflect.Int64: v.SetInt(i) return nil @@ -952,10 +957,6 @@ 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: