You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@avro.apache.org by mg...@apache.org on 2022/11/07 20:39:03 UTC

[avro] branch master updated: AVRO-3663: [Rust] Allow raw identifiers as field names (#1945)

This is an automated email from the ASF dual-hosted git repository.

mgrigorov pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/avro.git


The following commit(s) were added to refs/heads/master by this push:
     new 48b9bacf3 AVRO-3663: [Rust] Allow raw identifiers as field names (#1945)
48b9bacf3 is described below

commit 48b9bacf308d6cd98cfb9cdb22e96aaaea324ecd
Author: Neidhart Orlich <ne...@gmail.com>
AuthorDate: Mon Nov 7 21:38:57 2022 +0100

    AVRO-3663: [Rust] Allow raw identifiers as field names (#1945)
    
    * avro_derive: Allow raw identifiers as field names
    
    * AVRO-3663: Add the ticket number in the test method name
    
    * AVRO-3663: Print the debug info for the derived schema
    
    * AVRO-3663: Fix formatting
    
    Signed-off-by: Martin Tzvetanov Grigorov <mg...@apache.org>
    
    Signed-off-by: Martin Tzvetanov Grigorov <mg...@apache.org>
    Co-authored-by: Martin Grigorov <ma...@users.noreply.github.com>
    Co-authored-by: Martin Tzvetanov Grigorov <mg...@apache.org>
---
 lang/rust/avro_derive/src/lib.rs      |  3 +++
 lang/rust/avro_derive/tests/derive.rs | 16 ++++++++++++++++
 2 files changed, 19 insertions(+)

diff --git a/lang/rust/avro_derive/src/lib.rs b/lang/rust/avro_derive/src/lib.rs
index 3c20b8095..238d95b18 100644
--- a/lang/rust/avro_derive/src/lib.rs
+++ b/lang/rust/avro_derive/src/lib.rs
@@ -123,6 +123,9 @@ fn get_data_struct_schema_def(
             let mut index: usize = 0;
             for field in a.named.iter() {
                 let mut name = field.ident.as_ref().unwrap().to_string(); // we know everything has a name
+                if let Some(raw_name) = name.strip_prefix("r#") {
+                    name = raw_name.to_string();
+                }
                 let field_attrs =
                     FieldOptions::from_attributes(&field.attrs[..]).map_err(darling_to_syn)?;
                 let doc = preserve_optional(field_attrs.doc);
diff --git a/lang/rust/avro_derive/tests/derive.rs b/lang/rust/avro_derive/tests/derive.rs
index 6eccb0579..a2ac0b6bf 100644
--- a/lang/rust/avro_derive/tests/derive.rs
+++ b/lang/rust/avro_derive/tests/derive.rs
@@ -1545,4 +1545,20 @@ mod test_derive {
             c: 987.654,
         });
     }
+
+    #[test]
+    fn test_avro_3663_raw_identifier_field_name() {
+        #[derive(Debug, Serialize, Deserialize, AvroSchema, Clone, PartialEq)]
+        struct TestRawIdent {
+            r#type: bool,
+        }
+
+        let derived_schema = TestRawIdent::get_schema();
+        if let Schema::Record { fields, .. } = derived_schema {
+            let field = fields.get(0).expect("TestRawIdent must contain a field");
+            assert_eq!(field.name, "type");
+        } else {
+            panic!("Unexpected schema type for {:?}", derived_schema)
+        }
+    }
 }