Skip to content

Commit

Permalink
WIP: Add support for root arrays in JSON in SeaORM SeaQL#1517
Browse files Browse the repository at this point in the history
  • Loading branch information
anshap1719 committed Aug 22, 2023
1 parent af52e86 commit a6893f8
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 14 deletions.
11 changes: 11 additions & 0 deletions sea-orm-macros/src/derives/try_getable_from_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,14 @@ pub fn expand_derive_from_json_query_result(ident: Ident) -> syn::Result<TokenSt
}
))
}

pub fn expand_derive_from_json_array_query_result(ident: Ident) -> syn::Result<TokenStream> {
let derive_try_getable_from_json_expansion = expand_derive_from_json_query_result(ident.clone());

Ok(quote!(
#derive_try_getable_from_json_expansion

#[automatically_derived]
impl sea_orm::sea_query::with_array::NotU8 for #ident {}
))
}
11 changes: 11 additions & 0 deletions sea-orm-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,17 @@ pub fn derive_from_json_query_result(input: TokenStream) -> TokenStream {
}
}

#[cfg(feature = "derive")]
#[proc_macro_derive(FromJsonArrayQueryResult)]
pub fn derive_from_json_array_query_result(input: TokenStream) -> TokenStream {
let DeriveInput { ident, .. } = parse_macro_input!(input);

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

/// The DerivePartialModel derive macro will implement `sea_orm::PartialModelTrait` for simplify partial model queries.
///
/// ## Usage
Expand Down
24 changes: 12 additions & 12 deletions src/entity/active_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,18 +144,18 @@ pub trait ActiveEnum: Sized + Iterable {
}
}

impl<T> TryGetable for Vec<T>
where
T: ActiveEnum,
T::ValueVec: TryGetable,
{
fn try_get_by<I: crate::ColIdx>(res: &QueryResult, index: I) -> Result<Self, TryGetError> {
<T::ValueVec as TryGetable>::try_get_by(res, index)?
.into_iter()
.map(|value| T::try_from_value(&value).map_err(Into::into))
.collect()
}
}
// impl<T> TryGetable for Vec<T>
// where
// T: ActiveEnum,
// T::ValueVec: TryGetable,
// {
// fn try_get_by<I: crate::ColIdx>(res: &QueryResult, index: I) -> Result<Self, TryGetError> {
// <T::ValueVec as TryGetable>::try_get_by(res, index)?
// .into_iter()
// .map(|value| T::try_from_value(&value).map_err(Into::into))
// .collect()
// }
// }

impl<T> TryFromU64 for T
where
Expand Down
3 changes: 3 additions & 0 deletions src/executor/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,9 @@ where
}
}

#[cfg(feature = "with-json")]
impl<T> TryGetableFromJson for Vec<T> where T: TryGetableFromJson {}

#[cfg(feature = "with-json")]
impl<T> TryGetable for T
where
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,11 +365,11 @@ pub use sea_orm_macros::{
DeriveActiveEnum, DeriveActiveModel, DeriveActiveModelBehavior, DeriveColumn,
DeriveCustomColumn, DeriveDisplay, DeriveEntity, DeriveEntityModel, DeriveIden,
DeriveIntoActiveModel, DeriveMigrationName, DeriveModel, DerivePartialModel, DerivePrimaryKey,
DeriveRelatedEntity, DeriveRelation, DeriveValueType, FromJsonQueryResult, FromQueryResult,
DeriveRelatedEntity, DeriveRelation, DeriveValueType, FromJsonQueryResult, FromJsonArrayQueryResult, FromQueryResult,
};

pub use sea_query;
pub use sea_query::Iden;

pub use sea_orm_macros::EnumIter;
pub use strum;
pub use strum;

0 comments on commit a6893f8

Please sign in to comment.