Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/codegen/src/csharp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1048,7 +1048,7 @@ impl Lang for Csharp<'_> {
indented_block(&mut output, |output| {
writeln!(output, "public RemoteTables(DbConnection conn)");
indented_block(output, |output| {
for (table_name, _) in iter_table_names_and_types(module) {
for (table_name, _) in iter_table_names_and_types(module, false) {
writeln!(
output,
"AddTable({} = new(conn));",
Expand All @@ -1071,7 +1071,7 @@ impl Lang for Csharp<'_> {

writeln!(output, "public sealed class From");
indented_block(&mut output, |output| {
for (table_name, product_type_ref) in iter_table_names_and_types(module) {
for (table_name, product_type_ref) in iter_table_names_and_types(module, false) {
let method_name = table_name.deref().to_case(Case::Pascal);
let row_type = type_ref_name(module, product_type_ref);
let table_name_lit = format!("{:?}", table_name.deref());
Expand Down
2 changes: 1 addition & 1 deletion crates/codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub use util::AUTO_GENERATED_PREFIX;

pub fn generate(module: &ModuleDef, lang: &dyn Lang) -> Vec<OutputFile> {
itertools::chain!(
module.tables().map(|tbl| lang.generate_table_file(module, tbl)),
util::iter_tables(module, false).map(|tbl| lang.generate_table_file(module, tbl)),
module.views().map(|view| lang.generate_view_file(module, view)),
module.types().flat_map(|typ| lang.generate_type_files(module, typ)),
util::iter_reducers(module).map(|reducer| lang.generate_reducer_file(module, reducer)),
Expand Down
16 changes: 8 additions & 8 deletions crates/codegen/src/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1131,7 +1131,7 @@ fn iter_module_names(module: &ModuleDef) -> impl Iterator<Item = String> + '_ {
itertools::chain!(
iter_types(module).map(|ty| type_module_name(&ty.name)),
iter_reducers(module).map(|r| reducer_module_name(&r.name)),
iter_tables(module).map(|tbl| table_module_name(&tbl.name)),
iter_tables(module, false).map(|tbl| table_module_name(&tbl.name)),
iter_views(module).map(|view| table_module_name(&view.name)),
iter_procedures(module).map(|proc| procedure_module_name(&proc.name)),
)
Expand All @@ -1151,7 +1151,7 @@ fn print_module_reexports(module: &ModuleDef, out: &mut Indenter) {
let type_name = collect_case(Case::Pascal, ty.name.name_segments());
writeln!(out, "pub use {mod_name}::{type_name};")
}
for (table_name, _) in iter_table_names_and_types(module) {
for (table_name, _) in iter_table_names_and_types(module, false) {
let mod_name = table_module_name(table_name);
// TODO: More precise reexport: we want:
// - The trait name.
Expand Down Expand Up @@ -1310,7 +1310,7 @@ fn print_db_update_defn(module: &ModuleDef, out: &mut Indenter) {
out.delimited_block(
"pub struct DbUpdate {",
|out| {
for (table_name, product_type_ref) in iter_table_names_and_types(module) {
for (table_name, product_type_ref) in iter_table_names_and_types(module, false) {
writeln!(
out,
"{}: __sdk::TableUpdate<{}>,",
Expand All @@ -1334,7 +1334,7 @@ impl TryFrom<__ws::DatabaseUpdate<__ws::BsatnFormat>> for DbUpdate {
match &table_update.table_name[..] {
",
|out| {
for (table_name, _) in iter_table_names_and_types(module) {
for (table_name, _) in iter_table_names_and_types(module, false) {
writeln!(
out,
"{:?} => db_update.{}.append({}::parse_table_update(table_update)?),",
Expand Down Expand Up @@ -1378,7 +1378,7 @@ impl __sdk::InModule for DbUpdate {{
let mut diff = AppliedDiff::default();
",
|out| {
for table in iter_tables(module) {
for table in iter_tables(module, false) {
let with_updates = table
.primary_key
.map(|col| {
Expand Down Expand Up @@ -1421,7 +1421,7 @@ fn print_applied_diff_defn(module: &ModuleDef, out: &mut Indenter) {
out.delimited_block(
"pub struct AppliedDiff<'r> {",
|out| {
for (table_name, product_type_ref) in iter_table_names_and_types(module) {
for (table_name, product_type_ref) in iter_table_names_and_types(module, false) {
writeln!(
out,
"{}: __sdk::TableAppliedDiff<'r, {}>,",
Expand Down Expand Up @@ -1454,7 +1454,7 @@ impl __sdk::InModule for AppliedDiff<'_> {{
out.delimited_block(
"fn invoke_row_callbacks(&self, event: &EventContext, callbacks: &mut __sdk::DbCallbacks<RemoteModule>) {",
|out| {
for (table_name, product_type_ref) in iter_table_names_and_types(module) {
for (table_name, product_type_ref) in iter_table_names_and_types(module, false) {
writeln!(
out,
"callbacks.invoke_table_row_callbacks::<{}>({:?}, &self.{}, event);",
Expand Down Expand Up @@ -1497,7 +1497,7 @@ type QueryBuilder = __sdk::QueryBuilder;
out.delimited_block(
"fn register_tables(client_cache: &mut __sdk::ClientCache<Self>) {",
|out| {
for (table_name, _) in iter_table_names_and_types(module) {
for (table_name, _) in iter_table_names_and_types(module, false) {
writeln!(out, "{}::register_table(client_cache);", table_module_name(table_name));
}
},
Expand Down
4 changes: 2 additions & 2 deletions crates/codegen/src/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ impl Lang for TypeScript {

writeln!(out);
writeln!(out, "// Import and reexport all table handle types");
for (table_name, _) in iter_table_names_and_types(module) {
for (table_name, _) in iter_table_names_and_types(module, false) {
let table_module_name = table_module_name(table_name);
let table_name_pascalcase = table_name.deref().to_case(Case::Pascal);
// TODO: This really shouldn't be necessary. We could also have `table()` accept
Expand All @@ -253,7 +253,7 @@ impl Lang for TypeScript {
writeln!(out, "/** The schema information for all tables in this module. This is defined the same was as the tables would have been defined in the server. */");
writeln!(out, "const tablesSchema = __schema(");
out.indent(1);
for table in iter_tables(module) {
for table in iter_tables(module, false) {
let type_ref = table.product_type_ref;
let table_name_pascalcase = table.name.deref().to_case(Case::Pascal);
writeln!(out, "__table({{");
Expand Down
18 changes: 9 additions & 9 deletions crates/codegen/src/unrealcpp.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Autogenerated Unreal‑C++ code‑gen backend for SpacetimeDB CLI
use crate::code_indenter::CodeIndenter;
use crate::util::{
collect_case, fmt_fn, iter_table_names_and_types, print_auto_generated_file_comment,
collect_case, fmt_fn, iter_table_names_and_types, iter_tables, print_auto_generated_file_comment,
print_auto_generated_version_comment,
};
use crate::util::{iter_indexes, iter_procedures, iter_reducers};
Expand Down Expand Up @@ -865,7 +865,7 @@ impl Lang for UnrealCpp<'_> {
writeln!(client_h);

writeln!(client_h, "/** Forward declaration for tables */");
for (table_name, _) in iter_table_names_and_types(module) {
for (table_name, _) in iter_table_names_and_types(module, false) {
writeln!(client_h, "class U{}Table;", table_name.deref().to_case(Case::Pascal));
}
writeln!(client_h, "/***/");
Expand Down Expand Up @@ -958,7 +958,7 @@ impl Lang for UnrealCpp<'_> {
});

// Build table includes
let table_includes: Vec<String> = iter_table_names_and_types(module)
let table_includes: Vec<String> = iter_table_names_and_types(module, false)
.map(|(table_name, _)| {
format!(
"ModuleBindings/Tables/{}Table.g.h",
Expand Down Expand Up @@ -987,7 +987,7 @@ impl Lang for UnrealCpp<'_> {
});

// Generate .cpp implementation files for each table
for table in module.tables() {
for table in iter_tables(module, false) {
let schema = TableSchema::from_module_def(module, table, (), 0.into())
.validated()
.expect("table schema should validate");
Expand Down Expand Up @@ -2349,7 +2349,7 @@ fn generate_remote_tables_class(output: &mut UnrealCppAutogen, module: &ModuleDe
writeln!(output);

// Generate table handle properties
for (table_name, _) in iter_table_names_and_types(module) {
for (table_name, _) in iter_table_names_and_types(module, false) {
writeln!(output, " UPROPERTY(BlueprintReadOnly, Category=\"SpacetimeDB\")");
writeln!(
output,
Expand Down Expand Up @@ -3023,7 +3023,7 @@ fn generate_client_implementation(output: &mut UnrealCppAutogen, module: &Module
writeln!(output, "\tProcedures->Conn = this;");
writeln!(output);

for (table_name, product_type_ref) in iter_table_names_and_types(module) {
for (table_name, product_type_ref) in iter_table_names_and_types(module, false) {
let struct_name = type_ref_name(module, product_type_ref);
let table_name = table_name.deref();
writeln!(
Expand Down Expand Up @@ -3079,7 +3079,7 @@ fn generate_client_implementation(output: &mut UnrealCppAutogen, module: &Module
writeln!(output, "{{");
writeln!(output);
writeln!(output, "\t/** Creating tables */");
for (table_name, _) in iter_table_names_and_types(module) {
for (table_name, _) in iter_table_names_and_types(module, false) {
writeln!(
output,
"\t{} = NewObject<U{}Table>(this);",
Expand All @@ -3090,7 +3090,7 @@ fn generate_client_implementation(output: &mut UnrealCppAutogen, module: &Module
writeln!(output, "\t/**/");
writeln!(output);
writeln!(output, "\t/** Initialization */");
for (table_name, _) in iter_table_names_and_types(module) {
for (table_name, _) in iter_table_names_and_types(module, false) {
writeln!(
output,
"\t{}->PostInitialize();",
Expand Down Expand Up @@ -3980,7 +3980,7 @@ fn collect_wrapper_types(module: &ModuleDef) -> (HashSet<String>, HashSet<(Strin
}

// Collect from all tables
for (_, product_type_ref) in iter_table_names_and_types(module) {
for (_, product_type_ref) in iter_table_names_and_types(module, false) {
let product_type = module.typespace_for_generate()[product_type_ref].as_product().unwrap();
for (_, field_ty) in &product_type.elements {
collect_from_type(module, field_ty, &mut optional_types, &mut result_types);
Expand Down
14 changes: 11 additions & 3 deletions crates/codegen/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::{
use super::code_indenter::Indenter;
use convert_case::{Case, Casing};
use itertools::Itertools;
use spacetimedb_lib::db::raw_def::v9::TableAccess;
use spacetimedb_lib::sats::layout::PrimitiveType;
use spacetimedb_lib::version;
use spacetimedb_lib::{db::raw_def::v9::Lifecycle, sats::AlgebraicTypeRef};
Expand Down Expand Up @@ -111,8 +112,11 @@ pub(super) fn iter_procedures(module: &ModuleDef) -> impl Iterator<Item = &Proce
/// Iterate over all the [`TableDef`]s defined by the module, in alphabetical order by name.
///
/// Sorting is necessary to have deterministic reproducible codegen.
pub(super) fn iter_tables(module: &ModuleDef) -> impl Iterator<Item = &TableDef> {
module.tables().sorted_by_key(|table| &table.name)
pub(super) fn iter_tables(module: &ModuleDef, include_private: bool) -> impl Iterator<Item = &TableDef> {
module
.tables()
.filter(move |table| include_private || table.table_access == TableAccess::Public)
.sorted_by_key(|table| &table.name)
}

/// Iterate over all the [`ViewDef`]s defined by the module, in alphabetical order by name.
Expand All @@ -125,9 +129,13 @@ pub(super) fn iter_views(module: &ModuleDef) -> impl Iterator<Item = &ViewDef> {
/// Iterate over the names of all the tables and views defined by the module, in alphabetical order.
///
/// Sorting is necessary to have deterministic reproducible codegen.
pub(super) fn iter_table_names_and_types(module: &ModuleDef) -> impl Iterator<Item = (&Identifier, AlgebraicTypeRef)> {
pub(super) fn iter_table_names_and_types(
module: &ModuleDef,
include_private: bool,
) -> impl Iterator<Item = (&Identifier, AlgebraicTypeRef)> {
module
.tables()
.filter(move |table| include_private || table.table_access == TableAccess::Public)
.map(|def| (&def.name, def.product_type_ref))
.chain(module.views().map(|def| (&def.name, def.product_type_ref)))
.sorted_by_key(|(name, _)| *name)
Expand Down
Loading
Loading