Skip to content

Conversation

izo0x90
Copy link
Contributor

@izo0x90 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
Copy link
Collaborator

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

Copy link
Collaborator

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 BaseRequeststruct 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):
Copy link
Collaborator

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

Copy link
Contributor Author

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:
Copy link
Collaborator

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:
Copy link
Collaborator

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
Copy link
Collaborator

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

@saviorand
Copy link
Collaborator

Going to merge this now! And we can address the comments separately in dedicated PRs

@saviorand saviorand merged commit 1c67ca5 into Lightbug-HQ:main Feb 6, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants