Skip to content

Commit

Permalink
Rename macro to skip_serializing_none
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasbb committed Apr 2, 2019
1 parent f7f64e3 commit 5ae2c21
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
18 changes: 9 additions & 9 deletions serde_with_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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<proc_macro2::TokenStream, String> {
fn skip_serializing_none_do(input: TokenStream) -> Result<proc_macro2::TokenStream, String> {
// 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::<ItemStruct>(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::<ItemEnum>(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::<Result<(), _>>()?;
Ok(quote!(#input))
} else {
Expand Down Expand Up @@ -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<Item = &'a mut Field>,
) -> Result<(), String> {
fields.into_iter().map(|field| ->Result<(), String> {
Expand Down Expand Up @@ -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())
}
}
}
14 changes: 7 additions & 7 deletions serde_with_macros/tests/skip_serializing_null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -38,7 +38,7 @@ macro_rules! test_tuple {
};
}

#[skip_serializing_null]
#[skip_serializing_none]
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)]
struct DataBasic {
a: Option<String>,
Expand All @@ -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<String>,
Expand All @@ -62,7 +62,7 @@ fn never<T>(_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")]
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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<String>, std::option::Option<String>);
test_tuple!(test_tuple, DataTuple);

#[skip_serializing_null]
#[skip_serializing_none]
#[derive(Debug, Eq, PartialEq, Serialize)]
enum DataEnum {
Tuple(Option<i64>, std::option::Option<bool>),
Expand Down

0 comments on commit 5ae2c21

Please sign in to comment.