-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow structs to be annotated with
#[table_name="foo"]
This is related to #86, where we are not properly inferring the table name for some structs. While I do want to actually fix some of the cases in that issue, it's also been pointed out that we don't handle any edge cases for pluralization. I might improve pluralization *slightly*, but we're not going to maintain an actual mapping of every word as it's brittle, difficult to maintain, and causes bug fixes to stop people's code from compiling. Regardless of how good our inference is, we should decouple the table name from the struct name. This now allows specifying the table name with an annotation. This does not affect any public API, only associations which I have not made public or documented as they're still very prototypical. It should be noted that this *only* affects other annotations on the same struct. When we're processing an annotation on `Foo`, we don't actually have a way to go look at the annotations on `Bar` (at least not as far as I can tell). At this point in time, we do not directly reference any struct in a way that should be affected by this. `#[belongs_to]` comes close, but the table name is based on the association name, not the foreign struct that we look for.
- Loading branch information
Showing
6 changed files
with
90 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use syntax::ast; | ||
use syntax::attr::AttrMetaMethods; | ||
use syntax::ext::base::ExtCtxt; | ||
use syntax::parse::token::str_to_ident; | ||
|
||
fn str_value_of_attr( | ||
cx: &mut ExtCtxt, | ||
attr: &ast::Attribute, | ||
name: &str, | ||
) -> Option<ast::Ident> { | ||
attr.value_str().map(|value| { | ||
str_to_ident(&value) | ||
}).or_else(|| { | ||
cx.span_err(attr.span(), | ||
&format!(r#"`{}` must be in the form `#[{}="something"]`"#, name, name)); | ||
None | ||
}) | ||
} | ||
|
||
pub fn str_value_of_attr_with_name( | ||
cx: &mut ExtCtxt, | ||
attrs: &[ast::Attribute], | ||
name: &str, | ||
) -> Option<ast::Ident> { | ||
attrs.iter() | ||
.find(|a| a.check_name(name)) | ||
.and_then(|a| str_value_of_attr(cx, &a, name)) | ||
} | ||
|
||
#[cfg(feature = "with-syntex")] | ||
pub fn strip_attributes(krate: ast::Crate) -> ast::Crate { | ||
use syntax::fold; | ||
|
||
struct StripAttributeFolder; | ||
|
||
impl fold::Folder for StripAttributeFolder { | ||
fn fold_attribute(&mut self, attr: ast::Attribute) -> Option<ast::Attribute> { | ||
if attr.check_name("table_name") { | ||
None | ||
} else { | ||
Some(attr) | ||
} | ||
} | ||
|
||
fn fold_mac(&mut self, mac: ast::Mac) -> ast::Mac { | ||
fold::noop_fold_mac(mac, self) | ||
} | ||
} | ||
|
||
fold::Folder::fold_crate(&mut StripAttributeFolder, krate) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters