-
-
Notifications
You must be signed in to change notification settings - Fork 219
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
serializing nested struct with #[serde(flatten)] is not supported #239
Comments
OK, so this isn't a panic. The CSV writer is returning an error, and you're unwrapping it, and that is what is causing the panic. I've changed the title to update this. A panic inside the CSV writer itself is a serious bug. But it returning an error is totally normal when you do something that it doesn't support.
The alternative is to write your own Serde impls or change your type definitions to be structured in a way that you want.
The error message is pretty clear I think: "serializing maps is not supported, if you have a use case." It does ask you to open an issue. And there are many other issues related to |
@tristanCadet There is an experimental branch with support for that! I've been using it for a few months already and haven't had any issues. It's tracked over here: #223.
I'd be great if you could try it too. If you have any issues, please report - that would really help. |
Hi, I just started using this library today and I ran into this issue having created struct references using |
Nope. |
I also just run into this, trying to avoid the limitation with writing headers of a struct containing an array by flattening the struct. It would be great for this to be fixed. |
This comment was marked as off-topic.
This comment was marked as off-topic.
Hi! I happen to have a use case that I didn't see mentioned anywhere until now: I need to be able to serialize as extra CSV columns a generic struct provided by my crate's user (order of those columns doesn't matter in my case). Expected output with the following example code:
Here's an example code of what would be ideal (csv version 1.1.6): use serde::Serialize;
fn main() {
let mut w = csv::Writer::from_writer(std::io::stdout());
w.serialize(Data::<CustomColumns>::default()).unwrap();
}
#[derive(Debug, Serialize, Default)]
struct Data<Extra: Serialize + Default> {
a: usize,
b: String,
#[serde(flatten)]
extra: Extra,
}
#[derive(Debug, Serialize, Default)]
struct CustomColumns {
custom: String,
} Which, naturally, fails with:
If I don't try to use
Instead, I'm forced to workaround this situation by using a tuple struct + an intermediate inner struct for the consistent fields. This has a negative impact on ergonomics. use serde::Serialize;
fn main() {
let mut w = csv::Writer::from_writer(std::io::stdout());
w.serialize(Data::<CustomColumns>::default()).unwrap();
}
#[derive(Debug, Serialize, Default)]
struct Data<Extra: Serialize + Default>(DataInner, Extra);
#[derive(Debug, Serialize, Default)]
struct DataInner {
a: usize,
b: String,
}
#[derive(Debug, Serialize, Default)]
struct CustomColumns {
custom: String,
} |
When you say "negative impact on ergonomics," I think that applies to your internal code and not the public API you present to your callers, right? |
Those required workarounds impact negatively the ergonomics of both the You're right about it not being a matter of public interface of my crate here, since I can still expose to the user only the tailored struct I want, and then go through all the intermediate representations I need in the internal code. It's starting to be a lot of boilerplate, though:
I won't go over the reasons why one would want to reduce boilerplate in a codebase, but the support for Going forward
If you're ok with that, Stockly can look at it before the end of the year 😄 |
Aye okay, understood. Basically what it comes down to here is that I think the thing required here is not just a simple matter of doing the work to implement this, but to do a very thorough investigation of how it would work and what kinds of use cases it would support. In theory, your I do appreciate the offer though. |
Hi @BurntSushi, I feel like Stockly can provide solutions to the pain points highlighted. We could:
Would that work for you? Thanks a have a nice day! |
If I had the bandwidth to engage with that kind of contribution, I would love it. But as of right now, the most likely outcome is that y'all post an RFC and I'm not able to meaningfully digest it for quite some time. You can still go forward with it, but I just want to be very clear that my bandwidth is extremely low at present. Most of my focus is on the regex crate, and I don't anticipate that changing in the next few months. More to the point, there are several higher priority things that the So I don't want to say "no I don't want your contribution," but I also don't want to say "yes and I will be very responsive to all your work and get everything merge and be an amazing maintainer." Does that make sense? |
Hi @BurntSushi Perfectly clear thanks 😊 We'll move on with the RFC then and no pressure to review it. As a side question, what are the higher priority things for the Thanks a have a nice day, |
CI needs to be fixed. I also need to decide how to straighten out the existing flatten feature for deserialization. And fix the dependency situation as there are a few that need to be updated. There's probably more. |
Would make sense if |
I've just hit this issue trying to deserialize a nested flattened struct, I really appreciate the related PR. Is there any chance to see a viable solution integrated into this crate? If not, is there a usable fork or alternative supporting this feature in a reliable way? |
@izolyomi There's a chance, sure. Multiple work arounds have been discussed above. You may not like them though. |
What version of the
csv
crate are you using?version = "1.1.6"
Briefly describe the question, bug or feature request.
The program panics when serializing a struct that contains a nested struct decorated by #[serde(flatten)].
I am not sure whether this is a bug or expected behavior but I would expect the program to output a csv.
If it is expected are there any alternatives to obtain the desired output (without removing nesting) ?
Include a complete program demonstrating a problem.
What is the observed behavior of the code above?
What is the expected or desired behavior of the code above?
The text was updated successfully, but these errors were encountered: