Skip to content

Commit

Permalink
make codegen deterministic (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
carllerche authored Mar 3, 2025
1 parent d3d93cf commit 1a43d2b
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 77 deletions.
64 changes: 32 additions & 32 deletions examples/composite-key/src/db/todo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ impl Todo {
super::user::User::filter(super::user::User::ID.eq(&self.user_id)).into_select(),
)
}
pub async fn get_by_user_id(
db: &Db,
user_id: impl IntoExpr<Id<super::user::User>>,
) -> Result<Todo> {
Self::filter_by_user_id(user_id).get(db).await
}
pub fn filter_by_user_id(user_id: impl IntoExpr<Id<super::user::User>>) -> Query {
Query::default().filter_by_user_id(user_id)
}
pub async fn get_by_user_id_and_id(
db: &Db,
user_id: impl IntoExpr<Id<super::user::User>>,
Expand All @@ -37,15 +46,6 @@ impl Todo {
) -> Query {
Query::default().filter_by_user_id_and_id_batch(keys)
}
pub async fn get_by_user_id(
db: &Db,
user_id: impl IntoExpr<Id<super::user::User>>,
) -> Result<Todo> {
Self::filter_by_user_id(user_id).get(db).await
}
pub fn filter_by_user_id(user_id: impl IntoExpr<Id<super::user::User>>) -> Query {
Query::default().filter_by_user_id(user_id)
}
pub fn create() -> builders::CreateTodo {
builders::CreateTodo::default()
}
Expand Down Expand Up @@ -135,6 +135,16 @@ impl Query {
pub const fn from_stmt(stmt: stmt::Select<Todo>) -> Query {
Query { stmt }
}
pub async fn get_by_user_id(
self,
db: &Db,
user_id: impl IntoExpr<Id<super::user::User>>,
) -> Result<Todo> {
self.filter_by_user_id(user_id).get(db).await
}
pub fn filter_by_user_id(self, user_id: impl IntoExpr<Id<super::user::User>>) -> Query {
self.filter(Todo::USER_ID.eq(user_id))
}
pub async fn get_by_user_id_and_id(
self,
db: &Db,
Expand All @@ -159,16 +169,6 @@ impl Query {
) -> Query {
self.filter(stmt::Expr::in_list((Todo::USER_ID, Todo::ID), keys))
}
pub async fn get_by_user_id(
self,
db: &Db,
user_id: impl IntoExpr<Id<super::user::User>>,
) -> Result<Todo> {
self.filter_by_user_id(user_id).get(db).await
}
pub fn filter_by_user_id(self, user_id: impl IntoExpr<Id<super::user::User>>) -> Query {
self.filter(Todo::USER_ID.eq(user_id))
}
pub async fn all(self, db: &Db) -> Result<Cursor<Todo>> {
db.all(self.stmt).await
}
Expand Down Expand Up @@ -420,6 +420,19 @@ pub mod relations {
pub fn from_stmt(stmt: stmt::Association<[Todo]>) -> Many {
Many { stmt }
}
pub async fn get_by_user_id(
self,
db: &Db,
user_id: impl IntoExpr<Id<super::super::user::User>>,
) -> Result<Todo> {
self.filter_by_user_id(user_id).get(db).await
}
pub fn filter_by_user_id(
self,
user_id: impl IntoExpr<Id<super::super::user::User>>,
) -> Query {
Query::from_stmt(self.into_select()).filter(Todo::USER_ID.eq(user_id))
}
pub async fn get_by_user_id_and_id(
self,
db: &Db,
Expand Down Expand Up @@ -450,19 +463,6 @@ pub mod relations {
pub fn filter_by_id(self, id: impl IntoExpr<Id<Todo>>) -> Query {
Query::from_stmt(self.into_select()).filter(Todo::ID.eq(id))
}
pub async fn get_by_user_id(
self,
db: &Db,
user_id: impl IntoExpr<Id<super::super::user::User>>,
) -> Result<Todo> {
self.filter_by_user_id(user_id).get(db).await
}
pub fn filter_by_user_id(
self,
user_id: impl IntoExpr<Id<super::super::user::User>>,
) -> Query {
Query::from_stmt(self.into_select()).filter(Todo::USER_ID.eq(user_id))
}
#[doc = r" Iterate all entries in the relation"]
pub async fn all(self, db: &Db) -> Result<Cursor<Todo>> {
db.all(self.stmt.into_select()).await
Expand Down
12 changes: 6 additions & 6 deletions examples/cratehub/src/db/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,12 +400,6 @@ pub mod relations {
pub fn from_stmt(stmt: stmt::Association<[Package]>) -> Many {
Many { stmt }
}
pub async fn get_by_id(self, db: &Db, id: impl IntoExpr<Id<Package>>) -> Result<Package> {
self.filter_by_id(id).get(db).await
}
pub fn filter_by_id(self, id: impl IntoExpr<Id<Package>>) -> Query {
Query::from_stmt(self.into_select()).filter(Package::ID.eq(id))
}
pub async fn get_by_user_id(
self,
db: &Db,
Expand Down Expand Up @@ -443,6 +437,12 @@ pub mod relations {
) -> Query {
Query::from_stmt(self.into_select()).filter_by_user_id_and_id_batch(keys)
}
pub async fn get_by_id(self, db: &Db, id: impl IntoExpr<Id<Package>>) -> Result<Package> {
self.filter_by_id(id).get(db).await
}
pub fn filter_by_id(self, id: impl IntoExpr<Id<Package>>) -> Query {
Query::from_stmt(self.into_select()).filter(Package::ID.eq(id))
}
#[doc = r" Iterate all entries in the relation"]
pub async fn all(self, db: &Db) -> Result<Cursor<Package>> {
db.all(self.stmt.into_select()).await
Expand Down
36 changes: 18 additions & 18 deletions examples/cratehub/src/db/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,6 @@ impl User {
Self::PACKAGES.into(),
))
}
pub async fn get_by_email(db: &Db, email: impl IntoExpr<String>) -> Result<User> {
Self::filter_by_email(email).get(db).await
}
pub fn filter_by_email(email: impl IntoExpr<String>) -> Query {
Query::default().filter_by_email(email)
}
pub async fn get_by_id(db: &Db, id: impl IntoExpr<Id<User>>) -> Result<User> {
Self::filter_by_id(id).get(db).await
}
Expand All @@ -35,6 +29,12 @@ impl User {
pub fn filter_by_id_batch(keys: impl IntoExpr<[Id<User>]>) -> Query {
Query::default().filter_by_id_batch(keys)
}
pub async fn get_by_email(db: &Db, email: impl IntoExpr<String>) -> Result<User> {
Self::filter_by_email(email).get(db).await
}
pub fn filter_by_email(email: impl IntoExpr<String>) -> Query {
Query::default().filter_by_email(email)
}
pub fn create() -> builders::CreateUser {
builders::CreateUser::default()
}
Expand Down Expand Up @@ -117,12 +117,6 @@ impl Query {
pub const fn from_stmt(stmt: stmt::Select<User>) -> Query {
Query { stmt }
}
pub async fn get_by_email(self, db: &Db, email: impl IntoExpr<String>) -> Result<User> {
self.filter_by_email(email).get(db).await
}
pub fn filter_by_email(self, email: impl IntoExpr<String>) -> Query {
self.filter(User::EMAIL.eq(email))
}
pub async fn get_by_id(self, db: &Db, id: impl IntoExpr<Id<User>>) -> Result<User> {
self.filter_by_id(id).get(db).await
}
Expand All @@ -132,6 +126,12 @@ impl Query {
pub fn filter_by_id_batch(self, keys: impl IntoExpr<[Id<User>]>) -> Query {
self.filter(stmt::Expr::in_list(User::ID, keys))
}
pub async fn get_by_email(self, db: &Db, email: impl IntoExpr<String>) -> Result<User> {
self.filter_by_email(email).get(db).await
}
pub fn filter_by_email(self, email: impl IntoExpr<String>) -> Query {
self.filter(User::EMAIL.eq(email))
}
pub async fn all(self, db: &Db) -> Result<Cursor<User>> {
db.all(self.stmt).await
}
Expand Down Expand Up @@ -366,12 +366,6 @@ pub mod relations {
pub fn from_stmt(stmt: stmt::Association<[User]>) -> Many {
Many { stmt }
}
pub async fn get_by_email(self, db: &Db, email: impl IntoExpr<String>) -> Result<User> {
self.filter_by_email(email).get(db).await
}
pub fn filter_by_email(self, email: impl IntoExpr<String>) -> Query {
Query::from_stmt(self.into_select()).filter(User::EMAIL.eq(email))
}
pub async fn get_by_id(self, db: &Db, id: impl IntoExpr<Id<User>>) -> Result<User> {
self.filter_by_id(id).get(db).await
}
Expand All @@ -381,6 +375,12 @@ pub mod relations {
pub fn filter_by_id_batch(self, keys: impl IntoExpr<[Id<User>]>) -> Query {
Query::from_stmt(self.into_select()).filter_by_id_batch(keys)
}
pub async fn get_by_email(self, db: &Db, email: impl IntoExpr<String>) -> Result<User> {
self.filter_by_email(email).get(db).await
}
pub fn filter_by_email(self, email: impl IntoExpr<String>) -> Query {
Query::from_stmt(self.into_select()).filter(User::EMAIL.eq(email))
}
#[doc = r" Iterate all entries in the relation"]
pub async fn all(self, db: &Db) -> Result<Cursor<User>> {
db.all(self.stmt.into_select()).await
Expand Down
36 changes: 18 additions & 18 deletions examples/hello-toasty/src/db/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,6 @@ impl User {
Self::TODOS.into(),
))
}
pub async fn get_by_email(db: &Db, email: impl IntoExpr<String>) -> Result<User> {
Self::filter_by_email(email).get(db).await
}
pub fn filter_by_email(email: impl IntoExpr<String>) -> Query {
Query::default().filter_by_email(email)
}
pub async fn get_by_id(db: &Db, id: impl IntoExpr<Id<User>>) -> Result<User> {
Self::filter_by_id(id).get(db).await
}
Expand All @@ -35,6 +29,12 @@ impl User {
pub fn filter_by_id_batch(keys: impl IntoExpr<[Id<User>]>) -> Query {
Query::default().filter_by_id_batch(keys)
}
pub async fn get_by_email(db: &Db, email: impl IntoExpr<String>) -> Result<User> {
Self::filter_by_email(email).get(db).await
}
pub fn filter_by_email(email: impl IntoExpr<String>) -> Query {
Query::default().filter_by_email(email)
}
pub fn create() -> builders::CreateUser {
builders::CreateUser::default()
}
Expand Down Expand Up @@ -118,12 +118,6 @@ impl Query {
pub const fn from_stmt(stmt: stmt::Select<User>) -> Query {
Query { stmt }
}
pub async fn get_by_email(self, db: &Db, email: impl IntoExpr<String>) -> Result<User> {
self.filter_by_email(email).get(db).await
}
pub fn filter_by_email(self, email: impl IntoExpr<String>) -> Query {
self.filter(User::EMAIL.eq(email))
}
pub async fn get_by_id(self, db: &Db, id: impl IntoExpr<Id<User>>) -> Result<User> {
self.filter_by_id(id).get(db).await
}
Expand All @@ -133,6 +127,12 @@ impl Query {
pub fn filter_by_id_batch(self, keys: impl IntoExpr<[Id<User>]>) -> Query {
self.filter(stmt::Expr::in_list(User::ID, keys))
}
pub async fn get_by_email(self, db: &Db, email: impl IntoExpr<String>) -> Result<User> {
self.filter_by_email(email).get(db).await
}
pub fn filter_by_email(self, email: impl IntoExpr<String>) -> Query {
self.filter(User::EMAIL.eq(email))
}
pub async fn all(self, db: &Db) -> Result<Cursor<User>> {
db.all(self.stmt).await
}
Expand Down Expand Up @@ -389,12 +389,6 @@ pub mod relations {
pub fn from_stmt(stmt: stmt::Association<[User]>) -> Many {
Many { stmt }
}
pub async fn get_by_email(self, db: &Db, email: impl IntoExpr<String>) -> Result<User> {
self.filter_by_email(email).get(db).await
}
pub fn filter_by_email(self, email: impl IntoExpr<String>) -> Query {
Query::from_stmt(self.into_select()).filter(User::EMAIL.eq(email))
}
pub async fn get_by_id(self, db: &Db, id: impl IntoExpr<Id<User>>) -> Result<User> {
self.filter_by_id(id).get(db).await
}
Expand All @@ -404,6 +398,12 @@ pub mod relations {
pub fn filter_by_id_batch(self, keys: impl IntoExpr<[Id<User>]>) -> Query {
Query::from_stmt(self.into_select()).filter_by_id_batch(keys)
}
pub async fn get_by_email(self, db: &Db, email: impl IntoExpr<String>) -> Result<User> {
self.filter_by_email(email).get(db).await
}
pub fn filter_by_email(self, email: impl IntoExpr<String>) -> Query {
Query::from_stmt(self.into_select()).filter(User::EMAIL.eq(email))
}
#[doc = r" Iterate all entries in the relation"]
pub async fn all(self, db: &Db) -> Result<Cursor<User>> {
db.all(self.stmt.into_select()).await
Expand Down
1 change: 1 addition & 0 deletions src/codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
indexmap.workspace = true
proc-macro2.workspace = true
quote.workspace = true
std-util.workspace = true
Expand Down
6 changes: 3 additions & 3 deletions src/codegen/src/model/filters.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::*;

use std::collections::HashMap;
use indexmap::IndexMap;

/// Combination of fields for which filter a method should be generated.
pub(super) struct Filter {
Expand All @@ -16,14 +16,14 @@ pub(super) struct Filter {

struct BuildModelFilters<'a> {
model: &'a app::Model,
filters: HashMap<Vec<app::FieldId>, Filter>,
filters: IndexMap<Vec<app::FieldId>, Filter>,
}

impl Filter {
pub(super) fn build_model_filters(model: &app::Model) -> Vec<Filter> {
BuildModelFilters {
model,
filters: HashMap::new(),
filters: IndexMap::new(),
}
.build()
}
Expand Down

0 comments on commit 1a43d2b

Please sign in to comment.