Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fully qualify types in DeriveValueType macro #2054

Merged
merged 1 commit into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions sea-orm-macros/src/derives/value_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,26 +100,27 @@ 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()
}
}

#[automatically_derived]
impl sea_orm::TryGetable for #name {
fn try_get_by<I: sea_orm::ColIdx>(res: &sea_orm::QueryResult, idx: I) -> Result<Self, sea_orm::TryGetError> {
fn try_get_by<I: sea_orm::ColIdx>(res: &sea_orm::QueryResult, idx: I)
-> std::result::Result<Self, sea_orm::TryGetError> {
<#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<Self, sea_orm::sea_query::ValueTypeErr> {
fn try_from(v: sea_orm::Value) -> std::result::Result<Self, sea_orm::sea_query::ValueTypeErr> {
<#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()
}

Expand Down
13 changes: 13 additions & 0 deletions sea-orm-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> 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 {
Expand Down
13 changes: 13 additions & 0 deletions sea-orm-macros/tests/derive_value_type_test.rs
Original file line number Diff line number Diff line change
@@ -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<T> = std::result::Result<T, ()>;
#[derive(sea_orm::DeriveValueType)]
struct MyString(String);
}
Loading