Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign upIs there a way to use strong types like `UserId`? #1014
Comments
killercup
added
discussion desired
enhancement
labels
Jul 10, 2017
This comment has been minimized.
|
I don't think this is easily possible right now—you'd need to implement a bunch of traits, like ToSql and FromSql, cf. this test. It'd be possible to write |
This comment has been minimized.
|
I think that would be a strong candidate for something that should best live as a third party crate, not as part of Diesel itself. However, I'm happy to assist someone in implementing such a crate. |
This comment has been minimized.
|
Oh yes, I meant as an external proc-macro crate.
(This might copy a bunch of code from the diesel-internal implementations of primitive SQL types, though.)
… Am 10.07.2017 um 17:53 schrieb Sean Griffin ***@***.***>:
I think that would be a strong candidate for something that should best live as a third party crate, not as part of Diesel itself. However, I'm happy to assist someone in implementing such a crate.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or mute the thread.
|
This comment has been minimized.
|
It shouldn't need to. The impls should all be able to look something like: pub struct UserId(pub i32);
impl<ST, DB> ToSql<ST, DB> for UserId
where
i32: ToSql<ST, DB>,
{
fn to_sql(...) {
self.0.to_sql(...)
}
} |
This comment has been minimized.
|
I think you'd also need the |
This comment has been minimized.
|
@Eijebong This isn't implementing a new SQL type. However, even if it were it could still be trivially delegated. pub struct MyIntegerType;
impl<DB> HasSqlType<MyIntegerType> for DB
where
DB: TypeMetadata + HasSqlType<Integer>,
{
fn metadata() -> DB::TypeMetadata {
<DB as HasSqlType<Integer>>::metadata()
}
} |
This comment has been minimized.
|
Oh, right, this is the same trait for every backend... I'm dumb :) |
This comment has been minimized.
|
I've just implemented a I would appreciate some eyes on it (bug reports/pull requests). If the name seems reasonable and we can get it into good enough shape I'm happy to upload it to crates.io and/or move it into the diesel-rs org. |
This comment has been minimized.
Lol I'd probably start with trying that out. ;) |
This comment has been minimized.
This seems reasonable to me. |
This comment has been minimized.
That's the goal. ;) |
This comment has been minimized.
|
I've only given it a cursory look, but it looks fine at first glance. You're missing a lot of impls that are required to use the newtype in every context you could use |
This comment has been minimized.
|
I've been wanting to create newtypes, but I've been too lazy to actually do the various From/Tos, so I just created a "ToUnderlying" trait that only my db modules have access to, and have been using that. With this I've swapped out a couple of my IDs in a couple of structs and, like I said, it seems to do... oh you've responded. Those all look easy enough, thanks! I'm heading out for the weekend soon, but I should be able to implement them early next week if no one beats me to it. |
This comment has been minimized.
|
I couldn't find any obvious way of verifying that my derive generates something reasonable, is there an example test file that guarantees that everything works without a db, or should I just bite the bullet and copy your db setup code? |
This comment has been minimized.
|
You'll want to verify against an actual database. Anything else will only be testing your assumptions. |
This comment has been minimized.
|
And here I've been assuming my assumptions were great. That makes my decision making easier. |
This comment has been minimized.
|
Could we have it as an example in the docs and tests? |
This comment has been minimized.
|
Have what as an example in which docs/tests? |
This comment has been minimized.
|
UserId as an example of having custom type, explaining what one needs to do
to achieve that.
…On 13 Jul 2017 17:27, "Brandon W Maister" ***@***.***> wrote:
Have what as an example in which docs/tests?
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#1014 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAOrM_xF-t-eyd4zwpzqVRHLIIhgLncwks5sNkVzgaJpZM4OTBE5>
.
|
This comment has been minimized.
|
Ah, yeah a "custom types" guide, explaining what each trait enables as you implement it, would be nice to have. I might be able to write something like that as I add tests and discover additional functionality for diesel-newtype. |
This comment has been minimized.
|
Exactly |
This comment has been minimized.
|
I've implemented (thanks entirely to @sgrif's help) auto-queryable and auto-asexpression for diesel-newtype in quodlibetor/diesel-derive-newtype#2. There are even tests that verify that obvious things that should work do work. If anybody wants to try substituting in a newtype using it now might be a reasonable time, although admittedly there's a decent chance that what you'd be doing is helping me debug rather than actually making your code better. |
This comment has been minimized.
|
So I ran into the fact that diesel's query builder operates pretty much entirely on the SQL types of I haven't been able to come up with something that would allow strong typing on the rust side of the query builder and still... work... with the rest of diesel. It seems like it is by design that queries operate on SQL types. I mean, it makes sense too, it's not like it's a bad decision. I'm starting work on a guide around implementing columnar types, and I'm curious if that is an accurate description of the way that diesel treats rust types once they enter the query builder. Additionally, that would affect how I document diesel-newtype. |
This comment has been minimized.
|
You'd need to create a custom domain type in PG to do what you're describing. |
This comment has been minimized.
|
Okay so that sounds like my understanding is correct but databases may support doing it on their side. I didn't even consider that. |
This comment has been minimized.
|
Closing as there's nothing actionable here. See #595 (comment) for more thoughts, and #162 for the tracking issue on ergonomics here. |
vityafx commentedJul 10, 2017
For example, I have a table
userswithid INTEGER PRIMARY KEY. Is there a way to usepub struct UserId(pub i32)instead ofi32for the user id?