default to not serializing None options

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
Wolfgang Bumiller 2019-07-19 13:22:18 +02:00
parent d9f57ed107
commit cb2c260f87
2 changed files with 11 additions and 1 deletions

View File

@ -527,7 +527,9 @@ fn handle_struct_named(
field_ident_list.extend(quote_spanned! { field_span => #field_ident, });
serialize_entries.extend(quote_spanned! { field_span =>
state.serialize_field(#field_str, &self.#field_ident)?;
if !::proxmox::api::ApiType::should_skip_serialization(&self.#field_ident) {
state.serialize_field(#field_str, &self.#field_ident)?;
}
});
field_option_init_list.extend(quote_spanned! { field_span =>

View File

@ -185,6 +185,10 @@ pub trait ApiType {
fn get_type_info(&self) -> &'static TypeInfo {
Self::type_info()
}
fn should_skip_serialization(&self) -> bool {
false
}
}
/// Option types are supposed to wrap their underlying types with an `optional:` text in their
@ -234,6 +238,10 @@ impl<T: ApiType> ApiType for Option<T> {
unsafe { (*DATA.info.as_ptr()).as_ref().unwrap() }
*/
}
fn should_skip_serialization(&self) -> bool {
self.is_none()
}
}
/// Any `Result<T, Error>` of course gets the same info as `T`, since this only means that it can