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

Custom type mapping #34

Open
thebluefish opened this issue May 11, 2023 · 1 comment
Open

Custom type mapping #34

thebluefish opened this issue May 11, 2023 · 1 comment

Comments

@thebluefish
Copy link
Contributor

thebluefish commented May 11, 2023

I would like to support rust_decimal's Decimal type. Depending on configuration, this type can be represented in serde as either a float or a string. This is further complicated by options covering the whole crate or just specific fields. While generating TypeScript types via tsync, I would like to somehow specify which representation to use.

Currently I can create a separate module exclusively for inclusion in tsync and export custom type mappings there, however I would prefer to erase the type entirely.

Alternatively, I can fork the project and and add the additional type to convert_type directly as a global type mapping. This produces results closer to what I want, but requires more work to maintain.

Neither of these solutions would help the per-field case.

I am wondering about adding a field parameter to override types in-line, something like:

#[derive(Debug, Serialize, Deserialize)]
#[tsync]
pub struct Foo {
    name: Option<String>,
    #[tsync(type = "number")]
    quantity: Decimal,
}
@Wulf
Copy link
Owner

Wulf commented Aug 7, 2023

Yeah, we definitely need something like this. You've probably noticed the hard-coded types for something like chrono's NaiveDate/Time types... it isn't an ideal solution, but it was a hack that got us this far 😅 .

I would love to see something like added to tsync. I really appreciate your detailed explanation of the problem and approaches you've thought of.

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

No branches or pull requests

2 participants