|
| 1 | +use syn; |
| 2 | +use quote; |
| 3 | + |
| 4 | +use model::{Model, infer_association_name}; |
| 5 | +use util::str_value_of_meta_item; |
| 6 | + |
| 7 | +pub fn derive_associations(input: syn::MacroInput) -> quote::Tokens { |
| 8 | + let mut derived_associations = Vec::new(); |
| 9 | + let model = t!(Model::from_item(&input, "Associations")); |
| 10 | + |
| 11 | + for attr in &input.attrs { |
| 12 | + if attr.name() == "has_many" { |
| 13 | + let options = t!(build_association_options(attr, "has_many")); |
| 14 | + derived_associations.push(expand_has_many(&model, options)) |
| 15 | + } |
| 16 | + |
| 17 | + if attr.name() == "belongs_to" { |
| 18 | + let options = t!(build_association_options(attr, "belongs_to")); |
| 19 | + derived_associations.push(expand_belongs_to(&model, options)) |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + quote!(#(derived_associations)*) |
| 24 | +} |
| 25 | + |
| 26 | +fn expand_belongs_to(model: &Model, options: AssociationOptions) -> quote::Tokens { |
| 27 | + let parent_struct = options.name; |
| 28 | + let struct_name = &model.name; |
| 29 | + |
| 30 | + let foreign_key_name = options.foreign_key_name.unwrap_or_else(|| |
| 31 | + to_foreign_key(&parent_struct.as_ref())); |
| 32 | + let child_table_name = model.table_name(); |
| 33 | + let fields = &model.attrs; |
| 34 | + |
| 35 | + quote!(BelongsTo! { |
| 36 | + ( |
| 37 | + struct_name = #struct_name, |
| 38 | + parent_struct = #parent_struct, |
| 39 | + foreign_key_name = #foreign_key_name, |
| 40 | + child_table_name = #child_table_name, |
| 41 | + ), |
| 42 | + fields = [#(fields)*], |
| 43 | + }) |
| 44 | +} |
| 45 | + |
| 46 | +fn expand_has_many(model: &Model, options: AssociationOptions) -> quote::Tokens { |
| 47 | + let parent_table_name = model.table_name(); |
| 48 | + let child_table_name = options.name; |
| 49 | + let foreign_key_name = options.foreign_key_name.unwrap_or_else(|| |
| 50 | + to_foreign_key(&model.name.as_ref())); |
| 51 | + let fields = &model.attrs; |
| 52 | + |
| 53 | + quote!(HasMany! { |
| 54 | + ( |
| 55 | + parent_table_name = #parent_table_name, |
| 56 | + child_table = #child_table_name::table, |
| 57 | + foreign_key = #child_table_name::#foreign_key_name, |
| 58 | + ), |
| 59 | + fields = [#(fields)*], |
| 60 | + }) |
| 61 | +} |
| 62 | + |
| 63 | +struct AssociationOptions { |
| 64 | + name: syn::Ident, |
| 65 | + foreign_key_name: Option<syn::Ident>, |
| 66 | +} |
| 67 | + |
| 68 | +fn build_association_options( |
| 69 | + attr: &syn::Attribute, |
| 70 | + association_kind: &str, |
| 71 | +) -> Result<AssociationOptions, String> { |
| 72 | + let usage_error = Err(format!( |
| 73 | + "`#[{}]` must be in the form `#[{}(table_name, option=value)]`", |
| 74 | + association_kind, association_kind)); |
| 75 | + match attr.value { |
| 76 | + syn::MetaItem::List(_, ref options) if options.len() >= 1 => { |
| 77 | + let association_name = match options[0] { |
| 78 | + syn::MetaItem::Word(ref name) => name.clone(), |
| 79 | + _ => return usage_error, |
| 80 | + }; |
| 81 | + let foreign_key_name = options.iter().find(|a| a.name() == "foreign_key") |
| 82 | + .map(|a| str_value_of_meta_item(a, "foreign_key")) |
| 83 | + .map(syn::Ident::new); |
| 84 | + |
| 85 | + Ok(AssociationOptions { |
| 86 | + name: association_name, |
| 87 | + foreign_key_name: foreign_key_name, |
| 88 | + }) |
| 89 | + } |
| 90 | + _ => usage_error, |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +fn to_foreign_key(model_name: &str) -> syn::Ident { |
| 95 | + let lower_cased = infer_association_name(model_name); |
| 96 | + syn::Ident::new(format!("{}_id", &lower_cased)) |
| 97 | +} |
| 98 | + |
| 99 | +#[test] |
| 100 | +fn to_foreign_key_properly_handles_underscores() { |
| 101 | + assert_eq!(str_to_ident("foo_bar_id"), to_foreign_key("FooBar")); |
| 102 | + assert_eq!(str_to_ident("foo_bar_baz_id"), to_foreign_key("FooBarBaz")); |
| 103 | +} |
0 commit comments