New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request: macros for making `NewPost` and `Post` structs less redundant #919

Closed
rbalicki2 opened this Issue May 20, 2017 · 3 comments

Comments

Projects
None yet
3 participants
@rbalicki2
Contributor

rbalicki2 commented May 20, 2017

Would it be possible for diesel to provide macros so that if I have two models, say Post and NewPost, which differ only in that Post also has an id field, I could define them using only a single struct?

e.g.

#[alsoDefineNew]
pub struct Post {
  #[excludeInNew] id: i32,
  title: String,
}

And now, there is a NewPost struct as well.

I've thought about doing this with traits, but it ends up being very redundant and OOP in a java-like way. Macros could probably help, though.

This is far above my pay grade right now, but I wanted to make this request - perhaps it already exists or is in the pipeline :)

@rbalicki2

This comment has been minimized.

Contributor

rbalicki2 commented May 21, 2017

Come to think of it, this is not really core to diesel's mission. It's probably better as an external library. It might need to know about diesel's Insertable, etc. derivations, but otherwise would probably be best as a separate library.

@rbalicki2 rbalicki2 closed this May 21, 2017

@Eijebong

This comment has been minimized.

Member

Eijebong commented May 21, 2017

Have a look at #860 too ^^

@behos

This comment has been minimized.

behos commented Nov 10, 2017

I've created something along these lines for the new structure here

https://github.com/behos/models-derive

Used like https://github.com/behos/models-derive/blob/master/tests/default_insertable.rs#L20

but i haven't published this as a crate since I'm not 100% sure it's complete. I'd be happy to get some feedback and ideas/contributions around that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment