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

Allow specifying diesel path as derive attribute #4277

Open
wants to merge 6 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions diesel_derives/src/as_changeset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ pub fn derive(item: DeriveInput) -> Result<TokenStream> {
treat_none_as_null,
)?);

generate_borrowed_changeset = false; // as soon as we hit one field with #[diesel(serialize_as)] there is no point in generating the impl of AsChangeset for borrowed structs
generate_borrowed_changeset = false; // as soon as we hit one field with
// #[diesel(serialize_as)] there is no point in
// generating the impl of AsChangeset for
// borrowed structs
}
(Some(AttributeSpanWrapper { attribute_span, .. }), true) => {
return Err(syn::Error::new(
Expand Down Expand Up @@ -160,14 +163,17 @@ pub fn derive(item: DeriveInput) -> Result<TokenStream> {
quote! {}
};

Ok(wrap_in_dummy_mod(quote!(
use diesel::query_builder::AsChangeset;
use diesel::prelude::*;
Ok(wrap_in_dummy_mod(
quote!(
use diesel::query_builder::AsChangeset;
use diesel::prelude::*;

#changeset_owned
#changeset_owned

#changeset_borrowed
)))
#changeset_borrowed
),
model.diesel_path.as_ref(),
))
}

fn field_changeset_ty_embed(field: &Field, lifetime: Option<TokenStream>) -> TokenStream {
Expand Down
19 changes: 11 additions & 8 deletions diesel_derives/src/as_expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,15 @@ pub fn derive(item: DeriveInput) -> Result<TokenStream> {
}
});

Ok(wrap_in_dummy_mod(quote! {
use diesel::expression::AsExpression;
use diesel::internal::derives::as_expression::Bound;
use diesel::sql_types::Nullable;
use diesel::serialize::{self, ToSql, Output};

#(#tokens)*
}))
Ok(wrap_in_dummy_mod(
quote! {
use diesel::expression::AsExpression;
use diesel::internal::derives::as_expression::Bound;
use diesel::sql_types::Nullable;
use diesel::serialize::{self, ToSql, Output};

#(#tokens)*
},
model.diesel_path.as_ref(),
))
}
5 changes: 4 additions & 1 deletion diesel_derives/src/associations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ pub fn derive(item: DeriveInput) -> Result<TokenStream> {
.map(|assoc| derive_belongs_to(&item, &model, assoc))
.collect::<Result<Vec<_>>>()?;

Ok(wrap_in_dummy_mod(quote!(#(#tokens)*)))
Ok(wrap_in_dummy_mod(
quote!(#(#tokens)*),
model.diesel_path.as_ref(),
))
}

fn derive_belongs_to(item: &DeriveInput, model: &Model, assoc: &BelongsTo) -> Result<TokenStream> {
Expand Down
15 changes: 11 additions & 4 deletions diesel_derives/src/attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ use crate::deprecated::ParseDeprecated;
use crate::parsers::{BelongsTo, MysqlType, PostgresType, SqliteType};
use crate::util::{
parse_eq, parse_paren, unknown_attribute, BELONGS_TO_NOTE, COLUMN_NAME_NOTE,
DESERIALIZE_AS_NOTE, MYSQL_TYPE_NOTE, POSTGRES_TYPE_NOTE, SELECT_EXPRESSION_NOTE,
SELECT_EXPRESSION_TYPE_NOTE, SERIALIZE_AS_NOTE, SQLITE_TYPE_NOTE, SQL_TYPE_NOTE,
TABLE_NAME_NOTE, TREAT_NONE_AS_DEFAULT_VALUE_NOTE, TREAT_NONE_AS_NULL_NOTE,
DESERIALIZE_AS_NOTE, DIESEL_PATH_NOTE, MYSQL_TYPE_NOTE, POSTGRES_TYPE_NOTE,
SELECT_EXPRESSION_NOTE, SELECT_EXPRESSION_TYPE_NOTE, SERIALIZE_AS_NOTE, SQLITE_TYPE_NOTE,
SQL_TYPE_NOTE, TABLE_NAME_NOTE, TREAT_NONE_AS_DEFAULT_VALUE_NOTE, TREAT_NONE_AS_NULL_NOTE
};

use crate::util::{parse_paren_list, CHECK_FOR_BACKEND_NOTE};
Expand Down Expand Up @@ -228,6 +228,8 @@ pub enum StructAttr {
PostgresType(Ident, PostgresType),
PrimaryKey(Ident, Punctuated<Ident, Comma>),
CheckForBackend(Ident, syn::punctuated::Punctuated<TypePath, syn::Token![,]>),

DieselPath(Ident, Path),
}

impl Parse for StructAttr {
Expand Down Expand Up @@ -278,6 +280,10 @@ impl Parse for StructAttr {
name,
parse_paren_list(input, CHECK_FOR_BACKEND_NOTE, syn::Token![,])?,
)),
"crate" => Ok(StructAttr::DieselPath(
name,
parse_eq(input, DIESEL_PATH_NOTE)?,
)),

_ => Err(unknown_attribute(
&name,
Expand Down Expand Up @@ -316,7 +322,8 @@ impl MySpanned for StructAttr {
| StructAttr::SqliteType(ident, _)
| StructAttr::PostgresType(ident, _)
| StructAttr::CheckForBackend(ident, _)
| StructAttr::PrimaryKey(ident, _) => ident.span(),
| StructAttr::PrimaryKey(ident, _)
| StructAttr::DieselPath(ident, _) => ident.span(),
}
}
}
Expand Down
103 changes: 53 additions & 50 deletions diesel_derives/src/diesel_numeric_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,66 +22,69 @@ pub fn derive(mut item: DeriveInput) -> TokenStream {
impl_generics.params.push(parse_quote!(__Rhs));
let (impl_generics, _, _) = impl_generics.split_for_impl();

wrap_in_dummy_mod(quote! {
use diesel::internal::derives::numeric_ops as ops;
use diesel::expression::{Expression, AsExpression};
use diesel::sql_types::ops::{Add, Sub, Mul, Div};
use diesel::sql_types::{SqlType, SingleValue};
wrap_in_dummy_mod(
quote! {
use diesel::internal::derives::numeric_ops as ops;
use diesel::expression::{Expression, AsExpression};
use diesel::sql_types::ops::{Add, Sub, Mul, Div};
use diesel::sql_types::{SqlType, SingleValue};

impl #impl_generics ::std::ops::Add<__Rhs> for #struct_name #ty_generics
#where_clause
Self: Expression,
<Self as Expression>::SqlType: Add,
<<Self as Expression>::SqlType as Add>::Rhs: SqlType + SingleValue,
__Rhs: AsExpression<<<Self as Expression>::SqlType as Add>::Rhs>,
{
type Output = ops::Add<Self, __Rhs::Expression>;
impl #impl_generics ::std::ops::Add<__Rhs> for #struct_name #ty_generics
#where_clause
Self: Expression,
<Self as Expression>::SqlType: Add,
<<Self as Expression>::SqlType as Add>::Rhs: SqlType + SingleValue,
__Rhs: AsExpression<<<Self as Expression>::SqlType as Add>::Rhs>,
{
type Output = ops::Add<Self, __Rhs::Expression>;

fn add(self, rhs: __Rhs) -> Self::Output {
ops::Add::new(self, rhs.as_expression())
fn add(self, rhs: __Rhs) -> Self::Output {
ops::Add::new(self, rhs.as_expression())
}
}
}

impl #impl_generics ::std::ops::Sub<__Rhs> for #struct_name #ty_generics
#where_clause
Self: Expression,
<Self as Expression>::SqlType: Sub,
<<Self as Expression>::SqlType as Sub>::Rhs: SqlType + SingleValue,
__Rhs: AsExpression<<<Self as Expression>::SqlType as Sub>::Rhs>,
{
type Output = ops::Sub<Self, __Rhs::Expression>;
impl #impl_generics ::std::ops::Sub<__Rhs> for #struct_name #ty_generics
#where_clause
Self: Expression,
<Self as Expression>::SqlType: Sub,
<<Self as Expression>::SqlType as Sub>::Rhs: SqlType + SingleValue,
__Rhs: AsExpression<<<Self as Expression>::SqlType as Sub>::Rhs>,
{
type Output = ops::Sub<Self, __Rhs::Expression>;

fn sub(self, rhs: __Rhs) -> Self::Output {
ops::Sub::new(self, rhs.as_expression())
fn sub(self, rhs: __Rhs) -> Self::Output {
ops::Sub::new(self, rhs.as_expression())
}
}
}

impl #impl_generics ::std::ops::Mul<__Rhs> for #struct_name #ty_generics
#where_clause
Self: Expression,
<Self as Expression>::SqlType: Mul,
<<Self as Expression>::SqlType as Mul>::Rhs: SqlType + SingleValue,
__Rhs: AsExpression<<<Self as Expression>::SqlType as Mul>::Rhs>,
{
type Output = ops::Mul<Self, __Rhs::Expression>;
impl #impl_generics ::std::ops::Mul<__Rhs> for #struct_name #ty_generics
#where_clause
Self: Expression,
<Self as Expression>::SqlType: Mul,
<<Self as Expression>::SqlType as Mul>::Rhs: SqlType + SingleValue,
__Rhs: AsExpression<<<Self as Expression>::SqlType as Mul>::Rhs>,
{
type Output = ops::Mul<Self, __Rhs::Expression>;

fn mul(self, rhs: __Rhs) -> Self::Output {
ops::Mul::new(self, rhs.as_expression())
fn mul(self, rhs: __Rhs) -> Self::Output {
ops::Mul::new(self, rhs.as_expression())
}
}
}

impl #impl_generics ::std::ops::Div<__Rhs> for #struct_name #ty_generics
#where_clause
Self: Expression,
<Self as Expression>::SqlType: Div,
<<Self as Expression>::SqlType as Div>::Rhs: SqlType + SingleValue,
__Rhs: AsExpression<<<Self as Expression>::SqlType as Div>::Rhs>,
{
type Output = ops::Div<Self, __Rhs::Expression>;
impl #impl_generics ::std::ops::Div<__Rhs> for #struct_name #ty_generics
#where_clause
Self: Expression,
<Self as Expression>::SqlType: Div,
<<Self as Expression>::SqlType as Div>::Rhs: SqlType + SingleValue,
__Rhs: AsExpression<<<Self as Expression>::SqlType as Div>::Rhs>,
{
type Output = ops::Div<Self, __Rhs::Expression>;

fn div(self, rhs: __Rhs) -> Self::Output {
ops::Div::new(self, rhs.as_expression())
fn div(self, rhs: __Rhs) -> Self::Output {
ops::Div::new(self, rhs.as_expression())
}
}
}
})
},
None,
)
}
25 changes: 14 additions & 11 deletions diesel_derives/src/from_sql_row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,21 @@ pub fn derive(mut item: DeriveInput) -> Result<TokenStream> {
}
let (impl_generics, _, where_clause) = item.generics.split_for_impl();

Ok(wrap_in_dummy_mod(quote! {
use diesel::deserialize::{self, FromSql, Queryable};
Ok(wrap_in_dummy_mod(
quote! {
use diesel::deserialize::{self, FromSql, Queryable};

// Need to put __ST and __DB after lifetimes but before const params
impl #impl_generics Queryable<__ST, __DB> for #struct_ty
#where_clause
{
type Row = Self;
// Need to put __ST and __DB after lifetimes but before const params
impl #impl_generics Queryable<__ST, __DB> for #struct_ty
#where_clause
{
type Row = Self;

fn build(row: Self::Row) -> deserialize::Result<Self> {
Ok(row)
fn build(row: Self::Row) -> deserialize::Result<Self> {
Ok(row)
}
}
}
}))
},
model.diesel_path.as_ref(),
))
}
51 changes: 27 additions & 24 deletions diesel_derives/src/identifiable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,37 +38,40 @@ pub fn derive(item: DeriveInput) -> Result<TokenStream> {
}
}

Ok(wrap_in_dummy_mod(quote! {
use diesel::associations::{HasTable, Identifiable};
Ok(wrap_in_dummy_mod(
quote! {
use diesel::associations::{HasTable, Identifiable};

impl #impl_generics HasTable for #struct_name #ty_generics
#where_clause
{
type Table = #table_name::table;
impl #impl_generics HasTable for #struct_name #ty_generics
#where_clause
{
type Table = #table_name::table;

fn table() -> Self::Table {
#table_name::table
fn table() -> Self::Table {
#table_name::table
}
}
}

impl #ref_generics Identifiable for &'ident #struct_name #ty_generics
#where_clause
{
type Id = (#(#field_ty),*);
impl #ref_generics Identifiable for &'ident #struct_name #ty_generics
#where_clause
{
type Id = (#(#field_ty),*);

fn id(self) -> Self::Id {
(#(#field_name),*)
fn id(self) -> Self::Id {
(#(#field_name),*)
}
}
}

impl #ref_generics Identifiable for &'_ &'ident #struct_name #ty_generics
#where_clause
{
type Id = (#(#field_ty),*);
impl #ref_generics Identifiable for &'_ &'ident #struct_name #ty_generics
#where_clause
{
type Id = (#(#field_ty),*);

fn id(self) -> Self::Id {
(#(#field_name),*)
fn id(self) -> Self::Id {
(#(#field_name),*)
}
}
}
}))
},
model.diesel_path.as_ref(),
))
}
20 changes: 13 additions & 7 deletions diesel_derives/src/insertable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ pub fn derive(item: DeriveInput) -> Result<TokenStream> {
.map(|table_name| derive_into_single_table(&item, &model, table_name))
.collect::<Result<Vec<_>>>()?;

Ok(wrap_in_dummy_mod(quote! {
use diesel::insertable::Insertable;
use diesel::internal::derives::insertable::UndecoratedInsertRecord;
use diesel::prelude::*;
Ok(wrap_in_dummy_mod(
quote! {
use diesel::insertable::Insertable;
use diesel::internal::derives::insertable::UndecoratedInsertRecord;
use diesel::prelude::*;

#(#tokens)*
}))
#(#tokens)*
},
model.diesel_path.as_ref(),
))
}

fn derive_into_single_table(
Expand Down Expand Up @@ -118,7 +121,10 @@ fn derive_into_single_table(
treat_none_as_default_value,
)?);

generate_borrowed_insert = false; // as soon as we hit one field with #[diesel(serialize_as)] there is no point in generating the impl of Insertable for borrowed structs
generate_borrowed_insert = false; // as soon as we hit one field with
// #[diesel(serialize_as)] there is no point in
// generating the impl of Insertable for borrowed
// structs
}
(Some(AttributeSpanWrapper { attribute_span, .. }), true) => {
return Err(syn::Error::new(
Expand Down
Loading
Loading