feat: handle same field name but different namespace

issue #186
This commit is contained in:
Marc-Antoine Arnaud
2024-08-31 16:54:53 +02:00
parent e4aff84acb
commit a067b85ee2
6 changed files with 76 additions and 8 deletions

View File

@@ -90,6 +90,13 @@ impl YaSerdeField {
},
);
let prefix = self
.attributes
.prefix
.clone()
.map(|p| format!("{}_", p.to_upper_camel_case()))
.unwrap_or_default();
let attribute = self
.attributes
.attribute
@@ -98,7 +105,8 @@ impl YaSerdeField {
Ident::new(
&format!(
"__Visitor_{attribute}{}_{}",
"__Visitor_{attribute}{}{}_{}",
prefix,
label.replace('.', "_").to_upper_camel_case(),
struct_id
),
@@ -130,6 +138,20 @@ impl YaSerdeField {
.map(|skip_serializing_if| Ident::new(skip_serializing_if, self.get_span()))
}
pub fn prefix_namespace(&self, root_attributes: &YaSerdeAttribute) -> String {
root_attributes
.namespaces
.iter()
.find_map(|(prefix, namespace)| {
if self.attributes.prefix.eq(prefix) {
Some(namespace.clone())
} else {
None
}
})
.unwrap_or_default()
}
pub fn get_namespace_matching(
&self,
root_attributes: &YaSerdeAttribute,
@@ -261,7 +283,7 @@ impl From<&syn::PathSegment> for Field {
return Field::from(&path.path);
}
Some(syn::GenericArgument::Type(syn::Type::Group(syn::TypeGroup { elem, .. }))) => {
if let syn::Type::Path(ref group) = elem.as_ref() {
if let Path(ref group) = elem.as_ref() {
return Field::from(&group.path);
}
}

View File

@@ -148,9 +148,11 @@ pub fn parse(
let value_label = field.get_value_label();
let label_name = field.renamed_label_without_namespace();
let namespace = field.prefix_namespace(root_attributes);
let visit_struct = |struct_name: syn::Path, action: TokenStream| {
Some(quote! {
#label_name => {
(#namespace, #label_name) => {
if depth == 0 {
// Don't count current struct's StartElement as substruct's StartElement
let _root = reader.next_event();
@@ -423,7 +425,9 @@ pub fn parse(
let event = reader.next_event()?;
#write_unused
} else {
match name.local_name.as_str() {
let namespace = name.namespace.clone().unwrap_or_default();
match (namespace.as_str(), name.local_name.as_str()) {
#call_visitors
_ => {
let event = reader.next_event()?;
@@ -493,8 +497,10 @@ fn build_call_visitor(
quote!(name.local_name.as_str()),
);
let namespace = field.prefix_namespace(root_attributes);
Some(quote! {
#label_name => {
(#namespace, #label_name) => {
let visitor = #visitor_label{};
#namespaces_matching