Add "skip_serializing" attribute

If it is present on a field, that field should not be included in the output
If both "skip_serializing" and "skip_serializing_if" are present,
"skip_serializing" should take precedent.
This commit is contained in:
Amy Keibler
2022-01-06 11:26:32 -05:00
parent 19b51168c9
commit 14dcfc43d7
5 changed files with 48 additions and 4 deletions

View File

@@ -12,6 +12,7 @@ pub struct YaSerdeAttribute {
pub namespaces: BTreeMap<Option<String>, String>,
pub prefix: Option<String>,
pub rename: Option<String>,
pub skip_serializing: bool,
pub skip_serializing_if: Option<String>,
pub text: bool,
}
@@ -39,6 +40,7 @@ impl YaSerdeAttribute {
let mut namespaces = BTreeMap::new();
let mut prefix = None;
let mut rename = None;
let mut skip_serializing = false;
let mut skip_serializing_if = None;
let mut text = false;
@@ -80,6 +82,9 @@ impl YaSerdeAttribute {
"rename" => {
rename = get_value(&mut attr_iter);
}
"skip_serializing" => {
skip_serializing = true;
}
"skip_serializing_if" => {
skip_serializing_if = get_value(&mut attr_iter);
}
@@ -102,6 +107,7 @@ impl YaSerdeAttribute {
namespaces,
prefix,
rename,
skip_serializing,
skip_serializing_if,
text,
}
@@ -177,6 +183,7 @@ fn parse_empty_attributes() {
namespaces: BTreeMap::new(),
prefix: None,
rename: None,
skip_serializing: false,
skip_serializing_if: None,
text: false,
},
@@ -226,6 +233,7 @@ fn parse_attributes() {
namespaces: BTreeMap::new(),
prefix: None,
rename: None,
skip_serializing: false,
skip_serializing_if: None,
text: false,
},
@@ -275,6 +283,7 @@ fn only_parse_yaserde_attributes() {
namespaces: BTreeMap::new(),
prefix: None,
rename: None,
skip_serializing: false,
skip_serializing_if: None,
text: false,
},
@@ -327,6 +336,7 @@ fn parse_attributes_with_values() {
namespaces,
prefix: None,
rename: None,
skip_serializing: false,
skip_serializing_if: None,
text: false,
},

View File

@@ -40,6 +40,10 @@ impl YaSerdeField {
self.syn_field.ident.clone()
}
pub fn is_skip_serializing(&self) -> bool {
self.attributes.skip_serializing
}
pub fn get_value_label(&self) -> Option<syn::Ident> {
self
.syn_field

View File

@@ -47,18 +47,20 @@ pub fn condition_generator(label: &Option<Ident>, field: &YaSerdeField) -> Token
.get_default_function()
.map(|default_function| quote!(self.#label != #default_function()));
let skip_serializing = field.is_skip_serializing();
field
.get_skip_serializing_if_function()
.map(|skip_if_function| {
if let Some(prev_conditions) = &default_condition {
quote!(if !self.#skip_if_function(&self.#label) && #prev_conditions)
quote!(if !#skip_serializing && !self.#skip_if_function(&self.#label) && #prev_conditions)
} else {
quote!(if !self.#skip_if_function(&self.#label))
quote!(if !#skip_serializing && !self.#skip_if_function(&self.#label))
}
})
.unwrap_or_else(|| {
default_condition
.map(|condition| quote!(if #condition))
.unwrap_or_default()
.map(|condition| quote!(if !#skip_serializing && #condition))
.unwrap_or(quote!(if !#skip_serializing))
})
}