-
Notifications
You must be signed in to change notification settings - Fork 212
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 for writing a header for nested structs #197
base: master
Are you sure you want to change the base?
support for writing a header for nested structs #197
Conversation
@fmorency, @Kampfkarren, could you please take a look if this covers your use cases? |
In particular, take a look at this test: Line 1185 in a2f7a8d
|
@BurntSushi could you please take a look? |
I've tried to see if this works with my use case, which is nested structs and wishing to write the headers for them, and it didn't. |
The patch works for my simple use case, two nested structs. It would help me if the PR got merged. |
Would help here also for flattening stuff which I don't want to patch in one big struct. |
This branch works fine for me for serializing nested structs. Thanks a lot @ilya-epifanov! |
Thank you so much @ilya-epifanov. This however fails when serializing nested structs wrapped in enums. use serde::Serialize;
#[derive(Serialize)]
struct Row {
nested_one: Option<Nested>,
nested_two: Nested,
}
#[derive(Serialize)]
struct Nested { x: i32, y: i32 }
fn main() {
let mut writer = csv::Writer::from_writer(std::io::stdout());
let row = Row { nested_one: Some(Nested { x: 1, y: 2 }), nested_two: Nested { x: 3, y: 4}};
writer.serialize(row).unwrap();
} This panics with
|
Ok, @BurntSushi, this is what I got. SummaryThere is 2 main step in writing the CSV: - Writing the header and - Writing the values. Currently the values are written properly even when they are nested in a struct. On the other hand, the header for a That's the problem this PR tries to solve and that is why this PR mainly modifies the behaviour of How it is implementedBasically, this PR hack the In order to check the type, the PR has set a new field for the Therefore, if the ProblemThis however does not work when the The underlying problem is that the hack does not work this time as Now we could try to patch A second problem I faced is that we cannot use the I think it will be better to redo this PR for nesting by implementing the |
This covers some of the #98 and #155 use cases.
It makes
#[serde(flatten)]
unnecessary for nested structs, as CSV is a flat format anyway.