Skip to content

Commit

Permalink
derive: special-case empty namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
Ten0 committed Apr 7, 2024
1 parent bf3aba1 commit 1ddb5c6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub(super) struct FieldTypeAndInstantiationsBuilder<'t, 'm> {
pub(super) added_where_clause_predicate_for_types:
std::collections::HashSet<Cow<'t, syn::Type>>,
pub(super) errors: &'m mut TokenStream,
pub(super) namespace: &'m Option<syn::LitStr>,
pub(super) namespace: &'m Option<String>,
pub(super) expand_namespace_var: bool,
}

Expand Down Expand Up @@ -115,18 +115,21 @@ impl<'t> FieldTypeAndInstantiationsBuilder<'t, '_> {
}
}
Some(namespace) => {
let namespace_prefix = if namespace.is_empty() {
"".to_owned()
} else {
format!("{}.", namespace)
};
let type_name = match override_fixed_name {
OverrideFixedName::NewtypeStruct { struct_name } => {
format!("{}.{}", namespace.value(), struct_name)
format!("{}{}", namespace_prefix, struct_name)
}
OverrideFixedName::NewtypeVariant {
enum_name,
variant_name,
} => format!(
"{}.{}.{}",
namespace.value(),
enum_name,
variant_name
"{}{}.{}",
namespace_prefix, enum_name, variant_name
),
};
quote! { #type_name.to_owned() }
Expand Down
8 changes: 6 additions & 2 deletions serde_avro_derive_macros/src/build_schema/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub(crate) struct SchemaDeriveInput {
data: darling::ast::Data<SchemaDeriveVariant, SchemaDeriveField>,
generics: syn::Generics,

namespace: Option<syn::LitStr>,
namespace: Option<String>,
}

#[derive(darling::FromField, Debug)]
Expand Down Expand Up @@ -79,7 +79,11 @@ pub(crate) fn schema_impl(input: SchemaDeriveInput) -> Result<TokenStream, Error
}
}
Some(namespace) => {
let type_name = format!("{}.{}", namespace.value(), type_ident);
let type_name = if namespace.is_empty() {
type_ident.to_string()
} else {
format!("{}.{}", namespace, type_ident)
};
quote! {
let mut type_name = #type_name.to_owned();
}
Expand Down

0 comments on commit 1ddb5c6

Please sign in to comment.