Skip to content

Commit

Permalink
add #[ormx(insert_attribute = ..)]
Browse files Browse the repository at this point in the history
  • Loading branch information
NyxCode committed Oct 4, 2021
1 parent aea5e9d commit bc65c51
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 4 deletions.
2 changes: 1 addition & 1 deletion ormx-macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ormx-macros"
version = "0.10.0"
version = "0.10.4"
authors = ["moritz"]
edition = "2018"
description = "lightweight procedural macros bringing orm-like features to sqlx"
Expand Down
12 changes: 11 additions & 1 deletion ormx-macros/src/attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ pub enum TableFieldAttr {
Set(Option<Ident>),
// by_ref
ByRef(()),
// insert_attribute = <attribute>
InsertAttr(AnyAttribute)
}

#[derive(Clone)]
Expand Down Expand Up @@ -153,7 +155,8 @@ impl_parse!(TableFieldAttr {
"set" => Set((= Ident)?),
"custom_type" => CustomType(),
"default" => Default(),
"by_ref" => ByRef()
"by_ref" => ByRef(),
"insert_attribute" => InsertAttr(= AnyAttribute)
});

impl_parse!(PatchAttr {
Expand All @@ -167,3 +170,10 @@ impl_parse!(PatchFieldAttr {
"custom_type" => CustomType(),
"by_ref" => ByRef()
});

pub struct AnyAttribute(pub Vec<Attribute>);
impl syn::parse::Parse for AnyAttribute {
fn parse(input: ParseStream) -> Result<Self> {
input.call(Attribute::parse_outer).map(Self)
}
}
3 changes: 2 additions & 1 deletion ormx-macros/src/backend/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,8 @@ pub(crate) fn insert_struct<B: Backend>(table: &Table<B>) -> TokenStream {
let insert_fields = table.insertable_fields().map(|field| {
let ident = &field.field;
let ty = &field.ty;
quote!(#vis #ident: #ty)
let attrs = &field.insert_attrs;
quote!(#(#attrs)* #vis #ident: #ty)
});

let from_impl = impl_from_for_insert_struct(table, ident);
Expand Down
3 changes: 2 additions & 1 deletion ormx-macros/src/table/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{borrow::Cow, convert::TryFrom, marker::PhantomData};
use itertools::Itertools;
use proc_macro2::{Ident, Span, TokenStream};
use quote::quote;
use syn::{DeriveInput, Result, Type, Visibility};
use syn::{DeriveInput, Result, Type, Visibility, Attribute};

use crate::{
attrs::{Getter, Insertable},
Expand Down Expand Up @@ -35,6 +35,7 @@ pub struct TableField<B: Backend> {
pub get_many: Option<Getter>,
pub set: Option<Ident>,
pub by_ref: bool,
pub insert_attrs: Vec<Attribute>,
pub _phantom: PhantomData<*const B>,
}

Expand Down
3 changes: 3 additions & 0 deletions ormx-macros/src/table/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ impl<B: Backend> TryFrom<&syn::Field> for TableField<B> {
default,
by_ref
);
let mut insert_attrs = vec![];

for attr in parse_attrs::<TableFieldAttr>(&value.attrs)? {
match attr {
Expand All @@ -52,6 +53,7 @@ impl<B: Backend> TryFrom<&syn::Field> for TableField<B> {
}
TableFieldAttr::Default(..) => set_once(&mut default, true)?,
TableFieldAttr::ByRef(..) => set_once(&mut by_ref, true)?,
TableFieldAttr::InsertAttr(mut attr) => insert_attrs.append(&mut attr.0),
}
}
Ok(TableField {
Expand All @@ -66,6 +68,7 @@ impl<B: Backend> TryFrom<&syn::Field> for TableField<B> {
get_many,
set,
by_ref: by_ref.unwrap_or(false),
insert_attrs,
_phantom: PhantomData,
})
}
Expand Down

0 comments on commit bc65c51

Please sign in to comment.