From 1497c2c7f0f3f706e218a77477a873fd168cb27c Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Thu, 27 Oct 2022 00:17:46 +0800 Subject: [PATCH] Fix DeriveActiveEnum requires additional imports (#1154) * fix: Support deriving ActiveEnum without importing EnumIter, Iden, and ActiveEnum * style: Fix format * DeriveActiveEnum without depending on sea_query::Iden derive macros * [issues] add tests * Fix [issues] features Co-authored-by: Naoki Ikeguchi --- issues/1143/Cargo.toml | 17 +++++++++++ issues/1143/src/entity/mod.rs | 1 + .../1143/src/entity/sea_orm_active_enums.rs | 28 +++++++++++++++++++ issues/1143/src/main.rs | 4 +++ issues/249/app/Cargo.toml | 2 +- issues/892/Cargo.toml | 2 +- sea-orm-macros/src/derives/active_enum.rs | 27 ++++++++++++++---- sea-orm-macros/src/lib.rs | 13 +++++++++ 8 files changed, 87 insertions(+), 7 deletions(-) create mode 100644 issues/1143/Cargo.toml create mode 100644 issues/1143/src/entity/mod.rs create mode 100644 issues/1143/src/entity/sea_orm_active_enums.rs create mode 100644 issues/1143/src/main.rs diff --git a/issues/1143/Cargo.toml b/issues/1143/Cargo.toml new file mode 100644 index 00000000..6dae314c --- /dev/null +++ b/issues/1143/Cargo.toml @@ -0,0 +1,17 @@ +[workspace] +# A separate workspace + +[package] +name = "sea-orm-issues-1143" +version = "0.1.0" +edition = "2021" +publish = false + +[dependencies] +serde = "^1" +tokio = { version = "^1", features = ["rt", "rt-multi-thread", "macros"] } + +[dependencies.sea-orm] +path = "../../" +default-features = false +features = ["macros", "runtime-tokio-native-tls"] diff --git a/issues/1143/src/entity/mod.rs b/issues/1143/src/entity/mod.rs new file mode 100644 index 00000000..df52351b --- /dev/null +++ b/issues/1143/src/entity/mod.rs @@ -0,0 +1 @@ +pub mod sea_orm_active_enums; diff --git a/issues/1143/src/entity/sea_orm_active_enums.rs b/issues/1143/src/entity/sea_orm_active_enums.rs new file mode 100644 index 00000000..57ba0551 --- /dev/null +++ b/issues/1143/src/entity/sea_orm_active_enums.rs @@ -0,0 +1,28 @@ +use sea_orm::entity::prelude::*; + +#[derive(Debug, Clone, PartialEq, Eq, EnumIter, DeriveActiveEnum)] +#[sea_orm(rs_type = "String", db_type = "String(Some(1))")] +pub enum Category { + #[sea_orm(string_value = "B")] + Big, + #[sea_orm(string_value = "S")] + Small, +} + +#[derive(Debug, Clone, PartialEq, Eq, EnumIter, DeriveActiveEnum)] +#[sea_orm(rs_type = "i32", db_type = "Integer")] +pub enum Color { + #[sea_orm(num_value = 0)] + Black, + #[sea_orm(num_value = 1)] + White, +} + +#[derive(Debug, Clone, PartialEq, Eq, EnumIter, DeriveActiveEnum)] +#[sea_orm(rs_type = "String", db_type = "Enum", enum_name = "tea")] +pub enum Tea { + #[sea_orm(string_value = "EverydayTea")] + EverydayTea, + #[sea_orm(string_value = "BreakfastTea")] + BreakfastTea, +} diff --git a/issues/1143/src/main.rs b/issues/1143/src/main.rs new file mode 100644 index 00000000..197adabe --- /dev/null +++ b/issues/1143/src/main.rs @@ -0,0 +1,4 @@ +mod entity; + +#[tokio::main] +async fn main() {} diff --git a/issues/249/app/Cargo.toml b/issues/249/app/Cargo.toml index 74f2b990..08341381 100644 --- a/issues/249/app/Cargo.toml +++ b/issues/249/app/Cargo.toml @@ -6,4 +6,4 @@ publish = false [dependencies] core = { path = "../core" } -sea-orm = { path = "../../../", default-features = false, features = ["macros", "sqlx-sqlite", "runtime-async-std-native-tls"] } +sea-orm = { path = "../../../", default-features = false, features = ["macros", "tests-cfg", "sqlx-sqlite", "runtime-async-std-native-tls"] } diff --git a/issues/892/Cargo.toml b/issues/892/Cargo.toml index cb87d22a..935e9e99 100644 --- a/issues/892/Cargo.toml +++ b/issues/892/Cargo.toml @@ -13,5 +13,5 @@ tokio = { version = "1.20.0", features = ["rt-multi-thread", "macros"] } [dependencies.sea-orm] path = "../../" # remove this line in your own project -features = ["runtime-tokio-rustls", "sqlx-sqlite", "macros"] +features = ["runtime-tokio-rustls", "tests-cfg", "sqlx-sqlite", "macros"] default-features = false diff --git a/sea-orm-macros/src/derives/active_enum.rs b/sea-orm-macros/src/derives/active_enum.rs index ee479171..5e6b27f0 100644 --- a/sea-orm-macros/src/derives/active_enum.rs +++ b/sea-orm-macros/src/derives/active_enum.rs @@ -250,14 +250,25 @@ impl ActiveEnum { .collect(); quote!( - #[derive(Debug, Clone, PartialEq, Eq, EnumIter, Iden)] + #[derive(Debug, Clone, PartialEq, Eq, sea_orm::EnumIter)] pub enum #enum_variant_iden { #( - #[iden = #str_variants] #enum_variants, )* } + #[automatically_derived] + impl sea_orm::sea_query::Iden for #enum_variant_iden { + fn unquoted(&self, s: &mut dyn std::fmt::Write) { + write!(s, "{}", match self { + #( + Self::#enum_variants => #str_variants, + )* + }).unwrap(); + } + } + + #[automatically_derived] impl #ident { pub fn iden_values() -> Vec { <#enum_variant_iden as sea_orm::strum::IntoEnumIterator>::iter() @@ -271,10 +282,16 @@ impl ActiveEnum { }; quote!( - #[derive(Debug, Clone, PartialEq, Eq, Iden)] - #[iden = #enum_name] + #[derive(Debug, Clone, PartialEq, Eq)] pub struct #enum_name_iden; + #[automatically_derived] + impl sea_orm::sea_query::Iden for #enum_name_iden { + fn unquoted(&self, s: &mut dyn std::fmt::Write) { + write!(s, "{}", #enum_name).unwrap(); + } + } + #impl_enum_variant_iden #[automatically_derived] @@ -357,7 +374,7 @@ impl ActiveEnum { #[automatically_derived] impl std::fmt::Display for #ident { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let v: sea_orm::sea_query::Value = self.to_value().into(); + let v: sea_orm::sea_query::Value = ::to_value(&self).into(); write!(f, "{}", v) } } diff --git a/sea-orm-macros/src/lib.rs b/sea-orm-macros/src/lib.rs index ef77c1e4..da80b062 100644 --- a/sea-orm-macros/src/lib.rs +++ b/sea-orm-macros/src/lib.rs @@ -539,6 +539,19 @@ pub fn derive_active_model_behavior(input: TokenStream) -> TokenStream { /// - For `string_value`, value should be passed as string, i.e. `string_value = "A"` /// - For `num_value`, value should be passed as integer, i.e. `num_value = 1` or `num_value = 1i32` /// - Note that only one of it can be specified, and all variants of an enum have to annotate with the same `*_value` macro attribute +/// +/// # Usage +/// +/// ``` +/// use sea_orm::{entity::prelude::*, DeriveActiveEnum}; +/// +/// #[derive(EnumIter, DeriveActiveEnum)] +/// #[sea_orm(rs_type = "i32", db_type = "Integer")] +/// pub enum Color { +/// Black = 0, +/// White = 1, +/// } +/// ``` #[proc_macro_derive(DeriveActiveEnum, attributes(sea_orm))] pub fn derive_active_enum(input: TokenStream) -> TokenStream { let input = parse_macro_input!(input as DeriveInput);