Skip to content

Commit

Permalink
Rewrite #[derive(Associations)] in derives2
Browse files Browse the repository at this point in the history
This was a pretty straightforward rewrite. Overall I feel much better
about this code structure than what we had before. We're having to do a
bit of funkyness to avoid
rust-lang/rust#47941, but other than that
everything here was pretty straightforward. I had the UI tests guide me
the entire time.
  • Loading branch information
sgrif committed Feb 3, 2018
1 parent 14887fc commit bf6d86d
Show file tree
Hide file tree
Showing 19 changed files with 397 additions and 210 deletions.
32 changes: 0 additions & 32 deletions diesel_compile_tests/tests/compile-fail/associations_errors.rs

This file was deleted.

34 changes: 34 additions & 0 deletions diesel_compile_tests/tests/ui/belongs_to_invalid_option_syntax.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#[macro_use]
extern crate diesel;

table! {
foo {
id -> Integer,
}
}

#[derive(Identifiable)]
#[table_name = "foo"]
struct Bar {
id: i32,
}

#[derive(Identifiable)]
#[table_name = "foo"]
struct Baz {
id: i32,
}

#[derive(Associations)]
#[belongs_to]
#[belongs_to = "Bar"]
#[belongs_to()]
#[belongs_to(foreign_key = "bar_id")]
#[belongs_to(Bar, foreign_key)]
#[belongs_to(Bar, foreign_key(bar_id))]
#[belongs_to(Baz, foreign_key = "bar_id", random_option)]
struct Foo {
bar_id: i32,
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
error: `belongs_to` must be in the form `belongs_to(...)`
--> <macro expansion>:1:1
|
1 | #[belongs_to]
| ^

error: `belongs_to` must be in the form `belongs_to(...)`
--> <macro expansion>:1:1
|
1 | #[belongs_to = "Bar"]
| ^

error: Expected a struct name
--> <macro expansion>:1:1
|
1 | #[belongs_to()]
| ^
|
= help: e.g. `#[belongs_to(User)]`

error: Expected a struct name
--> <macro expansion>:1:1
|
1 | #[belongs_to(foreign_key = "bar_id")]
| ^
|
= help: e.g. `#[belongs_to(User)]`

error: `foreign_key` must be in the form `foreign_key = "value"`
--> <macro expansion>:1:1
|
1 | #[belongs_to(Bar, foreign_key)]
| ^

warning: The form `foreign_key(value)` is deprecated. Use `foreign_key = "value"` instead
--> <macro expansion>:1:1
|
1 | #[belongs_to(Bar, foreign_key(bar_id))]
| ^

warning: Unrecognized option random_option
--> <macro expansion>:1:1
|
1 | #[belongs_to(Baz, foreign_key = "bar_id", random_option)]
| ^

error: aborting due to 5 previous errors

Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#[macro_use]
extern crate diesel;

struct Bar;

table! {
foo {
id -> Integer,
}
}

#[derive(Associations)]
#[belongs_to(Bar)]
#[table_name = "foo"]
struct Foo1 {
bar_id: i32,
}

#[derive(Associations)]
#[belongs_to(Bar, foreign_key = "bar_id")]
#[table_name = "foo"]
struct Foo2 {
bar_id: i32,
}

#[derive(Associations)]
#[belongs_to(Bar)]
#[table_name = "foo"]
struct Foo3 {
#[column_name = "bar_id"]
baz_id: i32,
}

#[derive(Associations)]
#[belongs_to(Bar, foreign_key = "bar_id")]
#[table_name = "foo"]
struct Foo4 {
#[column_name = "bar_id"]
baz_id: i32,
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
error[E0412]: cannot find type `bar_id` in module `foo`
--> <macro expansion>:1:1
|
1 | #[belongs_to(Bar)]
| ^ not found in `foo`

error[E0425]: cannot find value `bar_id` in module `foo`
--> <macro expansion>:1:1
|
1 | #[belongs_to(Bar)]
| ^ not found in `foo`

error[E0412]: cannot find type `bar_id` in module `foo`
--> <macro expansion>:1:1
|
1 | #[belongs_to(Bar, foreign_key = "bar_id")]
| ^ not found in `foo`

error[E0425]: cannot find value `bar_id` in module `foo`
--> <macro expansion>:1:1
|
1 | #[belongs_to(Bar, foreign_key = "bar_id")]
| ^ not found in `foo`

error[E0412]: cannot find type `bar_id` in module `foo`
--> <macro expansion>:1:1
|
1 | #[belongs_to(Bar)]
| ^ not found in `foo`

error[E0425]: cannot find value `bar_id` in module `foo`
--> <macro expansion>:1:1
|
1 | #[belongs_to(Bar)]
| ^ not found in `foo`

error[E0412]: cannot find type `bar_id` in module `foo`
--> <macro expansion>:1:1
|
1 | #[belongs_to(Bar, foreign_key = "bar_id")]
| ^ not found in `foo`

error[E0425]: cannot find value `bar_id` in module `foo`
--> <macro expansion>:1:1
|
1 | #[belongs_to(Bar, foreign_key = "bar_id")]
| ^ not found in `foo`

error: aborting due to 8 previous errors

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#[macro_use]
extern crate diesel;

struct Bar;

#[derive(Associations)]
#[belongs_to(Bar)]
#[belongs_to(Bar, foreign_key = "bar_id")]
struct Foo {}

#[derive(Associations)]
#[belongs_to(Bar)]
#[belongs_to(Bar, foreign_key = "bar_id")]
struct Baz {
#[column_name = "baz_id"]
bar_id: i32,
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
error: No field with column name bar_id
--> <macro expansion>:1:1
|
1 | #[belongs_to(Bar)]
| ^

error: No field with column name bar_id
--> <macro expansion>:1:1
|
1 | #[belongs_to(Bar, foreign_key = "bar_id")]
| ^

error: No field with column name bar_id
--> <macro expansion>:1:1
|
1 | #[belongs_to(Bar)]
| ^

error: No field with column name bar_id
--> <macro expansion>:1:1
|
1 | #[belongs_to(Bar, foreign_key = "bar_id")]
| ^

error: aborting due to 4 previous errors

17 changes: 17 additions & 0 deletions diesel_compile_tests/tests/ui/belongs_to_missing_parent_import.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#[macro_use]
extern crate diesel;

table! {
foos {
id -> Integer,
bar_id -> Integer,
}
}

#[derive(Associations)]
#[belongs_to(Bar)]
struct Foo {
bar_id: i32,
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error[E0412]: cannot find type `Bar` in this scope
--> <macro expansion>:1:1
|
1 | #[belongs_to(Bar)]
| ^ not found in this scope

error: aborting due to previous error

Loading

0 comments on commit bf6d86d

Please sign in to comment.