From 7320235ea5c0756a00f9d26003506e0f3399f498 Mon Sep 17 00:00:00 2001 From: Matheus Alcantara Date: Sat, 24 Aug 2019 01:22:41 -0300 Subject: [PATCH 1/3] Fixed #289: Float64 trucating --- marshal_test.go | 21 ++++++++++++++++++++- tomltree_write.go | 4 ++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/marshal_test.go b/marshal_test.go index 45c0e921..ecd0fa34 100644 --- a/marshal_test.go +++ b/marshal_test.go @@ -55,6 +55,9 @@ Ystrlist = ["Howdy","Hey There"] String2 = "Three" `) +var testMarshalFloat64 = []byte(`Value = 1.24675324675 +`) + func TestBasicMarshal(t *testing.T) { result, err := Marshal(basicTestData) if err != nil { @@ -101,6 +104,22 @@ func TestBasicMarshalOrderedWithPointer(t *testing.T) { } } +func TestMarshalFloat64(t *testing.T) { + foo := struct { + Value float64 + }{ + Value: 1.24675324675, + } + b, err := Marshal(&foo) + if err != nil { + t.Fatal(err) + } + expected := testMarshalFloat64 + if !bytes.Equal(b, expected) { + t.Errorf("Bad marshal: expected\n-----\n%s\n-----\ngot\n-----\n%s\n-----\n", expected, b) + } +} + func TestBasicUnmarshal(t *testing.T) { result := basicMarshalTestStruct{} err := Unmarshal(basicTestToml, &result) @@ -1495,7 +1514,7 @@ func TestUnmarshalPreservesUnexportedFields(t *testing.T) { [[slice1]] exported1 = "visible3" - + [[slice1]] exported1 = "visible4" diff --git a/tomltree_write.go b/tomltree_write.go index 9381a88f..93acb5d4 100644 --- a/tomltree_write.go +++ b/tomltree_write.go @@ -109,9 +109,9 @@ func tomlValueStringRepresentation(v interface{}, indent string, arraysOneElemen // Ensure a round float does contain a decimal point. Otherwise feeding // the output back to the parser would convert to an integer. if math.Trunc(value) == value { - return strings.ToLower(strconv.FormatFloat(value, 'f', 1, 32)), nil + return strings.ToLower(strconv.FormatFloat(value, 'f', 1, 64)), nil } - return strings.ToLower(strconv.FormatFloat(value, 'f', -1, 32)), nil + return strings.ToLower(strconv.FormatFloat(value, 'f', -1, 64)), nil case string: if tv.multiline { return "\"\"\"\n" + encodeMultilineTomlString(value) + "\"\"\"", nil From 79bed9a866a9cc679bf1d808b9ad4cf77935d91e Mon Sep 17 00:00:00 2001 From: Matheus Alcantara Date: Sat, 24 Aug 2019 02:46:19 -0300 Subject: [PATCH 2/3] Fixed #289: fixed parse of float32 and float64 --- marshal.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/marshal.go b/marshal.go index 6984dd8f..f2a68eaa 100644 --- a/marshal.go +++ b/marshal.go @@ -422,7 +422,7 @@ func (e *Encoder) valueToToml(mtype reflect.Type, mval reflect.Value) (interface case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: return mval.Uint(), nil case reflect.Float32, reflect.Float64: - return mval.Float(), nil + return strconv.ParseFloat(fmt.Sprintf("%v", mval.Interface()), 64) case reflect.String: return mval.String(), nil case reflect.Struct: From dad49490568f64fe2fa3673399be70c0094e3748 Mon Sep 17 00:00:00 2001 From: Matheus Alcantara Date: Mon, 26 Aug 2019 21:00:56 -0300 Subject: [PATCH 3/3] Fixed #289: improvement parse float 32 and 64 --- marshal.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/marshal.go b/marshal.go index f2a68eaa..4f0f8cf5 100644 --- a/marshal.go +++ b/marshal.go @@ -421,8 +421,10 @@ func (e *Encoder) valueToToml(mtype reflect.Type, mval reflect.Value) (interface return mval.Int(), nil case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: return mval.Uint(), nil - case reflect.Float32, reflect.Float64: - return strconv.ParseFloat(fmt.Sprintf("%v", mval.Interface()), 64) + case reflect.Float32: + return strconv.ParseFloat(strconv.FormatFloat(float64(mval.Interface().(float32)), 'f', -1, 32), 64) + case reflect.Float64: + return mval.Float(), nil case reflect.String: return mval.String(), nil case reflect.Struct: