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

Support serde flatten for Typescript #115

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

Roms1383
Copy link

@Roms1383 Roms1383 commented May 7, 2023

This PR concretizes #114.
I haven't added tests, yet.

@Roms1383
Copy link
Author

Roms1383 commented May 7, 2023

So, before I figure out how tests are setup,
here's what it does:

#[derive(Debug, Deserialize, Serialize)]
#[typeshare]
pub struct Foo {
    pub value: String,
}

#[derive(Debug, Deserialize, Serialize)]
#[typeshare]
pub struct Bar {
    pub value: String,
}

#[derive(Debug, Deserialize, Serialize)]
#[typeshare]
pub struct Dummy {
    /// generates a normal nested definition
    pub foo: Foo,
    #[serde(flatten)]
    /// generates a flattened definition
    pub bar: Bar,
}

then

typeshare . --lang=typescript --output-file=definitions.ts`

becomes

export interface Foo {
	value: string;
}

export interface Bar {
	value: string;
}

export interface Dummy extends Bar {
	/** generates a normal nested definition */
	foo: Foo;
}

It also does not affect the other languages' implementations for Language,
but the presence of #[serde(flatten)] does produce an expected panic! on e.g.

typeshare . --lang=swift --output-file=definitions.swift

A workaround could be to allow the consumer to define a blank feature flag,
and maybe #[cfg_attr] can be parsed to allow basically to ignore it on languages which does not (yet or never) support it, e.g.:

#[derive(Debug, Deserialize, Serialize)]
#[typeshare]
pub struct Dummy {
    /// generates a normal nested definition
    pub foo: Foo,
    #[cfg_attr(feature = "typescript", serde(flatten)])] // <--- this line
    /// generates a flattened definition
    pub bar: Bar,
}

But yes ultimately it's not super ergonomic, so looking for your feedback :)

@Lucretiel
Copy link
Contributor

Thanks for the PR! I'll give it a review next week

@Roms1383
Copy link
Author

cc @Lucretiel :)

@snowsignal snowsignal requested a review from Lucretiel May 23, 2023 16:31
@Lucretiel
Copy link
Contributor

Sorry for the delay, reviewing now. really like the idea of using inheritance to solve this, but I'm concerned that this solution won't have an equivalent in languages that don't have inheritance (like Go), or that do but for which it's an unusual pattern (like Swift). I'll give it a review as written but I'll also do some discussion with the typeshare team at our next team meeting.

@@ -78,7 +78,7 @@ pub enum ParseError {
}

/// Parse the given Rust source string into `ParsedData`.
pub fn parse(input: &str) -> Result<ParsedData, ParseError> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

supports_flatten shouldn't be something the parser needs to worry about; it should unconditionally emit the fully parsed data (including #[serde(flatten)] attributes), and then that parsed data can be rejected by individual languages when the output is generated, if necessary.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds fair, but concretely just wondering how you would see it implemented.

@Roms1383
Copy link
Author

Alright! I understand your concern, please keep me posted in case I miss the discussion ! 👌

@Lucretiel Lucretiel added the improvement Improvement to an already-existing feature. label May 25, 2023
@Lucretiel Lucretiel assigned Lucretiel and Roms1383 and unassigned Lucretiel May 25, 2023
@Lucretiel Lucretiel linked an issue May 25, 2023 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
improvement Improvement to an already-existing feature.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Suggestion for #[serde(flatten)]
2 participants