From 13d383e43ce06a8e75d12cbf84a1cdb307e7f85e Mon Sep 17 00:00:00 2001 From: bobogei81123 <74331647+bobogei81123@users.noreply.github.com> Date: Sun, 14 Jan 2024 07:06:40 -0800 Subject: [PATCH] fix: fully qualify types in DeriveValueType macro (#2054) `DeriveValueType` proc macro relies on several sea-orm types to be imported. This is partially fixed in #1855 by fully qualifying `sea_orm::QueryResult`, but others like `sea_orm::Value` is still not being qualified. Also, if use defined a type alias on `std::result::Result`, it can cause the proc macro to generate code that won't compile. - Fully qualify `sea_orm::Value`, `std::string::String`, and `std::result::Result` in `DeriveValueType` proc macro. - Add some simple integration tests for this. - Add doc and doc test in `sea_orm_macro::derive_value_type`. --- sea-orm-macros/src/derives/value_type.rs | 9 +++++---- sea-orm-macros/src/lib.rs | 13 +++++++++++++ sea-orm-macros/tests/derive_value_type_test.rs | 13 +++++++++++++ 3 files changed, 31 insertions(+), 4 deletions(-) create mode 100644 sea-orm-macros/tests/derive_value_type_test.rs diff --git a/sea-orm-macros/src/derives/value_type.rs b/sea-orm-macros/src/derives/value_type.rs index e6cee9e6..286a9f73 100644 --- a/sea-orm-macros/src/derives/value_type.rs +++ b/sea-orm-macros/src/derives/value_type.rs @@ -100,7 +100,7 @@ impl DeriveValueType { quote!( #[automatically_derived] - impl std::convert::From<#name> for Value { + impl std::convert::From<#name> for sea_orm::Value { fn from(source: #name) -> Self { source.0.into() } @@ -108,18 +108,19 @@ impl DeriveValueType { #[automatically_derived] impl sea_orm::TryGetable for #name { - fn try_get_by(res: &sea_orm::QueryResult, idx: I) -> Result { + fn try_get_by(res: &sea_orm::QueryResult, idx: I) + -> std::result::Result { <#field_type as sea_orm::TryGetable>::try_get_by(res, idx).map(|v| #name(v)) } } #[automatically_derived] impl sea_orm::sea_query::ValueType for #name { - fn try_from(v: Value) -> Result { + fn try_from(v: sea_orm::Value) -> std::result::Result { <#field_type as sea_orm::sea_query::ValueType>::try_from(v).map(|v| #name(v)) } - fn type_name() -> String { + fn type_name() -> std::string::String { stringify!(#name).to_owned() } diff --git a/sea-orm-macros/src/lib.rs b/sea-orm-macros/src/lib.rs index 52f2e734..6d2db2d0 100644 --- a/sea-orm-macros/src/lib.rs +++ b/sea-orm-macros/src/lib.rs @@ -838,6 +838,19 @@ pub fn enum_iter(input: TokenStream) -> TokenStream { .into() } +/// Implements traits for types that wrap a database value type. +/// +/// This procedure macro implements `From for Value`, `sea_orm::TryGetTable`, and +/// `sea_query::ValueType` for the wrapper type `T`. +/// +/// ## Usage +/// +/// ```rust +/// use sea_orm::DeriveValueType; +/// +/// #[derive(DeriveValueType)] +/// struct MyString(String); +/// ``` #[cfg(feature = "derive")] #[proc_macro_derive(DeriveValueType, attributes(sea_orm))] pub fn derive_value_type(input: TokenStream) -> TokenStream { diff --git a/sea-orm-macros/tests/derive_value_type_test.rs b/sea-orm-macros/tests/derive_value_type_test.rs new file mode 100644 index 00000000..b1de7f15 --- /dev/null +++ b/sea-orm-macros/tests/derive_value_type_test.rs @@ -0,0 +1,13 @@ +#[test] +fn when_user_import_nothing_macro_still_works_test() { + #[derive(sea_orm::DeriveValueType)] + struct MyString(String); +} + +#[test] +fn when_user_alias_result_macro_still_works_test() { + #[allow(dead_code)] + type Result = std::result::Result; + #[derive(sea_orm::DeriveValueType)] + struct MyString(String); +}