mybatis_util/
table_util.rs

1use std::collections::{BTreeMap, HashMap};
2/// Simplifies table construction by relying on the Default trait
3///
4/// step1:  impl Default
5/// #[crud_table]
6/// #[derive(Clone, Debug, Default)]
7/// BizActivity{
8/// }
9///
10/// //step2: make struct
11/// let activity = mybatis::make_table!(BizActivity{
12///             id : "12312".to_string(),
13///             delete_flag : 1,
14///             name:  None,
15///             });
16#[macro_export]
17macro_rules! make_table {
18        ($t:path{ $($key:ident:$value:expr$(,)?)+ }) => {
19           {
20            let mut temp_table_data = <$t>::default();
21            $(temp_table_data.$key = $value.into();)+
22            temp_table_data
23           }
24        }
25}
26/// take the target Vec member attribute Vec collection
27/// vec_ref: a reference to vec, field_name: the field name of the structure
28///
29/// need impl Clone or #[derive(Clone, Debug)]
30/// for example:
31///      struct SysUserRole{
32///         pub role_id:String
33///      }
34///      let user_roles: Vec<SysUserRole>;
35///      let role_ids = make_table_field_vec!(&user_roles,role_id); // role_ids: Vec<String>
36///
37///
38///
39#[allow(unused_macros)]
40#[macro_export]
41macro_rules! make_table_field_vec {
42    ($vec_ref:expr,$field_name:ident) => {{
43        let mut ids = vec![];
44        for item in $vec_ref {
45            match item.$field_name.as_ref() {
46                std::option::Option::Some(v) => {
47                    ids.push(v.clone());
48                }
49                _ => {}
50            }
51        }
52        ids
53    }};
54}
55
56/// Gets the HashMap collection of member attributes of the target Vec
57/// vec_ref: vec reference,field_name: the field name of the structure
58///
59/// need impl Clone or #[derive(Clone, Debug)]
60/// for example:
61///      struct SysUserRole{
62///         pub role_id:String
63///      }
64///      let user_roles: Vec<SysUserRole>;
65///      let role_ids = make_table_field_map!(&user_roles,role_id); // role_ids: HashMap<String,SysUserRole>
66///
67///
68///
69#[allow(unused_macros)]
70#[macro_export]
71macro_rules! make_table_field_map {
72    ($vec_ref:expr,$field_name:ident) => {{
73        let mut ids = std::collections::HashMap::new();
74        for item in $vec_ref {
75            match item.$field_name.as_ref() {
76                std::option::Option::Some(v) => {
77                    ids.insert(v.clone(), item.clone());
78                }
79                _ => {}
80            }
81        }
82        ids
83    }};
84}
85
86/// Gets the HashMap collection of member attributes of the target Vec
87/// vec_ref: vec reference,field_name: the field name of the structure
88///
89/// need impl Clone or #[derive(Clone, Debug)]
90/// for example:
91///      struct SysUserRole{
92///         pub role_id:String
93///      }
94///      let user_roles: Vec<SysUserRole>;
95///      let role_ids = make_table_field_map_btree!(&user_roles,role_id); // role_ids: HashMap<String,SysUserRole>
96///
97///
98///
99#[allow(unused_macros)]
100#[macro_export]
101macro_rules! make_table_field_map_btree {
102    ($vec_ref:expr,$field_name:ident) => {{
103        let mut ids = std::collections::BTreeMap::new();
104        for item in $vec_ref {
105            match item.$field_name.as_ref() {
106                std::option::Option::Some(v) => {
107                    ids.insert(v.clone(), item.clone());
108                }
109                _ => {}
110            }
111        }
112        ids
113    }};
114}
115
116#[allow(unused_macros)]
117#[macro_export]
118macro_rules! as_bson {
119    ($key:expr) => {
120        rbson::to_bson($key).unwrap_or_default()
121    }
122}
123
124/// Used to simulate enumerations to improve code maintainability.
125/// this is return &str data
126/// for example:
127/// let name=field_name!(BizActivity.id);
128/// rb.new_wrapper().eq(field_name!(BizActivity.id),"1")
129///
130#[allow(unused_macros)]
131#[macro_export]
132macro_rules! field_name {
133    ($t:ident.$field:ident) => {
134        {
135           if false{|a:$t|{a.$field};}
136           stringify!($field).trim_start_matches("r#")
137        }
138    };
139}
140
141
142/// will create pub fn field_name()->&str method
143/// for example:
144///     #[crud_table]
145///     #[derive(Clone, Debug)]
146///     pub struct BizActivity {
147///         pub id: Option<String>,
148///         pub name: Option<String>,
149///         pub delete_flag: Option<i32>,
150///     }
151///     impl_field_name_method!(BizActivity{id,name,delete_flag});
152///
153///    println!("{}",BizActivity::delete_flag());
154#[allow(unused_macros)]
155#[macro_export]
156macro_rules! impl_field_name_method {
157   ($target:path{$($field_name:ident$(,)?)+}) => {
158         impl $target{
159               $(
160                         #[inline]
161                         pub fn $field_name()->&'static str{
162                               if false{
163                                  |a:$target|{a.$field_name};
164                               }
165                             stringify!($field_name).trim_start_matches("r#")
166                         }
167               )+
168         }
169    };
170}