-
Notifications
You must be signed in to change notification settings - Fork 34
Open
Description
I have a struct that uses the attribute #[serde(flatten)] to share fields between multiple structs from an API.
For example, I have this:
#[derive(Deserialize, Getter)]
#[get = "pub"]
struct PartialChapterData {
id: u64,
language: String,
}
#[derive(Deserialize, Getters)
#[get = "pub"]
struct UserChapters {
user_id: u64,
created: u64, // Unix timestamp
chapters: Vec<PartialChapterData>,
}
#[derive(Deserialize, Getters)
#[get = "pub"]
struct DetailedChapter {
#[serde(flatten)]
chapter_partial: PartialChapterData,
volume: u16,
title: String,
}
fn main() {
let test_json = r#"{
"id": 1,
"language": "English",
"volume": 1,
"title": "The Chapter Title"
}"#;
let detailed_chapter: DetailedChapter = serde_json::from_str(test_json)?;
println!(
"Title: {}\nID: {}",
detailed_chapter.title(), // Does not panic
detailed_chapter.id(), // Panics with the error: error[E0599]: no method named `id` found for reference `&my_test_crate::v2::responses::chapter::DetailedChapter` in the current scope
);
}Is there a correct method or workaround for this? I haven't tested the Setter trait as I don't have a need for it.
Edit 1:
My workaround for now is to manually implement the setters in DetailedChapter like so:
// ...
#[derive(Deserialize, Getter)]
#[get = "pub"]
struct PartialChapterData {
id: u64,
language: String,
}
#[derive(Deserialize, Getters)
#[get = "pub"]
struct DetailedChapter {
#[serde(flatten)]
chapter_partial: PartialChapterData,
volume: u16,
title: String,
}
impl DetailedChapter {
pub fn id(&self) -> &u64 {
&self.chapter_partial.id()
}
pub fn language(&self) -> &String {
&self.chapter_partial.language()
}
}The other getters .volume() and .title() are generated from getset without needing to re-implement them.
Metadata
Metadata
Assignees
Labels
No labels