Wulf / tsync

Synchronize rust and typescript types!
Other
116 stars 10 forks source link

[feature request] Support serde flatten #48

Closed rlcurrall closed 5 months ago

rlcurrall commented 9 months ago

Currently the serde flatten attribute is not supported. It would be nice to have this code:

#[tsync]
#[derive(Serialize)]
struct Outter {
    id: String,
    #[serde(flatten)]
    inner: Inner,
}

#[tsync]
#[derive(Serialize)]
struct Inner {
    value: String,
}

generate something like this:

type Outter = Inner & {
  id: string;
}

interface Inner {
  value: string;
}

Also, enum support would be ideal. I'm open to other options for how the outputted TS would look, I'm only suggesting the intersection type because it would be easier for it to work with enums since they are usually defined with the type keyword.

Wulf commented 8 months ago

Hey @rlcurrall, thanks for this :)

Does the TS output resemble how serde serializes the object exactly?

rlcurrall commented 8 months ago

@Wulf, sorry for the delay in response!

I've created some examples to show what the functionality is, here's an example with an nested struct being flattened:

I think nested enums are a bit more interesting when flattened as you can see here:

One thing to note is that serde allows for you to flatten a record that has overlapping keys as you can see here. Not sure if we should, or even care to handle this case differently than serde, but something to note.

Wulf commented 5 months ago

hey @rlcurrall really sorry to have left this hanging.

Thanks for the examples -- they helped clarify the functionality.

:rocket: I'll merge in your PR and cut a release :)