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

feat: allow for usage of skip while using DerivePartialModel (analagous to FromQueryResult) #2167

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 32 additions & 12 deletions sea-orm-macros/src/derives/partial_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@ use syn::Expr;

use syn::Meta;

use self::util::GetAsKVMeta;
use self::util::{GetAsKVMeta, GetMeta};

#[derive(Debug)]
enum Error {
InputNotStruct,
EntityNotSpecific,
EntityNotSpecified,
NotSupportGeneric(Span),
BothFromColAndFromExpr(Span),
MultipleSourcesSpecified(Span),
Syn(syn::Error),
}

#[derive(Debug, PartialEq, Eq)]
enum ColumnAs {
/// column in the model
Expand Down Expand Up @@ -78,6 +79,7 @@ impl DerivePartialModel {

let mut from_col = None;
let mut from_expr = None;
let mut skip = false;

for attr in field.attrs.iter() {
if !attr.path().is_ident("sea_orm") {
Expand All @@ -94,16 +96,21 @@ impl DerivePartialModel {
.get_as_kv("from_expr")
.map(|s| syn::parse_str::<Expr>(&s).map_err(Error::Syn))
.transpose()?;
skip = meta.exists("skip");
}
}
}

if skip {
continue;
}

let field_name = field.ident.unwrap();

let col_as = match (from_col, from_expr) {
(None, None) => {
if entity.is_none() {
return Err(Error::EntityNotSpecific);
return Err(Error::EntityNotSpecified);
}
ColumnAs::Col(format_ident!(
"{}",
Expand All @@ -116,13 +123,13 @@ impl DerivePartialModel {
},
(Some(col), None) => {
if entity.is_none() {
return Err(Error::EntityNotSpecific);
return Err(Error::EntityNotSpecified);
}

let field = field_name.to_string();
ColumnAs::ColAlias { col, field }
}
(Some(_), Some(_)) => return Err(Error::BothFromColAndFromExpr(field_span)),
(Some(_), Some(_)) => return Err(Error::MultipleSourcesSpecified(field_span)),
};
column_as_list.push(col_as);
}
Expand Down Expand Up @@ -179,16 +186,16 @@ pub fn expand_derive_partial_model(input: syn::DeriveInput) -> syn::Result<Token
match DerivePartialModel::new(input) {
Ok(partial_model) => partial_model.expand(),
Err(Error::NotSupportGeneric(span)) => Ok(quote_spanned! {
span => compile_error!("you can only derive `DerivePartialModel` on named struct");
span => compile_error!("you can only derive `DerivePartialModel` on named, non-generic structs");
}),
Err(Error::BothFromColAndFromExpr(span)) => Ok(quote_spanned! {
span => compile_error!("you can only use one of `from_col` or `from_expr`");
Err(Error::MultipleSourcesSpecified(span)) => Ok(quote_spanned! {
span => compile_error!("you can only use one of `from_col`, `from_expr` or `skip`");
}),
Err(Error::EntityNotSpecific) => Ok(quote_spanned! {
ident_span => compile_error!("you need specific which entity you are using")
Err(Error::EntityNotSpecified) => Ok(quote_spanned! {
ident_span => compile_error!("you need to specify which entity you are using")
}),
Err(Error::InputNotStruct) => Ok(quote_spanned! {
ident_span => compile_error!("you can only derive `DerivePartialModel` on named struct");
ident_span => compile_error!("you can only derive `DerivePartialModel` on named structs");
}),
Err(Error::Syn(err)) => Err(err),
}
Expand Down Expand Up @@ -223,6 +230,19 @@ mod util {
}
}
}

pub(super) trait GetMeta {
fn exists(&self, k: &str) -> bool;
}

impl GetMeta for Meta {
fn exists(&self, k: &str) -> bool {
let Meta::Path(path) = self else {
return false;
};
path.is_ident(k)
}
}
}

#[cfg(test)]
Expand Down
9 changes: 9 additions & 0 deletions tests/partial_model_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,12 @@ struct FieldFromExpr {
#[sea_orm(from_expr = "Expr::col(Column::Id).equals(Column::Foo)")]
_bar: bool,
}

#[derive(FromQueryResult, DerivePartialModel)]
#[sea_orm(entity = "Entity")]
struct SkipField {
#[sea_orm(from_col = "foo2")]
_foo: i32,
#[sea_orm(skip)]
_bar: Option<()>,
}