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

Added implementation of #[field(flatten)]. #2225

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

azewierzejew
Copy link

@azewierzejew azewierzejew commented Jun 7, 2022

Limited to on flattened struct.

Implementation of #1916

Limited to on flattend struct
@azewierzejew
Copy link
Author

I'm proposing here an implementation (without error handling) of the flatten attribute in FromForm derive macro.
Before finishing it I would like to discuss the semantics of this trait.

With current FromForm trait interface, there is no way which flattened sub-struct uses given field.
One solution for that would be to pass the field to all flattened structs.
But that doesn't really work either because DataField can't be copied.
Also that would produce different results than serde for names duplicated by flattening, e.g:

#[derive(FromForm)]
struct X {
    name: String,
}
#[derive(FromForm)]
struct Y {
    #[field(flatten)]
    inner1: X,
    #[field(flatten)]
    inner2: X,
}

In above case serde initializes inner1.name with provided value and inner2.name with default value (or gives an Error).

Therefore I believe restricting flattening to only one field is the only way without serious API changes.

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.

None yet

1 participant