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

feat: implement support for serde flatten #49

Merged
merged 1 commit into from
May 12, 2024

Conversation

rlcurrall
Copy link
Contributor

@rlcurrall rlcurrall commented Jan 19, 2024

Addresses #48

Will convert these Rust types:

#[tsync]
#[derive(Serialize)]
/// Struct with flattened field.
struct Author {
    name: String,
    #[serde(flatten)]
    name: AuthorName,
}

#[tsync]
#[derive(Serialize)]
struct AuthorName {
    alias: Option<String>,
    first_name: String,
    last_name: String,
}

To these TypeScript types:

/** Struct with flattened field. */
export type Author = AuthorName & {
  name: string;
}

export interface AuthorName {
  alias?: string;
  first_name: string;
  last_name: string;
}

It also supports enums, so the following Rust types:

/// Doc comments are preserved too!
#[tsync]
struct Book {
    /// Name of the book.
    name: String,
    /// Chapters of the book.
    chapters: Vec<Chapter>,
    /// Reviews of the book
    /// by users.
    user_reviews: Option<Vec<String>>,
    #[serde(flatten)]
    book_type: BookType,
}

#[tsync]
#[derive(Serialize)]
#[serde(tag = "type")]
enum BookType {
    Fiction { genre: String },
    NonFiction { subject: String },
}

Will generate these TypeScript types:

/** Doc comments are preserved too! */
export type Book = BookType & {
  /** Name of the book. */
  name: string;
  /** Chapters of the book. */
  chapters: Array<Chapter>;
  /**
   * Reviews of the book
   * by users.
   */
  user_reviews?: Array<string>;
}

export type BookType =
  | BookType__Fiction
  | BookType__NonFiction;

type BookType__Fiction = {
  type: "Fiction";
  genre: string;
};
type BookType__NonFiction = {
  type: "NonFiction";
  subject: string;
};

@Wulf Wulf merged commit 2141be7 into Wulf:main May 12, 2024
@rlcurrall rlcurrall deleted the feat/implement-support-for-serde-flatten branch May 12, 2024 15:42
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