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

WIP: Add Serialize/Deserialize for ActiveValue #1631

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ uuid = { version = "1", default-features = false, optional = true }
ouroboros = { version = "0.15", default-features = false }
url = { version = "2.2", default-features = false }
thiserror = { version = "1", default-features = false }
ipnetwork = {version = "0.19", default-features = false, optional = true}
mac_address = { version = "1.1.5", default-features = false, optional = true}

[dev-dependencies]
smol = { version = "1.2" }
Expand Down Expand Up @@ -76,12 +78,14 @@ default = [
]
macros = ["sea-orm-macros/derive", "sea-query/derive"]
mock = []
with-json = ["serde_json", "sea-query/with-json", "chrono?/serde", "time?/serde", "uuid?/serde", "sea-query-binder?/with-json", "sqlx?/json"]
with-json = ["serde_json", "sea-query/with-json", "chrono?/serde", "time?/serde", "uuid?/serde", "sea-query-binder?/with-json", "sqlx?/json", "serde/derive", "ipnetwork?/serde", "mac_address?/serde"]
with-chrono = ["chrono", "sea-query/with-chrono", "sea-query-binder?/with-chrono", "sqlx?/chrono"]
with-rust_decimal = ["rust_decimal", "sea-query/with-rust_decimal", "sea-query-binder?/with-rust_decimal", "sqlx?/decimal"]
with-bigdecimal = ["bigdecimal", "sea-query/with-bigdecimal", "sea-query-binder?/with-bigdecimal", "sqlx?/bigdecimal"]
with-uuid = ["uuid", "sea-query/with-uuid", "sea-query-binder?/with-uuid", "sqlx?/uuid"]
with-time = ["time", "sea-query/with-time", "sea-query-binder?/with-time", "sqlx?/time"]
with-ipnetwork = ["sea-query/with-ipnetwork", "ipnetwork", "sea-query-binder/with-ipnetwork"]
with-mac_address = ["mac_address", "sea-query/with-mac_address", "sea-query-binder?/with-mac_address"]
postgres-array = ["sea-query/postgres-array", "sea-query-binder?/postgres-array", "sea-orm-macros/postgres-array"]
sea-orm-internal = []
sqlx-dep = []
Expand Down
1 change: 1 addition & 0 deletions sea-orm-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ quote = { version = "1", default-features = false }
heck = { version = "0.4", default-features = false }
proc-macro2 = { version = "1", default-features = false }
unicode-ident = { version = "1" }
serde = { version = "1.0", features = ["derive"] }

[dev-dependencies]
sea-orm = { path = "../", features = ["macros", "tests-cfg"] }
Expand Down
3 changes: 2 additions & 1 deletion sea-orm-macros/src/derives/active_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,12 @@ fn derive_active_model(all_fields: IntoIter<Field>) -> syn::Result<TokenStream>

Ok(quote!(
#[doc = " Generated by sea-orm-macros"]
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct ActiveModel {

#(
#[doc = " Generated by sea-orm-macros"]
#[serde(default)]
pub #field: sea_orm::ActiveValue<#ty>
),*
}
Expand Down
1 change: 1 addition & 0 deletions sea-orm-migration/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ sea-schema = { version = "0.12.0-rc.2" }
tracing = { version = "0.1", default-features = false, features = ["log"] }
tracing-subscriber = { version = "0.3.17", default-features = false, features = ["env-filter", "fmt"] }
futures = { version = "0.3", default-features = false, features = ["std"] }
serde = { version = "1.0", features = ["derive"] }

[dev-dependencies]
async-std = { version = "1", features = ["attributes", "tokio1"] }
Expand Down
3 changes: 3 additions & 0 deletions src/entity/active_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use crate::{
};
use async_trait::async_trait;
use sea_query::{Nullable, ValueTuple};
use serde::{Deserialize, Serialize};

use std::fmt::Debug;

pub use ActiveValue::NotSet;
Expand Down Expand Up @@ -36,6 +38,7 @@ pub use ActiveValue::NotSet;
/// );
/// ```
#[derive(Clone, Debug)]
#[cfg_attr(feature = "with-json", derive(Serialize, Deserialize))]
Copy link
Member

@tyt2y3 tyt2y3 Nov 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can use sea_query's value_to_json to handle the Serialize part, but in general ActiveValue does not seem to lend itself for Deserialize, because the trait bound only requires T -> Value.

pub enum ActiveValue<V>
where
V: Into<Value>,
Expand Down
48 changes: 48 additions & 0 deletions src/executor/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ use crate::debug_print;
use crate::driver::*;
#[cfg(feature = "sqlx-dep")]
use sqlx::Row;
#[cfg(feature = "with-ipnetwork")]
use ipnetwork::IpNetwork;
#[cfg(feature = "with-mac_address")]
use mac_address::MacAddress;

/// Defines the result of a query operation on a Model
#[derive(Debug)]
Expand Down Expand Up @@ -531,6 +535,50 @@ impl TryGetable for BigDecimal {
}
}

#[cfg(feature = "with-ipnetwork")]
impl TryGetable for IpNetwork {
#[allow(unused_variables)]
fn try_get_by<I: ColIdx>(res: &QueryResult, idx: I) -> Result<Self, TryGetError> {
match &res.row {
#[cfg(feature = "sqlx-postgres")]
QueryResultRow::SqlxPostgres(row) => row
.try_get::<Option<IpNetwork>, _>(idx.as_sqlx_postgres_index())
.map_err(|e| sqlx_error_to_query_err(e).into())
.and_then(|opt| opt.ok_or_else(|| err_null_idx_col(idx))),
#[cfg(feature = "mock")]
#[allow(unused_variables)]
QueryResultRow::Mock(row) => row.try_get(idx).map_err(|e| {
debug_print!("{:#?}", e.to_string());
err_null_idx_col(idx)
}),
#[allow(unreachable_patterns)]
_ => unreachable!(),
}
}
}

#[cfg(feature = "with-mac_address")]
impl TryGetable for MacAddress {
#[allow(unused_variables)]
fn try_get_by<I: ColIdx>(res: &QueryResult, idx: I) -> Result<Self, TryGetError> {
match &res.row {
#[cfg(feature = "sqlx-postgres")]
QueryResultRow::SqlxPostgres(row) => row
.try_get::<Option<MacAddress>, _>(idx.as_sqlx_postgres_index())
.map_err(|e| sqlx_error_to_query_err(e).into())
.and_then(|opt| opt.ok_or_else(|| err_null_idx_col(idx))),
#[cfg(feature = "mock")]
#[allow(unused_variables)]
QueryResultRow::Mock(row) => row.try_get(idx).map_err(|e| {
debug_print!("{:#?}", e.to_string());
err_null_idx_col(idx)
}),
#[allow(unreachable_patterns)]
_ => unreachable!(),
}
}
}

#[allow(unused_macros)]
macro_rules! try_getable_uuid {
( $type: ty, $conversion_fn: expr ) => {
Expand Down