Merge pull request #133 from amy-keibler/131-skip-serializing

Add "skip_serializing" attribute
This commit is contained in:
Marc-Antoine ARNAUD 2022-03-25 10:06:14 +01:00 committed by GitHub
commit 4444d3f507
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 48 additions and 4 deletions

View File

@ -32,6 +32,7 @@ This library will support XML de/ser-ializing with all specific features.
- [x] **namespace**: defines the namespace of the field - [x] **namespace**: defines the namespace of the field
- [x] **rename**: be able to rename a field - [x] **rename**: be able to rename a field
- [x] **root**: rename the based element. Used only at the XML root. - [x] **root**: rename the based element. Used only at the XML root.
- [x] **skip_serializing**: Exclude this field from the serialized output
- [x] **skip_serializing_if**: Skip the serialisation for this field if the condition is true - [x] **skip_serializing_if**: Skip the serialisation for this field if the condition is true
- [x] **text**: this field match to the text content - [x] **text**: this field match to the text content

27
yaserde/tests/skip.rs Normal file
View File

@ -0,0 +1,27 @@
#[macro_use]
extern crate yaserde;
#[macro_use]
extern crate yaserde_derive;
fn init() {
let _ = env_logger::builder().is_test(true).try_init();
}
#[test]
fn skip_serializing() {
init();
#[derive(YaSerialize, PartialEq, Debug)]
#[yaserde(rename = "base")]
pub struct XmlStruct {
#[yaserde(skip_serializing)]
skipped_serializing: String,
}
let model = XmlStruct {
skipped_serializing: "skipped serializing".to_string(),
};
let content = "<base />";
serialize_and_validate!(model, content);
}

View File

@ -12,6 +12,7 @@ pub struct YaSerdeAttribute {
pub namespaces: BTreeMap<Option<String>, String>, pub namespaces: BTreeMap<Option<String>, String>,
pub prefix: Option<String>, pub prefix: Option<String>,
pub rename: Option<String>, pub rename: Option<String>,
pub skip_serializing: bool,
pub skip_serializing_if: Option<String>, pub skip_serializing_if: Option<String>,
pub text: bool, pub text: bool,
} }
@ -39,6 +40,7 @@ impl YaSerdeAttribute {
let mut namespaces = BTreeMap::new(); let mut namespaces = BTreeMap::new();
let mut prefix = None; let mut prefix = None;
let mut rename = None; let mut rename = None;
let mut skip_serializing = false;
let mut skip_serializing_if = None; let mut skip_serializing_if = None;
let mut text = false; let mut text = false;
@ -80,6 +82,9 @@ impl YaSerdeAttribute {
"rename" => { "rename" => {
rename = get_value(&mut attr_iter); rename = get_value(&mut attr_iter);
} }
"skip_serializing" => {
skip_serializing = true;
}
"skip_serializing_if" => { "skip_serializing_if" => {
skip_serializing_if = get_value(&mut attr_iter); skip_serializing_if = get_value(&mut attr_iter);
} }
@ -102,6 +107,7 @@ impl YaSerdeAttribute {
namespaces, namespaces,
prefix, prefix,
rename, rename,
skip_serializing,
skip_serializing_if, skip_serializing_if,
text, text,
} }
@ -176,6 +182,7 @@ fn parse_empty_attributes() {
namespaces: BTreeMap::new(), namespaces: BTreeMap::new(),
prefix: None, prefix: None,
rename: None, rename: None,
skip_serializing: false,
skip_serializing_if: None, skip_serializing_if: None,
text: false, text: false,
}, },
@ -225,6 +232,7 @@ fn parse_attributes() {
namespaces: BTreeMap::new(), namespaces: BTreeMap::new(),
prefix: None, prefix: None,
rename: None, rename: None,
skip_serializing: false,
skip_serializing_if: None, skip_serializing_if: None,
text: false, text: false,
}, },
@ -274,6 +282,7 @@ fn only_parse_yaserde_attributes() {
namespaces: BTreeMap::new(), namespaces: BTreeMap::new(),
prefix: None, prefix: None,
rename: None, rename: None,
skip_serializing: false,
skip_serializing_if: None, skip_serializing_if: None,
text: false, text: false,
}, },
@ -329,6 +338,7 @@ fn parse_attributes_with_values() {
namespaces, namespaces,
prefix: None, prefix: None,
rename: None, rename: None,
skip_serializing: false,
skip_serializing_if: None, skip_serializing_if: None,
text: false, text: false,
}, },

View File

@ -40,6 +40,10 @@ impl YaSerdeField {
self.syn_field.ident.clone() 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> { pub fn get_value_label(&self) -> Option<syn::Ident> {
self self
.syn_field .syn_field

View File

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