From 51e686279795c608f94f8e8f57443713701694db Mon Sep 17 00:00:00 2001 From: tidwall Date: Fri, 3 May 2024 18:10:25 -0700 Subject: [PATCH] Fix field floating point parsing misrepresentation This commit fixes an issue where fields with floating points that have zero prefixes and underscores are being parsed as numbers. Now those are treated as string values. See #736 --- internal/field/field.go | 10 +++++++--- internal/field/field_test.go | 3 ++- internal/field/list_test.go | 2 +- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/internal/field/field.go b/internal/field/field.go index a14d9d89..6ab30b89 100644 --- a/internal/field/field.go +++ b/internal/field/field.go @@ -184,9 +184,13 @@ func ValueOf(data string) Value { } else if math.IsNaN(num) { return Value{kind: Number, data: "NaN", num: nan} } - return Value{kind: Number, data: data, num: num} - } - if gjson.Valid(data) { + // Make sure that this is a JSON compatible number. + // For example, "000123" and "000_123" both parse as floats but aren't + // really Numbers that can be represents in JSON. + if gjson.Valid(data) { + return Value{kind: Number, data: data, num: num} + } + } else if gjson.Valid(data) { data = strings.TrimSpace(data) r := gjson.Parse(data) switch r.Type { diff --git a/internal/field/field_test.go b/internal/field/field_test.go index 26830a03..31fdf751 100644 --- a/internal/field/field_test.go +++ b/internal/field/field_test.go @@ -130,5 +130,6 @@ func TestWeight(t *testing.T) { } func TestNumber(t *testing.T) { - assert.Assert(ValueOf("012").Num() == 12) + assert.Assert(ValueOf("12").Num() == 12) + assert.Assert(ValueOf("012").Num() == 0) } diff --git a/internal/field/list_test.go b/internal/field/list_test.go index 769fafe8..9fc7a5e2 100644 --- a/internal/field/list_test.go +++ b/internal/field/list_test.go @@ -63,7 +63,7 @@ func TestList(t *testing.T) { }) assert.Assert(names == "fellohellojellonello") assert.Assert(datas == "123456789012") - assert.Assert(nums == 1380) + assert.Assert(nums == 1368) names = "" datas = ""