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

Blanket IntoActiveValue implementations so custom db types are supported #833

Merged
merged 3 commits into from
Oct 11, 2022
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
42 changes: 24 additions & 18 deletions src/entity/active_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -641,31 +641,37 @@ where
fn into_active_value(self) -> ActiveValue<V>;
}

impl<V> IntoActiveValue<Option<V>> for Option<V>
where
V: IntoActiveValue<V> + Into<Value> + Nullable,
{
fn into_active_value(self) -> ActiveValue<Option<V>> {
match self {
Some(value) => Set(Some(value)),
None => NotSet,
}
}
}

impl<V> IntoActiveValue<Option<V>> for Option<Option<V>>
where
V: IntoActiveValue<V> + Into<Value> + Nullable,
{
fn into_active_value(self) -> ActiveValue<Option<V>> {
match self {
Some(value) => Set(value),
None => NotSet,
}
}
}

macro_rules! impl_into_active_value {
($ty: ty) => {
impl IntoActiveValue<$ty> for $ty {
fn into_active_value(self) -> ActiveValue<$ty> {
Set(self)
}
}

impl IntoActiveValue<Option<$ty>> for Option<$ty> {
fn into_active_value(self) -> ActiveValue<Option<$ty>> {
match self {
Some(value) => Set(Some(value)),
None => NotSet,
}
}
}

impl IntoActiveValue<Option<$ty>> for Option<Option<$ty>> {
fn into_active_value(self) -> ActiveValue<Option<$ty>> {
match self {
Some(value) => Set(value),
None => NotSet,
}
}
}
};
}

Expand Down
40 changes: 40 additions & 0 deletions tests/common/features/custom_active_model.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use super::sea_orm_active_enums::*;
use sea_orm::entity::prelude::*;
use sea_orm::{ActiveValue, IntoActiveValue};

#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[cfg_attr(feature = "sqlx-postgres", sea_orm(schema_name = "public"))]
#[sea_orm(table_name = "custom_active_model")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
pub weight: Option<f32>,
pub amount: Option<i32>,
pub category: Option<Category>,
pub color: Option<Color>,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}

impl ActiveModelBehavior for ActiveModel {}

#[derive(Clone, Debug, PartialEq, DeriveIntoActiveModel)]
pub struct CustomActiveModel {
pub weight: Option<f32>,
pub amount: Option<Option<i32>>,
pub category: Option<Category>,
pub color: Option<Option<Color>>,
}

impl IntoActiveValue<Category> for Category {
fn into_active_value(self) -> ActiveValue<Category> {
ActiveValue::set(self)
}
}

impl IntoActiveValue<Color> for Color {
fn into_active_value(self) -> ActiveValue<Color> {
ActiveValue::set(self)
}
}
1 change: 1 addition & 0 deletions tests/common/features/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub mod active_enum;
pub mod active_enum_child;
pub mod applog;
pub mod byte_primary_key;
pub mod custom_active_model;
pub mod insert_default;
pub mod json_struct;
pub mod json_vec;
Expand Down