Skip to content
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

Separate schema from format #245

Merged
merged 10 commits into from
Aug 20, 2024
Merged

Separate schema from format #245

merged 10 commits into from
Aug 20, 2024

Conversation

kanarus
Copy link
Member

@kanarus kanarus commented Aug 18, 2024

closes #241


Before ohkami provided typed::{Query, Payload} for handling query parameters and payloads like

#[Payload(JSON/D)]
struct Hello<'req> {
    name: &'req str
}

#[Query]
struct HelloQuery {
    repeat: Option<usize>
}

but this enforces integrating format and schema, which is not modular or healthy.


For example, think about a case of maintaining a handler that takes a Hello<'_> as argument, and oneday you have to change the payload format from json into x-www-form-urlemcoded. In this situation with current Ohkami, you have to change

#[Payload(JSON/D)]
struct Hello<'req> ...

into

#[Payload(URLEncoded/D)]
struct Hello<'req> ...

this means you need to touch the struct definition, while the schema of payload is not changed.

Additionally, the handler definition

async fn handler(req: Hello<'_>, ...) -> ...

doesn't show clearly how it takes the data of type Hello<'_> ( and you have to go to the definition of Hello to know it ).


Now I think the responsibility of format ( query, json, urlencoded, ... ) should not owe to a struct in charge of the schema.

This PR removes typed::{Payload, Query} and introduces format module to separate schema from format:

#[derive(Deserialize)]
struct Hello<'req> {
    name: &'req str
}

#[derive(Deserialize)]
struct HelloMetadata {
    repeat: Option<usize>
}
use ohkami::format::{Query, JSON};

async fn handler(
    JSON(req): JSON<Hello<'req>>,
    Query(q): Query<HelloMetadata>,
    ...
) -> ...

github-actions[bot]
github-actions bot previously approved these changes Aug 18, 2024
@kanarus
Copy link
Member Author

kanarus commented Aug 18, 2024

CI failed as I expected.
We have to wait cloudflare/workers-rs#619 to be merged and shipped.

@kanarus kanarus merged commit 29856cd into main Aug 20, 2024
3 checks passed
@kanarus kanarus deleted the typed/separate_schema_from_format branch August 20, 2024 06:57
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.

Separate schema from format in typed system
1 participant