Skip to content

Commit

Permalink
skip on Selectable
Browse files Browse the repository at this point in the history
  • Loading branch information
Ten0 committed Oct 27, 2021
1 parent 7b773fb commit fe84094
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
6 changes: 5 additions & 1 deletion diesel_derives/src/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ impl Field {
self.flags.has_flag(flag)
}

pub fn has_skip_flag(&self) -> bool {
self.has_flag("skip")
}

pub fn ty_for_serialize(&self) -> Result<Option<syn::Type>, Diagnostic> {
if let Some(meta) = self.flags.nested_item("serialize_as")? {
let ty = meta.ty_value()?;
Expand All @@ -104,7 +108,7 @@ impl Field {
pub fn ty_for_deserialize(&self) -> Result<Option<Cow<syn::Type>>, Diagnostic> {
match (
self.flags.nested_item("deserialize_as")?,
self.has_flag("skip"),
self.has_skip_flag(),
) {
(Some(meta), false) => Ok(Some(Cow::Owned(meta.ty_value()?))),
(None, false) => Ok(Some(Cow::Borrowed(&self.ty))),
Expand Down
1 change: 1 addition & 0 deletions diesel_derives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,7 @@ pub fn derive_queryable_by_name(input: TokenStream) -> TokenStream {
/// * `#[diesel(embed)]`, specifies that the current field maps not only
/// single database column, but is a type that implements
/// `Selectable` on it's own
/// * `[diesel(skip)]`, field is ignored for the `Selectable` impl. Consistent with `Queryable`.
#[proc_macro_derive(Selectable, attributes(table_name, column_name, sql_type, diesel))]
pub fn derive_selectable(input: TokenStream) -> TokenStream {
expand_proc_macro(input, selectable::derive)
Expand Down
9 changes: 7 additions & 2 deletions diesel_derives/src/selectable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@ pub fn derive(item: syn::DeriveInput) -> Result<proc_macro2::TokenStream, Diagno

let struct_name = &item.ident;

let field_columns_ty = model.fields().iter().map(|f| field_column_ty(f, &model));
let fields = model
.fields()
.iter()
.filter(|f| !f.has_skip_flag())
.collect::<Vec<_>>();

let field_columns_inst = model.fields().iter().map(|f| field_column_inst(f, &model));
let field_columns_ty = fields.iter().map(|f| field_column_ty(f, &model));
let field_columns_inst = fields.iter().map(|f| field_column_inst(f, &model));

Ok(wrap_in_dummy_mod(quote! {
use diesel::expression::Selectable;
Expand Down
17 changes: 17 additions & 0 deletions diesel_derives/tests/selectable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,20 @@ fn embedded_option() {
let data = my_structs::table.select(A::as_select()).get_result(conn);
assert!(data.is_err());
}

#[test]
fn named_struct_definition_with_skip() {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Queryable, Selectable)]
#[table_name = "my_structs"]
struct MyStruct {
foo: i32,
#[diesel(skip)]
bar: i32,
}

let conn = &mut connection();
let data = my_structs::table
.select(MyStruct::as_select())
.get_result(conn);
assert!(data.is_err());
}

0 comments on commit fe84094

Please sign in to comment.