This is an automated email from the ASF dual-hosted git repository. mgrigorov pushed a commit to branch avro-3709-add-aliases-to-record-fields in repository https://gitbox.apache.org/repos/asf/avro.git
commit fc39fae11e72eb3be7627822c09a5914299fdb1d Author: Martin Tzvetanov Grigorov <[email protected]> AuthorDate: Wed Feb 8 13:41:35 2023 +0200 AVRO-3709: [Rust] Add support for field aliases in avro_derive Signed-off-by: Martin Tzvetanov Grigorov <[email protected]> --- lang/rust/avro_derive/src/lib.rs | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/lang/rust/avro_derive/src/lib.rs b/lang/rust/avro_derive/src/lib.rs index 5f8c0bc1c..369bcfdb6 100644 --- a/lang/rust/avro_derive/src/lib.rs +++ b/lang/rust/avro_derive/src/lib.rs @@ -30,8 +30,8 @@ struct FieldOptions { doc: Option<String>, #[darling(default)] default: Option<String>, - // #[darling(default)] - // aliases: Option<Vec<String>>, + #[darling(multiple)] + alias: Vec<String>, #[darling(default)] rename: Option<String>, #[darling(default)] @@ -150,7 +150,7 @@ fn get_data_struct_schema_def( } None => quote! { None }, }; - let aliases = quote! { None }; + let aliases = preserve_vec(field_attrs.alias); let schema_expr = type_to_schema_expr(&field.ty)?; let position = index; record_field_exprs.push(quote! { @@ -459,8 +459,9 @@ mod tests { match syn::parse2::<DeriveInput>(test_struct) { Ok(mut input) => { - assert!(derive_avro_schema(&mut input).is_ok()); - assert!(derive_avro_schema(&mut input) + let schema_token_stream = derive_avro_schema(&mut input); + assert!(&schema_token_stream.is_ok()); + assert!(schema_token_stream .unwrap() .to_string() .contains("namespace.testing")) @@ -496,4 +497,26 @@ mod tests { assert_eq!(type_path_schema_expr(&syn::parse2::<TypePath>(quote!{Vec<T>}).unwrap()).to_string(), quote!{<Vec<T> as apache_avro::schema::derive::AvroSchemaComponent>::get_schema_in_ctxt(named_schemas, enclosing_namespace)}.to_string()); assert_eq!(type_path_schema_expr(&syn::parse2::<TypePath>(quote!{AnyType}).unwrap()).to_string(), quote!{<AnyType as apache_avro::schema::derive::AvroSchemaComponent>::get_schema_in_ctxt(named_schemas, enclosing_namespace)}.to_string()); } + + #[test] + fn test_avro_3709_record_field_attributes() { + let test_struct = quote! { + struct A { + #[avro(alias = "a1", alias = "a2", doc = "a doc", default = "123", rename = "a3")] + a: i32 + } + }; + + match syn::parse2::<DeriveInput>(test_struct) { + Ok(mut input) => { + let schema_res = derive_avro_schema(&mut input); + let expected_token_stream = r#"let schema_fields = vec ! [apache_avro :: schema :: RecordField { name : "a3" . to_string () , doc : Some ("a doc" . into ()) , default : Some (serde_json :: from_str ("123") . expect (format ! ("Invalid JSON: {:?}" , "123") . as_str ())) , aliases : Some (vec ! ["a1" . into () , "a2" . into ()]) , schema : apache_avro :: schema :: Schema :: Int , order : apache_avro :: schema :: RecordFieldOrder :: Ascending , position : 0usize , custom_att [...] + let schema_token_stream = schema_res.unwrap().to_string(); + assert!(schema_token_stream.contains(expected_token_stream)); + } + Err(error) => panic!( + "Failed to parse as derive input when it should be able to. Error: {error:?}" + ), + }; + } }
