DiceDB / dice

DiceDB is a redis-compliant, reactive, scalable, highly-available, unified cache optimized for modern hardware.
https://dicedb.io/
Other
6.83k stars 1.08k forks source link

Fixing #1163 : Inconsistent Behaviour in http server : Big Integers are getting converted to float before storing thus losing their precision #1227

Open deydebaditya opened 3 weeks ago

deydebaditya commented 3 weeks ago

Change description

When working with map[string]interface{} in Go,json.Marshal will automatically infer the data type for each element based on the interface{} type that we use. However, in our case, json.Marshal is casting values incorrectly (e.g., casting integer values to float64), it may be due to Go’s internal handling of untyped constants and generic types within interface{}

To fix this, I've added an explicit check during unmarshalling of the JSON body in the request payload for int64 types. I've also added a recursive unmarshalling to check for any such big integer values in nested JSON fields as well.

To ensure that json.Marshal preserves the original data types, I've enforced specific types in our map by using json.Number for numeric data types before marshaling again.

Lastly, instead of using a vanilla json.Unmarshal() at the end of the request map creation, the JSON decoder needs to be aware that it explicitly needs to use numbers, instead of using Go's default handling of the interface{} type.

Working

Request

image

Data set in DICE DB

image

Response from HTTP server

image

AshwinKul28 commented 1 week ago

Hey @deydebaditya can we work onto closing this PR? Just a one linter change is required as discussed above and we are good to go! Thanks

deydebaditya commented 1 week ago

Will work on it by tomorrow. Would need to change a bit if implementation in the method body as well, since I’m serializing the json bytes back into the map reference at the end. Like I said before, maps are reference types as in the values to map keys are references definitely, but the actual map object would still be passed by value in this method. Will make the relevant changes.

On Tue, 12 Nov 2024 at 9:17 PM, Ashwin Kulkarni @.***> wrote:

Hey @deydebaditya https://github.com/deydebaditya can we work onto closing this PR? Just a one linter change is required as discussed above and we are good to go! Thanks

— Reply to this email directly, view it on GitHub https://github.com/DiceDB/dice/pull/1227#issuecomment-2470887049, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACX7SWUKY425P3USK5XH33D2AIPHZAVCNFSM6AAAAABQ7TMNSKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDINZQHA4DOMBUHE . You are receiving this because you were mentioned.Message ID: @.***>