From d476fa510b4bc8fdb19f507a3d721e897f637a8e Mon Sep 17 00:00:00 2001 From: Daniel Stinson-Diess Date: Mon, 2 Dec 2024 09:36:05 -0600 Subject: [PATCH] fix(message): use correct encodings for JSON Number values (#281) --- message/message.go | 11 +++++++++++ transform/object_copy_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/message/message.go b/message/message.go index cc4d9d56..a3e4c746 100644 --- a/message/message.go +++ b/message/message.go @@ -5,6 +5,7 @@ import ( "bytes" "encoding/json" "fmt" + "math" "strings" "unicode/utf8" @@ -366,6 +367,16 @@ func setValue(json []byte, key string, value interface{}) ([]byte, error) { return sjson.SetBytes(json, key, base64.Encode(v)) } case Value: + // JSON number values can lose precision if not read with the right encoding. + // Determine if the value is an integer by checking if floating poit truncation has no + // affect of the value. + if v.gjson.Type == gjson.Number { + if v.Float() == math.Trunc(v.Float()) { + return sjson.SetBytes(json, key, v.Int()) + } + return sjson.SetBytes(json, key, v.Float()) + } + return sjson.SetBytes(json, key, v.Value()) default: return sjson.SetBytes(json, key, v) diff --git a/transform/object_copy_test.go b/transform/object_copy_test.go index d979b804..3be75a2f 100644 --- a/transform/object_copy_test.go +++ b/transform/object_copy_test.go @@ -32,6 +32,36 @@ var objectCopyTests = []struct { []byte(`{"a":"b","c":"b"}`), }, }, + { + "large integer", + config.Config{ + Settings: map[string]interface{}{ + "object": map[string]interface{}{ + "source_key": "a", + "target_key": "b", + }, + }, + }, + []byte(`{"a":30400402455622563}`), + [][]byte{ + []byte(`{"a":30400402455622563,"b":30400402455622563}`), + }, + }, + { + "large float", + config.Config{ + Settings: map[string]interface{}{ + "object": map[string]interface{}{ + "source_key": "a", + "target_key": "b", + }, + }, + }, + []byte(`{"a":3.141592653589793}`), + [][]byte{ + []byte(`{"a":3.141592653589793,"b":3.141592653589793}`), + }, + }, { "unescape object", config.Config{