Skip to content

Commit

Permalink
Merge pull request #1 from billy1624/pr/2167
Browse files Browse the repository at this point in the history
WIP
  • Loading branch information
jreppnow authored May 29, 2024
2 parents 03c1130 + 64127fb commit b8b6ce9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 18 deletions.
48 changes: 31 additions & 17 deletions sea-orm-macros/src/derives/partial_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use syn::Expr;

use syn::Meta;

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

#[derive(Debug)]
enum Error {
Expand Down Expand Up @@ -96,39 +96,40 @@ impl DerivePartialModel {
.get_as_kv("from_expr")
.map(|s| syn::parse_str::<Expr>(&s).map_err(Error::Syn))
.transpose()?;
skip = meta.get_as_kv("skip").is_some();
skip = meta.exists("skip");
}
}
}

if skip {
continue;
}

let field_name = field.ident.unwrap();

let col_as = match (from_col, from_expr, skip) {
(Some(col), None, false) => {
let col_as = match (from_col, from_expr) {
(None, None) => {
if entity.is_none() {
return Err(Error::EntityNotSpecified);
}

let field = field_name.to_string();
ColumnAs::ColAlias { col, field }
ColumnAs::Col(format_ident!(
"{}",
field_name.to_string().to_upper_camel_case()
))
}
(None, Some(expr), false) => ColumnAs::Expr {
(None, Some(expr)) => ColumnAs::Expr {
expr,
field_name: field_name.to_string(),
},
(None, None, true) => {
continue;
}
(None, None, false) => {
(Some(col), None) => {
if entity.is_none() {
return Err(Error::EntityNotSpecified);
}
ColumnAs::Col(format_ident!(
"{}",
field_name.to_string().to_upper_camel_case()
))

let field = field_name.to_string();
ColumnAs::ColAlias { col, field }
}
_ => return Err(Error::MultipleSourcesSpecified(field_span)),
(Some(_), Some(_)) => return Err(Error::MultipleSourcesSpecified(field_span)),
};
column_as_list.push(col_as);
}
Expand Down Expand Up @@ -229,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
3 changes: 2 additions & 1 deletion tests/partial_model_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ struct FieldFromExpr {
}

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

0 comments on commit b8b6ce9

Please sign in to comment.