martin-g commented on code in PR #398:
URL: https://github.com/apache/avro-rs/pull/398#discussion_r2703511584
##########
avro_derive/tests/derive.rs:
##########
@@ -1996,6 +1996,42 @@ fn avro_rs_397_derive_with_expr_lambda() {
assert_eq!(expected_schema, derived_schema);
}
+#[test]
+fn avro_rs_398_transparent_with_skip() {
+ let schema = Schema::parse_str(
+ r#"
+ {
+ "type":"record",
+ "name":"Foo",
+ "fields": [
+ {
+ "name":"b",
+ "type":"long"
+ }
+ ]
+ }
+ "#,
+ )
+ .unwrap();
+
+ fn long_schema(_named_schemas: &mut Names, _enclosing_namespace:
&Namespace) -> Schema {
+ Schema::Long
+ }
+
+ #[allow(dead_code)]
+ #[derive(AvroSchema)]
+ struct Foo {
Review Comment:
There is no usage of `#[serde(transparent)]` here
##########
avro_derive/src/lib.rs:
##########
@@ -34,86 +35,93 @@ use crate::{
#[proc_macro_derive(AvroSchema, attributes(avro, serde))]
// Templated from Serde
pub fn proc_macro_derive_avro_schema(input: proc_macro::TokenStream) ->
proc_macro::TokenStream {
- let mut input = parse_macro_input!(input as DeriveInput);
- derive_avro_schema(&mut input)
+ let input = parse_macro_input!(input as DeriveInput);
+ derive_avro_schema(input)
.unwrap_or_else(to_compile_errors)
.into()
}
-fn derive_avro_schema(input: &mut DeriveInput) -> Result<TokenStream,
Vec<syn::Error>> {
- let named_type_options = NamedTypeOptions::new(&input.attrs,
input.span())?;
-
- let rename_all = named_type_options.rename_all;
- let name = named_type_options.name.unwrap_or(input.ident.to_string());
-
- let full_schema_name = vec![named_type_options.namespace, Some(name)]
- .into_iter()
- .flatten()
- .collect::<Vec<String>>()
- .join(".");
- let schema_def = match &input.data {
- syn::Data::Struct(s) => get_data_struct_schema_def(
- &full_schema_name,
- named_type_options
- .doc
- .or_else(|| extract_outer_doc(&input.attrs)),
- named_type_options.alias,
- rename_all,
- s,
- input.ident.span(),
- )?,
- syn::Data::Enum(e) => get_data_enum_schema_def(
- &full_schema_name,
- named_type_options
- .doc
- .or_else(|| extract_outer_doc(&input.attrs)),
- named_type_options.alias,
- rename_all,
- e,
- input.ident.span(),
- )?,
- _ => {
- return Err(vec![syn::Error::new(
- input.ident.span(),
- "AvroSchema derive only works for structs and simple enums ",
- )]);
+fn derive_avro_schema(input: DeriveInput) -> Result<TokenStream,
Vec<syn::Error>> {
+ // It would be nice to parse the attributes before the `match`, but we
first need to validate that `input` is not a union.
+ // Otherwise a user could get errors related to the attributes and after
fixing those get an error because the attributes were on a union.
+ let input_span = input.span();
+ match input.data {
+ syn::Data::Struct(data_struct) => {
+ let named_type_options = NamedTypeOptions::new(&input.ident,
&input.attrs, input_span)?;
+ let inner = if named_type_options.transparent {
+ get_transparent_struct_schema_def(data_struct.fields,
input_span)?
+ } else {
+ let schema_def =
+ get_struct_schema_def(&named_type_options, data_struct,
input.ident.span())?;
+ handle_named_schemas(named_type_options.name, schema_def)
+ };
+ Ok(create_trait_definition(input.ident, &input.generics, inner))
}
- };
- let ident = &input.ident;
- let (impl_generics, ty_generics, where_clause) =
input.generics.split_for_impl();
- Ok(quote! {
+ syn::Data::Enum(data_enum) => {
+ let named_type_options = NamedTypeOptions::new(&input.ident,
&input.attrs, input_span)?;
+ if named_type_options.transparent {
+ return Err(vec![syn::Error::new(
+ input_span,
+ "AvroSchema: `#[serde(transparent)]` is only supported on
structs",
+ )]);
+ }
+ let schema_def =
+ get_data_enum_schema_def(&named_type_options, data_enum,
input.ident.span())?;
+ let inner = handle_named_schemas(named_type_options.name,
schema_def);
+ Ok(create_trait_definition(input.ident, &input.generics, inner))
+ }
+ syn::Data::Union(_) => Err(vec![syn::Error::new(
+ input.span(),
Review Comment:
```suggestion
input_span,
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]