Skip to content

Commit

Permalink
Get rid of table block in schema file
Browse files Browse the repository at this point in the history
This is another step towards matching Rust syntax for the schema file
  • Loading branch information
carllerche committed Jan 14, 2025
1 parent 5db3536 commit 49c2883
Show file tree
Hide file tree
Showing 69 changed files with 807 additions and 791 deletions.
40 changes: 20 additions & 20 deletions examples/cratehub/schema.toasty
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
table user_and_packages {
model User {
#[key]
#[auto]
id: Id,
#[table(user_and_packages)]
model User {
#[key]
#[auto]
id: Id,

name: String,
name: String,

#[unique]
email: String,
#[unique]
email: String,

packages: [Package],
}
packages: [Package],
}

#[key(partition = user_id, local = id)]
model Package {
#[relation(key = user_id, references = id)]
user: User,
#[table(user_and_packages)]
#[key(partition = user_id, local = id)]
model Package {
#[relation(key = user_id, references = id)]
user: User,

user_id: Id<User>,
user_id: Id<User>,

#[auto]
id: Id,
#[auto]
id: Id,

name: String,
}
}
name: String,
}
3 changes: 2 additions & 1 deletion src/codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ pub fn generate<'a>(schema: &'a Schema, in_macro: bool) -> Output<'a> {
let names = Rc::new(Names::from_schema(schema));

let models = schema
.models()
.models
.iter()
.map(|model| model::generate(schema, model, names.clone(), in_macro))
.collect();

Expand Down
42 changes: 23 additions & 19 deletions src/codegen/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use std::rc::Rc;

pub(crate) fn generate<'a>(
schema: &'a Schema,
model: &'a Model,
model: &'a app::Model,
names: Rc<Names>,
in_macro: bool,
) -> ModelOutput<'a> {
Expand All @@ -35,7 +35,7 @@ pub(crate) struct Generator<'a> {
pub schema: &'a Schema,

/// Model being generated
pub model: &'a Model,
pub model: &'a app::Model,

/// Stores various names
pub names: Rc<Names>,
Expand All @@ -48,7 +48,7 @@ impl<'a> Generator<'a> {
/// Create a new `GenModel` for the provided model
pub(crate) fn new(
schema: &'a Schema,
model: &'a Model,
model: &'a app::Model,
names: Rc<Names>,
in_macro: bool,
) -> Generator<'a> {
Expand All @@ -60,7 +60,7 @@ impl<'a> Generator<'a> {
}
}

pub(crate) fn module_path(&self, mid: ModelId, depth: usize) -> TokenStream {
pub(crate) fn module_path(&self, mid: app::ModelId, depth: usize) -> TokenStream {
if mid == self.model.id {
quote!()
} else {
Expand Down Expand Up @@ -89,7 +89,7 @@ impl<'a> Generator<'a> {
}
}

pub(crate) fn module_name(&self, id: ModelId, depth: usize) -> TokenStream {
pub(crate) fn module_name(&self, id: app::ModelId, depth: usize) -> TokenStream {
let name = &self.names.models[&id].module_name;

if id == self.model.id {
Expand All @@ -104,23 +104,23 @@ impl<'a> Generator<'a> {
}
}

pub(crate) fn field_name(&self, field: impl Into<FieldId>) -> &syn::Ident {
pub(crate) fn field_name(&self, field: impl Into<app::FieldId>) -> &syn::Ident {
let field = field.into();
&self.names.fields[&field].field_name
}

pub(crate) fn field_const_name(&self, field: impl Into<FieldId>) -> &syn::Ident {
pub(crate) fn field_const_name(&self, field: impl Into<app::FieldId>) -> &syn::Ident {
let field = field.into();
&self.names.fields[&field].const_name
}

pub(crate) fn singular_name(&self, field: impl Into<FieldId>) -> &syn::Ident {
pub(crate) fn singular_name(&self, field: impl Into<app::FieldId>) -> &syn::Ident {
let field = field.into();
self.names.relations[&field].singular_name.as_ref().unwrap()
}

pub(crate) fn field_ty(&self, field: &Field, depth: usize) -> TokenStream {
use FieldTy::*;
pub(crate) fn field_ty(&self, field: &app::Field, depth: usize) -> TokenStream {
use app::FieldTy::*;

match &field.ty {
Primitive(field_ty) => self.ty(&field_ty.ty, depth),
Expand All @@ -135,11 +135,11 @@ impl<'a> Generator<'a> {
}
}

pub(crate) fn query(&self, id: impl Into<QueryId>) -> &Query {
pub(crate) fn query(&self, id: impl Into<app::QueryId>) -> &app::Query {
self.schema.query(id.into())
}

pub(crate) fn pk_query(&self) -> &Query {
pub(crate) fn pk_query(&self) -> &app::Query {
self.schema.query(self.model.primary_key.query)
}

Expand All @@ -160,7 +160,11 @@ impl<'a> Generator<'a> {
self.model_struct_path(self.model.id, 1)
}

pub(crate) fn model_struct_path(&self, id: impl Into<ModelId>, depth: usize) -> TokenStream {
pub(crate) fn model_struct_path(
&self,
id: impl Into<app::ModelId>,
depth: usize,
) -> TokenStream {
let id = id.into();
let name = &self.names.models[&id].struct_name;

Expand All @@ -172,14 +176,14 @@ impl<'a> Generator<'a> {
}
}

pub(crate) fn relation_struct_name(&self, field: impl Into<FieldId>) -> &syn::Ident {
pub(crate) fn relation_struct_name(&self, field: impl Into<app::FieldId>) -> &syn::Ident {
let field = field.into();
&self.names.relations[&field].struct_name
}

pub(crate) fn relation_query_struct_path(
&self,
field: impl Into<FieldId>,
field: impl Into<app::FieldId>,
depth: usize,
) -> TokenStream {
let field = field.into();
Expand All @@ -188,23 +192,23 @@ impl<'a> Generator<'a> {
quote!(#target_mod_name::relation::#field_name::Query)
}

pub(crate) fn model_pk_query_method_name(&self, id: ModelId) -> &syn::Ident {
pub(crate) fn model_pk_query_method_name(&self, id: app::ModelId) -> &syn::Ident {
let query = self.schema.model(id).primary_key.query;
self.query_method_name(query)
}

pub(crate) fn query_method_name(&self, query: QueryId) -> &syn::Ident {
pub(crate) fn query_method_name(&self, query: app::QueryId) -> &syn::Ident {
&self.names.queries[&query].method_name
}

pub(crate) fn scoped_query_method_name(&self, query: QueryId) -> &syn::Ident {
pub(crate) fn scoped_query_method_name(&self, query: app::QueryId) -> &syn::Ident {
self.names.queries[&query]
.scoped_method_name
.as_ref()
.unwrap()
}

pub(crate) fn query_struct_name(&self, query: QueryId) -> &syn::Ident {
pub(crate) fn query_struct_name(&self, query: app::QueryId) -> &syn::Ident {
&self.names.queries[&query].struct_name
}

Expand Down
6 changes: 6 additions & 0 deletions src/codegen/src/model/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ impl<'a> Generator<'a> {

/// Each model field has an associated struct field
fn gen_struct_fields(&self) -> TokenStream {
use app::FieldTy;

self.model
.fields
.iter()
Expand Down Expand Up @@ -198,6 +200,8 @@ impl<'a> Generator<'a> {
}

fn gen_struct_load_fields(&self) -> TokenStream {
use app::FieldTy;

self.model
.fields
.iter()
Expand Down Expand Up @@ -227,6 +231,8 @@ impl<'a> Generator<'a> {
}

fn gen_struct_into_expr(&self) -> TokenStream {
use app::FieldTy;

let mut pk_exprs = vec![];

for field in self.model.primary_key_fields() {
Expand Down
4 changes: 3 additions & 1 deletion src/codegen/src/model/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl<'a> Generator<'a> {
self.create_struct_path(self.model.id, 0)
}

pub(super) fn create_struct_path(&self, id: ModelId, depth: usize) -> TokenStream {
pub(super) fn create_struct_path(&self, id: app::ModelId, depth: usize) -> TokenStream {
let name = &self.names.models[&id].create_name;

if id == self.model.id {
Expand All @@ -66,6 +66,8 @@ impl<'a> Generator<'a> {
}

fn gen_create_methods(&self) -> TokenStream {
use app::FieldTy;

self.model.fields.iter().map(move |field| {
let name = self.field_name(field.id);
let index = util::int(field.id.index);
Expand Down
6 changes: 3 additions & 3 deletions src/codegen/src/model/fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::*;

impl<'a> Generator<'a> {
pub(super) fn gen_model_field_consts(&self) -> TokenStream {
use FieldTy::*;
use app::FieldTy::*;

self.model
.fields
Expand Down Expand Up @@ -32,8 +32,8 @@ impl<'a> Generator<'a> {
.collect()
}

pub(super) fn gen_path_methods(&self, model: &'a Model, depth: usize) -> TokenStream {
use FieldTy::*;
pub(super) fn gen_path_methods(&self, model: &'a app::Model, depth: usize) -> TokenStream {
use app::FieldTy::*;

model
.fields
Expand Down
22 changes: 12 additions & 10 deletions src/codegen/src/model/find_by.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ impl<'a> Generator<'a> {
.collect()
}

pub(crate) fn gen_scoped_find_by_method(&self, scoped: &ScopedQuery) -> TokenStream {
pub(crate) fn gen_scoped_find_by_method(&self, scoped: &app::ScopedQuery) -> TokenStream {
let query_method_name = self.scoped_query_method_name(scoped.id);
let query_struct_name = self.query_struct_name(scoped.id);

Expand Down Expand Up @@ -50,22 +50,24 @@ impl<'a> Generator<'a> {
// TODO: split this up and unify with other fns
pub(crate) fn gen_scoped_find_by_struct(
&self,
scoped: &ScopedQuery,
scoped: &app::ScopedQuery,
depth: usize,
) -> TokenStream {
let query = self.query(scoped.id);
self.gen_find_by_struct(query, depth)
}

fn find_by_query(&self, query: &Query) -> TokenStream {
fn find_by_query(&self, query: &app::Query) -> TokenStream {
if !query.many {
self.find_basic_by_query(query)
} else {
self.find_many_by_query(query)
}
}

fn find_basic_by_query(&self, query: &Query) -> TokenStream {
fn find_basic_by_query(&self, query: &app::Query) -> TokenStream {
use app::FieldTy;

let query_method_name = self.query_method_name(query.id);
let model_struct_name = self.self_struct_name();
let query_struct_name = self.query_struct_name(query.id);
Expand Down Expand Up @@ -169,7 +171,7 @@ impl<'a> Generator<'a> {
}
}

fn find_many_by_query(&self, query: &Query) -> TokenStream {
fn find_many_by_query(&self, query: &app::Query) -> TokenStream {
let query_method_name = self.query_method_name(query.id);
let model_struct_name = self.self_struct_name();
let query_struct_name = self.query_struct_name(query.id);
Expand Down Expand Up @@ -263,7 +265,7 @@ impl<'a> Generator<'a> {

fn gen_method_args(
&self,
args: &[Arg],
args: &[app::Arg],
arg_idents: &[TokenStream],
depth: usize,
) -> TokenStream {
Expand Down Expand Up @@ -299,7 +301,7 @@ impl<'a> Generator<'a> {
quote!( #( #args ),* )
}

fn gen_item_arg_tys(&self, args: &[Arg]) -> TokenStream {
fn gen_item_arg_tys(&self, args: &[app::Arg]) -> TokenStream {
let mut tys = args.iter().map(move |arg| match &arg.ty {
stmt::Type::Model(model) => {
let target_struct_name = self.model_struct_path(*model, 1);
Expand All @@ -325,7 +327,7 @@ impl<'a> Generator<'a> {
}
}

fn gen_find_by_struct(&self, query: &Query, depth: usize) -> TokenStream {
fn gen_find_by_struct(&self, query: &app::Query, depth: usize) -> TokenStream {
let path = self.module_path(query.ret, depth);
let model_struct_name = self.model_struct_path(query.ret, depth);
let query_struct_name = self.query_struct_name(query.id);
Expand Down Expand Up @@ -370,7 +372,7 @@ impl<'a> Generator<'a> {

fn gen_expr_from_stmt(
&self,
mid: ModelId,
mid: app::ModelId,
args: &[TokenStream],
filter: &stmt::Expr,
depth: usize,
Expand Down Expand Up @@ -443,7 +445,7 @@ impl<'a> Generator<'a> {

fn gen_expr_chain(
&self,
model_id: ModelId,
model_id: app::ModelId,
args: &[TokenStream],
exprs: &[stmt::Expr],
f: TokenStream,
Expand Down
4 changes: 3 additions & 1 deletion src/codegen/src/model/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ impl<'a> Generator<'a> {
}

fn gen_relation_methods(&self) -> TokenStream {
use app::FieldTy;

self.model
.fields
.iter()
Expand All @@ -92,7 +94,7 @@ impl<'a> Generator<'a> {
.collect()
}

fn gen_belongs_to_method(&self, field: FieldId, target: ModelId) -> TokenStream {
fn gen_belongs_to_method(&self, field: app::FieldId, target: app::ModelId) -> TokenStream {
let name = self.field_name(field);
let module_name = self.module_name(target, 0);

Expand Down
Loading

0 comments on commit 49c2883

Please sign in to comment.