-
Notifications
You must be signed in to change notification settings - Fork 57
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(libwaku): support string and int64 for timestamps #3205
Conversation
You can find the image built from this PR at
Built from c459f4e |
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.
LGTM!
Maybe we could generalize this approach for easy reuse.
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.
LGTM! Thank you!
Would something like this work? import std/[json, strutils]
import results
import ../../../waku/waku_core/time
proc getProtoTimestamp*[T](node: JsonNode, key: string): Opt[T] =
let (value, ok) =
if node.hasKey(key):
if node[key].kind == JString:
(parseInt(node[key].getStr()), true)
else:
(node[key].getBiggestInt(), true)
if not (T is Timestamp or T is int or T is int64):
raise newException(ValueError, "Unsupported type")
return
if not ok:
return T.none()
else:
when T is Timestamp:
some(Timestamp(value))
else:
some(value) |
Yeah, kind of like that! |
@NagyZoltanPeter check the latest commit! |
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.
Awesome! Thanks so much for it! 🙌
library/utils.nim
Outdated
import results | ||
import ../waku/waku_core/time | ||
|
||
proc getProtoInt64*(node: JsonNode, key: string): Option[int64] = |
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.
I wonder if we should return a Result
instead so that we can better control any possible error
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.
I changed it to a result now. Please do check latest commit and let me know your thoughts!
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.
I like it! :-)
The Protocol Buffers JSON Specification for 64 bit numbers, recommends serializing numbers as strings to avoid loss of precision when working with 64-bit integers in JavaScript environments. This causes issues as some json serializers like Go's respect the specification when serializing a protobuffer into Json.
To avoid this issue, in this PR will allow accepting both integers and numeric strings for those cases in which this spec is not followed