-
Notifications
You must be signed in to change notification settings - Fork 7
Adds the scaffold for serialization and de-serialization #11
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
Adds the scaffold for serialization and de-serialization #11
Conversation
izo0x90
commented
Feb 6, 2025
- Trait to define custom handler payloads
- Handler object to process requests and responses
- BaseRequest and JSONType (only placeholder for json)
- Some placeholder toy JSON de-serialization until. deps. are decided on
- Trait to define custom handler payloads - Handler object to process requests and responses - BaseRequest and JSONType (only placeholder for json) - Some placeholder toy JSON de-serialization until. deps. are decided on
lightbug.🔥
Outdated
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.
We'll probably add convenience methods on BaseRequest
like uri()
that would return request.uri.path
, just so that the user doesn't have to write the whole req.request.uri.path
etc.
Also, request
on BaseRequest
should probably be renamed to _inner
or similar
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.
It would have been nice if we had struct inheritance so people could extend BaseRequest
struct and automatically get all the convenience methods on their custom struct for free.
But on this post in the forum from Dec Chris says they don't plan to add this feature in Mojo. I wonder if we can work around this limitation somehow
|
||
|
||
trait FromReq(Movable, Copyable): | ||
fn __init__(out self, request: HTTPRequest, json: JSONType): |
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.
this we can prob omit from the trait as not all will have json payloads. either that, or the payload can just be passed inside request
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.
So the reason that is required on the trait is that since we don't have struct inheritance or reflection we have to have the user deal with deconstructing the data on custom payloads so we want to make sure their type is able to handle this ... we could make them add a method that tells us if we they need json had_json
or something like that but I think maybe just defaulting this to an empty json object when no json is needed might be easier... more ergonomic ?
fn from_request(mut self, req: HTTPRequest) raises -> Self: | ||
... | ||
|
||
fn __str__(self) -> String: |
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.
is this a required method?
|
||
return ser(json) | ||
|
||
fn _deserialize_json(self, req: HTTPRequest) raises -> JSONType: |
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.
this could either call emberJSON .parse()
and then user gets a JSON
object or a custom implementation the user provides to return a struct
raise Error("Unsupported response type") | ||
|
||
fn _serialize_json(self, json: JSONType) raises -> String: | ||
# TODO: Placeholder json serialize implementation |
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.
this then calls either EmberJSON .to_string()
if the user has a JSON
object here, or user provides a custom implementation if they want to serialize their custom struct
Going to merge this now! And we can address the comments separately in dedicated PRs |