From 5ae2c21ecab5261fa694207d3d61b056b552ef25 Mon Sep 17 00:00:00 2001 From: Jonas Bushart Date: Sat, 23 Mar 2019 19:29:35 +0100 Subject: [PATCH] Rename macro to skip_serializing_none --- CHANGELOG.md | 2 +- serde_with_macros/src/lib.rs | 18 +++++++++--------- .../tests/skip_serializing_null.rs | 14 +++++++------- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 14b6127d..96b0dcc9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ### Added -* Add `skip_serializing_null` attribute, which adds `#[serde(skip_serializing_if = "Option::is_none")]` for each Option in a struct. +* Add `skip_serializing_none` attribute, which adds `#[serde(skip_serializing_if = "Option::is_none")]` for each Option in a struct. This is helpfull for APIs which have many optional fields. The effect of can be negated by adding `serialize_always` on those fields, which should always be serialized. Existing `skip_serializing_if` will never be modified and those fields keep their behavior. diff --git a/serde_with_macros/src/lib.rs b/serde_with_macros/src/lib.rs index 73b920a5..05b6ecf8 100644 --- a/serde_with_macros/src/lib.rs +++ b/serde_with_macros/src/lib.rs @@ -25,8 +25,8 @@ use syn::{ }; #[proc_macro_attribute] -pub fn skip_serializing_null(_args: TokenStream, input: TokenStream) -> TokenStream { - let res = match skip_serializing_null_do(input) { +pub fn skip_serializing_none(_args: TokenStream, input: TokenStream) -> TokenStream { + let res = match skip_serializing_none_do(input) { Ok(res) => res, Err(msg) => { let span = Span::call_site(); @@ -36,17 +36,17 @@ pub fn skip_serializing_null(_args: TokenStream, input: TokenStream) -> TokenStr TokenStream::from(res) } -fn skip_serializing_null_do(input: TokenStream) -> Result { +fn skip_serializing_none_do(input: TokenStream) -> Result { // For each field in the struct given by `input`, add the `skip_serializing_if` attribute, // if and only if, it is of type `Option` if let Ok(mut input) = syn::parse::(input.clone()) { - skip_serializing_null_handle_fields(&mut input.fields)?; + skip_serializing_none_handle_fields(&mut input.fields)?; Ok(quote!(#input)) } else if let Ok(mut input) = syn::parse::(input.clone()) { input .variants .iter_mut() - .map(|variant| skip_serializing_null_handle_fields(&mut variant.fields)) + .map(|variant| skip_serializing_none_handle_fields(&mut variant.fields)) .collect::>()?; Ok(quote!(#input)) } else { @@ -108,7 +108,7 @@ fn field_has_attribute(field: &Field, namespace: &str, name: &str) -> bool { } /// Add the skip_serializing_if annotation to each field of the struct -fn skip_serializing_null_add_attr_to_field<'a>( +fn skip_serializing_none_add_attr_to_field<'a>( fields: impl IntoIterator, ) -> Result<(), String> { fields.into_iter().map(|field| ->Result<(), String> { @@ -166,15 +166,15 @@ fn skip_serializing_null_add_attr_to_field<'a>( } /// Handle a single struct or a single enum variant -fn skip_serializing_null_handle_fields(fields: &mut Fields) -> Result<(), String> { +fn skip_serializing_none_handle_fields(fields: &mut Fields) -> Result<(), String> { match fields { // simple, no fields, do nothing Fields::Unit => Ok(()), Fields::Named(ref mut fields) => { - skip_serializing_null_add_attr_to_field(fields.named.iter_mut()) + skip_serializing_none_add_attr_to_field(fields.named.iter_mut()) } Fields::Unnamed(ref mut fields) => { - skip_serializing_null_add_attr_to_field(fields.unnamed.iter_mut()) + skip_serializing_none_add_attr_to_field(fields.unnamed.iter_mut()) } } } diff --git a/serde_with_macros/tests/skip_serializing_null.rs b/serde_with_macros/tests/skip_serializing_null.rs index 7fe1595c..7fb43bc9 100644 --- a/serde_with_macros/tests/skip_serializing_null.rs +++ b/serde_with_macros/tests/skip_serializing_null.rs @@ -6,7 +6,7 @@ extern crate serde_with_macros; use pretty_assertions::assert_eq; use serde::{Deserialize, Serialize}; use serde_json::json; -use serde_with_macros::skip_serializing_null; +use serde_with_macros::skip_serializing_none; macro_rules! test { ($fn:ident, $struct:ident) => { @@ -38,7 +38,7 @@ macro_rules! test_tuple { }; } -#[skip_serializing_null] +#[skip_serializing_none] #[derive(Debug, Eq, PartialEq, Serialize, Deserialize)] struct DataBasic { a: Option, @@ -48,7 +48,7 @@ struct DataBasic { } test!(test_basic, DataBasic); -#[skip_serializing_null] +#[skip_serializing_none] #[derive(Debug, Eq, PartialEq, Serialize, Deserialize)] struct DataFullyQualified { a: ::std::option::Option, @@ -62,7 +62,7 @@ fn never(_t: &T) -> bool { false } -#[skip_serializing_null] +#[skip_serializing_none] #[derive(Debug, Eq, PartialEq, Serialize, Deserialize)] struct DataExistingAnnotation { #[serde(skip_serializing_if = "Option::is_none")] @@ -90,7 +90,7 @@ fn test_existing_annotation() { assert_eq!(data, serde_json::from_value(res).unwrap()); } -#[skip_serializing_null] +#[skip_serializing_none] #[derive(Debug, Eq, PartialEq, Serialize, Deserialize)] struct DataSerializeAlways { #[serialize_always] @@ -121,12 +121,12 @@ fn test_serialize_always() { assert_eq!(data, serde_json::from_value(res).unwrap()); } -#[skip_serializing_null] +#[skip_serializing_none] #[derive(Debug, Eq, PartialEq, Serialize)] struct DataTuple(Option, std::option::Option); test_tuple!(test_tuple, DataTuple); -#[skip_serializing_null] +#[skip_serializing_none] #[derive(Debug, Eq, PartialEq, Serialize)] enum DataEnum { Tuple(Option, std::option::Option),