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

Serialize time types as serde_json::Value #1042

Merged
merged 6 commits into from
Oct 17, 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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ default = [
]
macros = ["sea-orm-macros"]
mock = []
with-json = ["serde_json", "sea-query/with-json", "chrono/serde", "sqlx?/json"]
with-json = ["serde_json", "sea-query/with-json", "chrono/serde", "time/serde", "sqlx?/json"]
with-chrono = ["chrono", "sea-query/with-chrono", "sqlx?/chrono"]
with-rust_decimal = ["rust_decimal", "sea-query/with-rust_decimal", "sqlx?/decimal"]
with-uuid = ["uuid", "sea-query/with-uuid", "sqlx?/uuid"]
Expand Down
17 changes: 17 additions & 0 deletions src/entity/active_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,7 @@ impl_into_active_value!(f32);
impl_into_active_value!(f64);
impl_into_active_value!(&'static str);
impl_into_active_value!(String);
impl_into_active_value!(Vec<u8>);

#[cfg(feature = "with-json")]
#[cfg_attr(docsrs, doc(cfg(feature = "with-json")))]
Expand Down Expand Up @@ -719,6 +720,22 @@ impl_into_active_value!(crate::prelude::Decimal);
#[cfg_attr(docsrs, doc(cfg(feature = "with-uuid")))]
impl_into_active_value!(crate::prelude::Uuid);

#[cfg(feature = "with-time")]
#[cfg_attr(docsrs, doc(cfg(feature = "with-time")))]
impl_into_active_value!(crate::prelude::TimeDate);

#[cfg(feature = "with-time")]
#[cfg_attr(docsrs, doc(cfg(feature = "with-time")))]
impl_into_active_value!(crate::prelude::TimeTime);

#[cfg(feature = "with-time")]
#[cfg_attr(docsrs, doc(cfg(feature = "with-time")))]
impl_into_active_value!(crate::prelude::TimeDateTime);

#[cfg(feature = "with-time")]
#[cfg_attr(docsrs, doc(cfg(feature = "with-time")))]
impl_into_active_value!(crate::prelude::TimeDateTimeWithTimeZone);

impl<V> Default for ActiveValue<V>
where
V: Into<Value>,
Expand Down
16 changes: 16 additions & 0 deletions src/query/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ impl FromQueryResult for JsonValue {
match_mysql_type!(chrono::NaiveDateTime);
#[cfg(feature = "with-chrono")]
match_mysql_type!(chrono::DateTime<chrono::Utc>);
#[cfg(feature = "with-time")]
match_mysql_type!(time::Date);
#[cfg(feature = "with-time")]
match_mysql_type!(time::Time);
#[cfg(feature = "with-time")]
match_mysql_type!(time::PrimitiveDateTime);
#[cfg(feature = "with-time")]
match_mysql_type!(time::OffsetDateTime);
#[cfg(feature = "with-rust_decimal")]
match_mysql_type!(rust_decimal::Decimal);
#[cfg(feature = "with-json")]
Expand Down Expand Up @@ -106,6 +114,14 @@ impl FromQueryResult for JsonValue {
match_postgres_type!(chrono::NaiveDateTime);
#[cfg(feature = "with-chrono")]
match_postgres_type!(chrono::DateTime<chrono::FixedOffset>);
#[cfg(feature = "with-time")]
match_postgres_type!(time::Date);
#[cfg(feature = "with-time")]
match_postgres_type!(time::Time);
#[cfg(feature = "with-time")]
match_postgres_type!(time::PrimitiveDateTime);
#[cfg(feature = "with-time")]
match_postgres_type!(time::OffsetDateTime);
#[cfg(feature = "with-rust_decimal")]
match_postgres_type!(rust_decimal::Decimal);
#[cfg(feature = "with-json")]
Expand Down
27 changes: 27 additions & 0 deletions tests/time_crate_tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub mod common;
pub use common::{features::*, setup::*, TestContext};
use sea_orm::{entity::prelude::*, DatabaseConnection, IntoActiveModel};
use serde_json::json;
use time::macros::{date, time};

#[sea_orm_macros::test]
Expand Down Expand Up @@ -42,5 +43,31 @@ pub async fn create_transaction_log(db: &DatabaseConnection) -> Result<(), DbErr
Some(transaction_log.clone())
);

let json = TransactionLog::find().into_json().one(db).await?.unwrap();

#[cfg(feature = "sqlx-postgres")]
assert_eq!(
json,
json!({
"id": 1,
"date": "2022-03-13",
"time": "16:24:00",
"date_time": "2022-03-13T16:24:00",
Copy link
Member

Choose a reason for hiding this comment

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

I suspect we'd have the sub-second component?

"date_time_tz": "2022-03-13T16:24:00+00:00",
})
);

#[cfg(feature = "sqlx-mysql")]
assert_eq!(
json,
json!({
"id": 1,
"date": "2022-03-13",
"time": "16:24:00",
"date_time": "2022-03-13T16:24:00",
"date_time_tz": "2022-03-13T16:24:00Z",
})
);

Ok(())
}
66 changes: 66 additions & 0 deletions tests/type_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
pub mod common;

use sea_orm::{IntoActiveValue, TryFromU64, TryGetable, Value};

/*

When supporting a new type in SeaORM we should implement the following traits for it:
- `IntoActiveValue`, given that it implemented `Into<Value>` already
- `TryGetable`
- `TryFromU64`

Also, we need to update `impl FromQueryResult for JsonValue` at `src/query/json.rs`
to correctly serialize the type as `serde_json::Value`.

*/

pub fn it_impl_into_active_value<T: IntoActiveValue<V>, V: Into<Value>>() {}

pub fn it_impl_try_getable<T: TryGetable>() {}

pub fn it_impl_try_from_u64<T: TryFromU64>() {}

#[allow(unused_macros)]
macro_rules! it_impl_traits {
Copy link
Member

Choose a reason for hiding this comment

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

This is smart

( $ty: ty ) => {
it_impl_into_active_value::<$ty, $ty>();
it_impl_into_active_value::<Option<$ty>, Option<$ty>>();
it_impl_into_active_value::<Option<Option<$ty>>, Option<$ty>>();

it_impl_try_getable::<$ty>();
it_impl_try_getable::<Option<$ty>>();

it_impl_try_from_u64::<$ty>();
};
}

#[sea_orm_macros::test]
#[cfg(feature = "sqlx-dep")]
fn main() {
it_impl_traits!(i8);
it_impl_traits!(i16);
it_impl_traits!(i32);
it_impl_traits!(i64);
it_impl_traits!(u8);
it_impl_traits!(u16);
it_impl_traits!(u32);
it_impl_traits!(u64);
it_impl_traits!(bool);
it_impl_traits!(f32);
it_impl_traits!(f64);
it_impl_traits!(Vec<u8>);
it_impl_traits!(String);
it_impl_traits!(serde_json::Value);
it_impl_traits!(chrono::NaiveDate);
it_impl_traits!(chrono::NaiveTime);
it_impl_traits!(chrono::NaiveDateTime);
it_impl_traits!(chrono::DateTime<chrono::FixedOffset>);
it_impl_traits!(chrono::DateTime<chrono::Utc>);
it_impl_traits!(chrono::DateTime<chrono::Local>);
it_impl_traits!(time::Date);
it_impl_traits!(time::Time);
it_impl_traits!(time::PrimitiveDateTime);
it_impl_traits!(time::OffsetDateTime);
it_impl_traits!(rust_decimal::Decimal);
it_impl_traits!(uuid::Uuid);
}