Skip to content

Commit acdf9ad

Browse files
authored
Merge pull request diesel-rs#1451 from diesel-rs/sg-column-name-attr
Deprecate `#[column_name(foo)]` in favor of `#[column_name = "foo"]`
2 parents d463915 + 39fcba8 commit acdf9ad

11 files changed

Lines changed: 43 additions & 33 deletions

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ for Rust libraries in [RFC #1105](https://github.com/rust-lang/rfcs/blob/master/
1616

1717
* Deprecated `impl_query_id!` in favor of `#[derive(QueryId)]`
1818

19+
* Deprecated specifying a column name as `#[column_name(foo)]`. `#[column_name =
20+
"foo"]` should be used instead.
21+
1922
## [1.0.0] - 2018-01-02
2023

2124
### Added

diesel_compile_tests/tests/compile-fail/insert_statement_does_not_support_returning_methods_on_sqlite.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,9 @@ impl<DB: Backend> Queryable<(Integer, VarChar), DB> for User where
3333
}
3434
}
3535

36-
pub struct NewUser(String);
37-
38-
impl_Insertable! {
39-
(users)
40-
pub struct NewUser(#[column_name(name)] String,);
41-
}
36+
#[derive(Insertable)]
37+
#[table_name = "users"]
38+
pub struct NewUser(#[column_name = "name"] String);
4239

4340
fn main() {
4441
let connection = SqliteConnection::establish(":memory:").unwrap();

diesel_compile_tests/tests/compile-fail/pg_on_conflict_requires_valid_conflict_target.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ table! {
1818
}
1919

2020
#[derive(Insertable)]
21-
#[table_name="users"]
22-
struct NewUser(#[column_name(name)] &'static str);
21+
#[table_name = "users"]
22+
pub struct NewUser(#[column_name = "name"] &'static str);
2323

2424
sql_function!(lower, lower_t, (x: diesel::types::Text) -> diesel::types::Text);
2525

diesel_compile_tests/tests/compile-fail/pg_specific_expressions_cant_be_used_in_a_sqlite_query.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ sql_function!(lower, lower_t, (x: VarChar) -> VarChar);
1515

1616
#[derive(Insertable)]
1717
#[table_name="users"]
18-
struct NewUser(#[column_name(name)] &'static str);
18+
struct NewUser(#[column_name = "name"] &'static str);
1919

2020
// NOTE: This test is meant to be comprehensive, but not exhaustive.
2121
fn main() {

diesel_compile_tests/tests/compile-fail/pg_upsert_do_update_requires_valid_update.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ table! {
1818
}
1919

2020
#[derive(Insertable)]
21-
#[table_name="users"]
22-
struct NewUser(#[column_name(name)] &'static str);
21+
#[table_name = "users"]
22+
pub struct NewUser(#[column_name = "name"] &'static str);
2323

2424
#[allow(deprecated)]
2525
fn main() {

diesel_compile_tests/tests/compile-fail/returning_cannot_be_called_twice.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,9 @@ table! {
99
}
1010
}
1111

12-
pub struct NewUser(String);
13-
14-
impl_Insertable! {
15-
(users)
16-
pub struct NewUser(#[column_name(name)] String,);
17-
}
12+
#[derive(Insertable)]
13+
#[table_name = "users"]
14+
pub struct NewUser(#[column_name = "name"] String);
1815

1916
fn main() {
2017
use self::users::dsl::*;

diesel_compile_tests/tests/compile-fail/returning_clause_requires_selectable_expression.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,9 @@ table! {
1010
}
1111
}
1212

13-
pub struct NewUser(String);
14-
15-
impl_Insertable! {
16-
(users)
17-
pub struct NewUser(#[column_name(name)] String,);
18-
}
13+
#[derive(Insertable)]
14+
#[table_name = "users"]
15+
pub struct NewUser(#[column_name = "name"] String);
1916

2017
table! {
2118
non_users {

diesel_derives/src/attr.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ impl Attr {
1717
pub fn from_struct_field((index, field): (usize, &syn::Field)) -> Self {
1818
let field_name = field.ident.clone();
1919
let column_name = ident_value_of_attr_with_name(&field.attrs, "column_name")
20-
.cloned()
2120
.or_else(|| field_name.clone());
2221
let ty = field.ty.clone();
2322
let sql_type = str_value_of_attr_with_name(&field.attrs, "sql_type")

diesel_derives/src/model.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ impl Model {
2929
let primary_key_names = list_value_of_attr_with_name(&item.attrs, "primary_key")
3030
.map(|v| v.into_iter().cloned().collect())
3131
.unwrap_or_else(|| vec![syn::Ident::new("id")]);
32-
let table_name_from_annotation =
33-
str_value_of_attr_with_name(&item.attrs, "table_name").map(syn::Ident::new);
32+
let table_name_from_annotation = ident_value_of_attr_with_name(&item.attrs, "table_name");
3433

3534
Ok(Model {
3635
ty: ty,

diesel_derives/src/util.rs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,30 @@ pub fn str_value_of_attr_with_name<'a>(attrs: &'a [Attribute], name: &str) -> Op
3838
attr_with_name(attrs, name).map(|attr| str_value_of_attr(attr, name))
3939
}
4040

41-
pub fn ident_value_of_attr_with_name<'a>(attrs: &'a [Attribute], name: &str) -> Option<&'a Ident> {
42-
list_value_of_attr_with_name(attrs, name).map(|idents| {
43-
if idents.len() != 1 {
44-
panic!(r#"`{}` must be in the form `#[{}(something)]`"#, name, name);
41+
pub fn ident_value_of_attr_with_name<'a>(attrs: &'a [Attribute], name: &str) -> Option<Ident> {
42+
let error = || {
43+
panic!(
44+
r#"`{}` must be in the form `#[{} = "something"]`"#,
45+
name, name
46+
)
47+
};
48+
attr_with_name(attrs, name).map(|attr| match attr.value {
49+
MetaItem::NameValue(_, Lit::Str(ref value, _)) => Ident::from(&**value),
50+
MetaItem::List(_, ref list) => {
51+
if list.len() != 1 {
52+
error();
53+
}
54+
println!(
55+
r#"The form `#[{}(something)]` is deprecated. Use `#[{} = "something"]` instead"#,
56+
name, name
57+
);
58+
if let NestedMetaItem::MetaItem(MetaItem::Word(ref ident)) = list[0] {
59+
ident.clone()
60+
} else {
61+
error()
62+
}
4563
}
46-
idents[0]
64+
_ => error(),
4765
})
4866
}
4967

0 commit comments

Comments
 (0)