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

chaokunyang pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/fory.git


The following commit(s) were added to refs/heads/main by this push:
     new 7698fee47 chore(rust): add tests to use #[derive(ForyObject)] in 
macro_rules! (#2867)
7698fee47 is described below

commit 7698fee476770f6a49ba028717a256161230b843
Author: Artavazd Balaian <[email protected]>
AuthorDate: Sun Nov 2 18:22:36 2025 +0800

    chore(rust): add tests to use #[derive(ForyObject)] in macro_rules! (#2867)
    
    ## Why?
    
    Allow to use #[derive(ForyObject)] in macro_rules!
    
    ## What does this PR do?
    
    Allow to use #[derive(ForyObject)] in macro_rules!
    
    ## Related issues
    
    
    Is there any related issue? If this PR closes them you say say
    fix/closes:
    
    - #2866
    
    ---------
    
    Co-authored-by: Shawn Yang <[email protected]>
---
 rust/fory-derive/src/object/read.rs | 14 +--------
 rust/tests/tests/test_fory.rs       | 58 +++++++++++++++++++++++++++++++++++++
 2 files changed, 59 insertions(+), 13 deletions(-)

diff --git a/rust/fory-derive/src/object/read.rs 
b/rust/fory-derive/src/object/read.rs
index ec92209e7..7160e814b 100644
--- a/rust/fory-derive/src/object/read.rs
+++ b/rust/fory-derive/src/object/read.rs
@@ -17,7 +17,7 @@
 
 use proc_macro2::{Ident, TokenStream};
 use quote::{format_ident, quote};
-use syn::{Field, Type};
+use syn::Field;
 
 use super::util::{
     classify_trait_object_field, compute_struct_version_hash, 
create_wrapper_types_arc,
@@ -288,12 +288,6 @@ fn gen_read_compatible_match_arm_body(field: &Field, 
var_name: &Ident) -> TokenS
     let base = if is_skip_flag {
         match field_kind {
             StructField::None => {
-                // Note: _base_ty is currently unused but kept for potential 
future use
-                let _base_ty = match &ty {
-                    Type::Path(type_path) => 
Some(&type_path.path.segments.first().unwrap().ident),
-                    Type::Tuple(_) => None, // Tuples don't have a simple ident
-                    _ => None,              // Other types also don't have a 
simple ident
-                };
                 let dec_by_option = need_declared_by_option(field);
                 if dec_by_option {
                     quote! {
@@ -391,12 +385,6 @@ fn gen_read_compatible_match_arm_body(field: &Field, 
var_name: &Ident) -> TokenS
                 }
             }
             StructField::None => {
-                // Note: _base_ty is currently unused but kept for potential 
future use
-                let _base_ty = match &ty {
-                    Type::Path(type_path) => 
Some(&type_path.path.segments.first().unwrap().ident),
-                    Type::Tuple(_) => None, // Tuples don't have a simple ident
-                    _ => None,              // Other types also don't have a 
simple ident
-                };
                 let skip_type_info = should_skip_type_info_for_field(ty);
                 let dec_by_option = need_declared_by_option(field);
                 if skip_type_info {
diff --git a/rust/tests/tests/test_fory.rs b/rust/tests/tests/test_fory.rs
index 541d36cbc..175694895 100644
--- a/rust/tests/tests/test_fory.rs
+++ b/rust/tests/tests/test_fory.rs
@@ -195,3 +195,61 @@ fn test_serialize_to_detailed() {
         assert_eq!(*point, deserialized);
     }
 }
+
+use chrono::{DateTime, NaiveDateTime, Utc};
+
+macro_rules! impl_value {
+    ($record:ident, $value:ident, { $($field:ident : $ty:ty = $expr:expr),* 
$(,)? }) => {
+        #[derive(ForyObject)]
+        pub struct $value {
+            $(pub $field: $ty,)*
+        }
+
+        impl $record {
+            pub fn to_key_value(self) -> (String, $value) {
+                let Self {
+                    feature_key,
+                    $($field,)*
+                } = self;
+
+                let value = $value {
+                    $($field: $expr,)*
+                };
+
+                (feature_key, value)
+            }
+        }
+    };
+}
+
+#[derive(Debug, Clone)]
+pub struct KeyValue {
+    feature_key: String,
+    count: u64,
+    last_seen_event_time: DateTime<Utc>,
+}
+
+impl_value!(
+    KeyValue,
+    Value,
+    {
+        count: u64 = count,
+        last_seen_event_time: NaiveDateTime = last_seen_event_time.naive_utc(),
+    }
+);
+
+#[test]
+fn test_in_macro() {
+    let key_value = KeyValue {
+        feature_key: "test_key".to_string(),
+        count: 100,
+        last_seen_event_time: Utc::now(),
+    };
+    let (key, value) = key_value.clone().to_key_value();
+    assert_eq!(key, "test_key");
+    assert_eq!(value.count, 100);
+    assert_eq!(
+        value.last_seen_event_time,
+        key_value.last_seen_event_time.naive_utc()
+    );
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to