-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(client): parse Presto JSON response with custom reviver and BigInts #25
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR and the changes look good to me.
Just left a minor question.
Also, update sample nestjs app to include custom JSON.stringify replacer
I refactored the code to do the compat check in the same function by checking if the context (and context.source) values are non falsy. The only weird thing that happens is if you try to stringify the query response since now it contains a BigInt which is not serializable by the default JSON.stringify function. You need to write a custom replacer like this: const results = await client.query(`SELECT 1234567890123456623`)
return {
result: JSON.stringify(results.data, (key, value) => {
if (typeof value !== 'bigint') return value
return value.toString()
}),
} Otherwise you may end up with an error like this:
We could either:
Any advice @yhwang ? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some comments below
@alonsovb per your question. My 2 cents: However, this requires API changes. We should document the limitation like what you did now and create an issue to track this and discuss the proper solution there. Your option 2 is also doable and no API change. |
Okay, just finished adding some initial unit tests. Let me know if anything else may be needed cc @yhwang @jorgeramirezamora Per @yhwang , I created this issue to discuss more about a potential API change for the Client's response object: Thanks everyone! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👏🏼
@@ -0,0 +1,38 @@ | |||
import { parseWithBigInts } from './utils' | |||
|
|||
describe('parse big ints', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❤️
* @param context Context with source text | ||
* @returns Parsed object with BigInts where required | ||
*/ | ||
export function parseWithBigInts(_: string, value: unknown, context: { source: string } | undefined) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤯
Fixes #24. For now, this only assumes the JSON.parse includes the context.source in the reviver callback. This is currently a stage 3 proposal but has very good browser support.
Changes
Uses a custom JSON.parse reviver for numbers so that everything uses the BigInt primitive instead of the regular Number which loses precision on bigger values.
Notes
None
Screenshots
None