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

(de)serialize custom JSON types - 2 #794

Merged
merged 3 commits into from
Jun 30, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 2 additions & 0 deletions sea-orm-macros/src/derives/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod into_active_model;
mod model;
mod primary_key;
mod relation;
mod try_getable_from_json;

pub use active_enum::*;
pub use active_model::*;
Expand All @@ -21,3 +22,4 @@ pub use into_active_model::*;
pub use model::*;
pub use primary_key::*;
pub use relation::*;
pub use try_getable_from_json::*;
44 changes: 44 additions & 0 deletions sea-orm-macros/src/derives/try_getable_from_json.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use proc_macro2::{Ident, TokenStream};
use quote::{format_ident, quote, quote_spanned};
use syn::{ext::IdentExt, Data, DataStruct, Field, Fields};

pub fn expand_derive_try_getable_from_json(ident: Ident, data: Data) -> syn::Result<TokenStream> {
Ok(quote!(
#[automatically_derived]
impl sea_orm::TryGetableFromJson for #ident {}

#[automatically_derived]
impl std::convert::From<#ident> for sea_orm::Value {
fn from(source: #ident) -> Self {
sea_orm::Value::Json(serde_json::to_value(&source).ok().map(|s| std::boxed::Box::new(s)))
}
}

#[automatically_derived]
impl sea_query::ValueType for #ident {
fn try_from(v: sea_orm::Value) -> Result<Self, sea_orm::sea_query::ValueTypeErr> {
match v {
sea_orm::Value::Json(Some(json)) => Ok(
serde_json::from_value(*json).map_err(|_| sea_orm::sea_query::ValueTypeErr)?,
),
_ => Err(sea_orm::sea_query::ValueTypeErr),
}
}

fn type_name() -> String {
stringify!(#ident).to_owned()
}

fn column_type() -> sea_orm::sea_query::ColumnType {
sea_orm::sea_query::ColumnType::Json
}
}

#[automatically_derived]
impl sea_orm::sea_query::Nullable for #ident {
fn null() -> sea_orm::Value {
sea_orm::Value::Json(None)
}
}
))
}
10 changes: 10 additions & 0 deletions sea-orm-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,16 @@ pub fn derive_relation(input: TokenStream) -> TokenStream {
.into()
}

#[proc_macro_derive(DeriveTryGetableFromJson)]
pub fn derive_try_getable_from_json(input: TokenStream) -> TokenStream {
let DeriveInput { ident, data, .. } = parse_macro_input!(input);

match derives::expand_derive_try_getable_from_json(ident, data) {
Ok(ts) => ts.into(),
Err(e) => e.to_compile_error().into(),
}
}

#[doc(hidden)]
#[proc_macro_attribute]
pub fn test(_: TokenStream, input: TokenStream) -> TokenStream {
Expand Down
2 changes: 1 addition & 1 deletion src/entity/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub use crate::{
pub use crate::{
DeriveActiveEnum, DeriveActiveModel, DeriveActiveModelBehavior, DeriveColumn,
DeriveCustomColumn, DeriveEntity, DeriveEntityModel, DeriveIntoActiveModel, DeriveModel,
DerivePrimaryKey, DeriveRelation,
DerivePrimaryKey, DeriveRelation, DeriveTryGetableFromJson,
};

#[cfg(feature = "with-json")]
Expand Down
60 changes: 60 additions & 0 deletions src/executor/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,66 @@ fn try_get_many_with_slice_len_of(len: usize, cols: &[String]) -> Result<(), Try
}
}

// TryGetableFromJson //

/// Perform a query on multiple columns
#[cfg(feature = "with-json")]
pub trait TryGetableFromJson: Sized
where
for<'de> Self: serde::Deserialize<'de>,
{
/// Ensure the type implements this method
fn try_get_from_json(res: &QueryResult, pre: &str, col: &str) -> Result<Self, TryGetError> {
let column = format!("{}{}", pre, col);
let res = match &res.row {
#[cfg(feature = "sqlx-mysql")]
QueryResultRow::SqlxMySql(row) => {
use sqlx::Row;
row.try_get::<Option<serde_json::Value>, _>(column.as_str())
.map_err(|e| TryGetError::DbErr(crate::sqlx_error_to_query_err(e)))
.and_then(|opt| opt.ok_or(TryGetError::Null))
}
#[cfg(feature = "sqlx-postgres")]
QueryResultRow::SqlxPostgres(row) => {
use sqlx::Row;
row.try_get::<Option<serde_json::Value>, _>(column.as_str())
.map_err(|e| TryGetError::DbErr(crate::sqlx_error_to_query_err(e)))
.and_then(|opt| opt.ok_or(TryGetError::Null))
}
#[cfg(feature = "sqlx-sqlite")]
QueryResultRow::SqlxSqlite(row) => {
use sqlx::Row;
row.try_get::<Option<serde_json::Value>, _>(column.as_str())
.map_err(|e| TryGetError::DbErr(crate::sqlx_error_to_query_err(e)))
.and_then(|opt| opt.ok_or(TryGetError::Null))
}
#[cfg(feature = "mock")]
QueryResultRow::Mock(row) => {
row.try_get::<serde_json::Value>(column.as_str())
.map_err(|e| {
debug_print!("{:#?}", e.to_string());
TryGetError::Null
})
}
#[allow(unreachable_patterns)]
_ => unreachable!(),
};
res.and_then(|json| {
serde_json::from_value(json).map_err(|e| TryGetError::DbErr(DbErr::Json(e.to_string())))
})
}
}

#[cfg(feature = "with-json")]
impl<T> TryGetable for T
where
T: TryGetableFromJson,
{
fn try_get(res: &QueryResult, pre: &str, col: &str) -> Result<Self, TryGetError> {
T::try_get_from_json(res, pre, col)
}
}

// TryFromU64 //
/// Try to convert a type to a u64
pub trait TryFromU64: Sized {
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ pub use schema::*;
pub use sea_orm_macros::{
DeriveActiveEnum, DeriveActiveModel, DeriveActiveModelBehavior, DeriveColumn,
DeriveCustomColumn, DeriveEntity, DeriveEntityModel, DeriveIntoActiveModel, DeriveModel,
DerivePrimaryKey, DeriveRelation, FromQueryResult,
DerivePrimaryKey, DeriveRelation, DeriveTryGetableFromJson, FromQueryResult,
};

pub use sea_query;
Expand Down
25 changes: 25 additions & 0 deletions tests/common/features/json_struct.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name = "json_struct")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
pub json: Json,
pub json_value: KeyValue,
pub json_value_opt: Option<KeyValue>,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, DeriveTryGetableFromJson)]
pub struct KeyValue {
pub id: i32,
pub name: String,
pub price: f32,
pub notes: Option<String>,
}

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

impl ActiveModelBehavior for ActiveModel {}
2 changes: 2 additions & 0 deletions tests/common/features/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub mod active_enum_child;
pub mod applog;
pub mod byte_primary_key;
pub mod insert_default;
pub mod json_struct;
pub mod json_vec;
pub mod metadata;
pub mod repository;
Expand All @@ -17,6 +18,7 @@ pub use active_enum_child::Entity as ActiveEnumChild;
pub use applog::Entity as Applog;
pub use byte_primary_key::Entity as BytePrimaryKey;
pub use insert_default::Entity as InsertDefault;
pub use json_struct::Entity as JsonStruct;
pub use json_vec::Entity as JsonVec;
pub use metadata::Entity as Metadata;
pub use repository::Entity as Repository;
Expand Down
23 changes: 23 additions & 0 deletions tests/common/features/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub async fn create_tables(db: &DatabaseConnection) -> Result<(), DbErr> {
create_satellites_table(db).await?;
create_transaction_log_table(db).await?;
create_json_vec_table(db).await?;
create_json_struct_table(db).await?;

let create_enum_stmts = match db_backend {
DbBackend::MySql | DbBackend::Sqlite => Vec::new(),
Expand Down Expand Up @@ -303,3 +304,25 @@ pub async fn create_json_vec_table(db: &DbConn) -> Result<ExecResult, DbErr> {

create_table(db, &create_table_stmt, JsonVec).await
}

pub async fn create_json_struct_table(db: &DbConn) -> Result<ExecResult, DbErr> {
let stmt = sea_query::Table::create()
.table(json_struct::Entity)
.col(
ColumnDef::new(json_struct::Column::Id)
.integer()
.not_null()
.auto_increment()
.primary_key(),
)
.col(ColumnDef::new(json_struct::Column::Json).json().not_null())
.col(
ColumnDef::new(json_struct::Column::JsonValue)
.json()
.not_null(),
)
.col(ColumnDef::new(json_struct::Column::JsonValueOpt).json())
.to_owned();

create_table(db, &stmt, JsonStruct).await
}
100 changes: 100 additions & 0 deletions tests/json_struct_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
pub mod common;

pub use common::{features::*, setup::*, TestContext};
use pretty_assertions::assert_eq;
use sea_orm::{entity::prelude::*, entity::*, DatabaseConnection};
use serde_json::json;

#[sea_orm_macros::test]
#[cfg(any(
feature = "sqlx-mysql",
feature = "sqlx-sqlite",
feature = "sqlx-postgres"
))]
async fn main() -> Result<(), DbErr> {
let ctx = TestContext::new("json_struct_tests").await;
create_tables(&ctx.db).await?;
insert_json_struct_1(&ctx.db).await?;
insert_json_struct_2(&ctx.db).await?;
ctx.delete().await;

Ok(())
}

pub async fn insert_json_struct_1(db: &DatabaseConnection) -> Result<(), DbErr> {
use json_struct::*;

let model = Model {
id: 1,
json: json!({
"id": 1,
"name": "apple",
"price": 12.01,
"notes": "hand picked, organic",
}),
json_value: KeyValue {
id: 1,
name: "apple".into(),
price: 12.01,
notes: Some("hand picked, organic".into()),
}
.into(),
json_value_opt: Some(KeyValue {
id: 1,
name: "apple".into(),
price: 12.01,
notes: Some("hand picked, organic".into()),
})
.into(),
};

let result = model.clone().into_active_model().insert(db).await?;

assert_eq!(result, model);

assert_eq!(
Entity::find()
.filter(Column::Id.eq(model.id))
.one(db)
.await?,
Some(model)
);

Ok(())
}

pub async fn insert_json_struct_2(db: &DatabaseConnection) -> Result<(), DbErr> {
use json_struct::*;

let model = Model {
id: 2,
json: json!({
"id": 2,
"name": "orange",
"price": 10.93,
"notes": "sweet & juicy",
}),
json_value: KeyValue {
id: 1,
name: "orange".into(),
price: 10.93,
notes: None,
}
.into(),
json_value_opt: None.into(),
};

let result = model.clone().into_active_model().insert(db).await?;

assert_eq!(result, model);

assert_eq!(
Entity::find()
.filter(Column::Id.eq(model.id))
.one(db)
.await?,
Some(model)
);

Ok(())
}