diff --git a/Cargo.toml b/Cargo.toml index fc5dffa02..4bae24805 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" } @@ -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 = [] diff --git a/sea-orm-macros/Cargo.toml b/sea-orm-macros/Cargo.toml index 18de55760..891ae3130 100644 --- a/sea-orm-macros/Cargo.toml +++ b/sea-orm-macros/Cargo.toml @@ -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"] } diff --git a/sea-orm-macros/src/derives/active_model.rs b/sea-orm-macros/src/derives/active_model.rs index 975eabfd7..608eb6a39 100644 --- a/sea-orm-macros/src/derives/active_model.rs +++ b/sea-orm-macros/src/derives/active_model.rs @@ -70,11 +70,12 @@ fn derive_active_model(all_fields: IntoIter) -> syn::Result 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> ),* } diff --git a/sea-orm-migration/Cargo.toml b/sea-orm-migration/Cargo.toml index ca423948b..6d3ea7f94 100644 --- a/sea-orm-migration/Cargo.toml +++ b/sea-orm-migration/Cargo.toml @@ -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"] } diff --git a/src/entity/active_model.rs b/src/entity/active_model.rs index 656fc1853..e89d142ee 100644 --- a/src/entity/active_model.rs +++ b/src/entity/active_model.rs @@ -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; @@ -36,6 +38,7 @@ pub use ActiveValue::NotSet; /// ); /// ``` #[derive(Clone, Debug)] +#[cfg_attr(feature = "with-json", derive(Serialize, Deserialize))] pub enum ActiveValue where V: Into, diff --git a/src/executor/query.rs b/src/executor/query.rs index 28093822b..b8ab9d522 100644 --- a/src/executor/query.rs +++ b/src/executor/query.rs @@ -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)] @@ -531,6 +535,50 @@ impl TryGetable for BigDecimal { } } +#[cfg(feature = "with-ipnetwork")] +impl TryGetable for IpNetwork { + #[allow(unused_variables)] + fn try_get_by(res: &QueryResult, idx: I) -> Result { + match &res.row { + #[cfg(feature = "sqlx-postgres")] + QueryResultRow::SqlxPostgres(row) => row + .try_get::, _>(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(res: &QueryResult, idx: I) -> Result { + match &res.row { + #[cfg(feature = "sqlx-postgres")] + QueryResultRow::SqlxPostgres(row) => row + .try_get::, _>(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 ) => {