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

Add flatten feature #81

Open
poelzi opened this issue Mar 21, 2023 · 2 comments
Open

Add flatten feature #81

poelzi opened this issue Mar 21, 2023 · 2 comments

Comments

@poelzi
Copy link

poelzi commented Mar 21, 2023

It would be nice there is a builder(flatten) to merge 2 structs into one builder.

#[derive(TypedBuilder)]
struct A {
  a: String,
}
#[derive(TypedBuilder)]
struct A {
  #[builder(flatten)]
  shared: A,
  more: bool,
}

let a1 = A::builder().a("test".into()).more(true).build();

This should create a builder that will construct and fill the shared structure.
Similar to serde(flatten).
This is a common used data structure and would simplify usage.

@idanarye
Copy link
Owner

Sadly, I don't think this is possible with Rust's macro system. The derive macro on the outer type has no access to the structure of the inner types, and thus cannot generate withers for their fields.

@mo8it
Copy link
Contributor

mo8it commented Jun 4, 2023

You would need to provide the fields of the struct to flatten, in this example: #[builder(flatten({a: String}))]. This could be automated with a macro that wraps both types, but it will get complicated and ugly...

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

3 participants