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:
parent
19b51168c9
commit
14dcfc43d7
@ -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] **rename**: be able to rename a field
|
||||
- [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] **text**: this field match to the text content
|
||||
|
||||
|
||||
27
yaserde/tests/skip.rs
Normal file
27
yaserde/tests/skip.rs
Normal 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);
|
||||
}
|
||||
@ -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,
|
||||
},
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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))
|
||||
})
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user