Skip to content

Commit c59ee1d

Browse files
[2.0 Breaking] Add --include-private and default private tables to not generate (#4241)
# Description of Changes Updated the codegen table/function iteration functions to take in a parameter to check visibility in all locations for the supported languages. - Updated the util.rs functions for iterating tables/functions to check for a CodegenVisibility enum (IncludePrivate, or OnlyPublic) - Added a new CodegenOptions struct to pass around the CodegenVisibility and future flags, defaulted visibility to OnlyPublic - Updated the CLI to return a list of all private tables not included (added a TODO to check the --include-private opt): ```bash Optimising module with wasm-opt... Build finished successfully. Skipping private tables during codegen: secret_note, secret_order, secret_person. Generate finished successfully. ``` # API and ABI breaking changes Technically API breaking as the private tables will no longer be available. (GitHub labels are not working at the moment) # Expected complexity level and risk 1 - Simple change the testing took longer # Testing Turns out when you remove private tables you invalidate most of the module_bindings across the system! - [x] Rust test SDK for all languages - [x] C# SDK tests - [x] C# dotnet tests - [x] Updated and checked snap files - [x] Updated Blackholio (Unreal + Unity) module_bindings and tested - [x] Ran Unreal SDK tests --------- Signed-off-by: Jason Larabie <jason@clockworklabs.io>
1 parent e4ae7c4 commit c59ee1d

73 files changed

Lines changed: 748 additions & 4902 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,10 @@ new.json
234234
# C++ build data
235235
modules/benchmarks-cpp/build/
236236
modules/module-test-cpp/build/
237+
modules/sdk-test-cpp/build/
238+
modules/sdk-test-connect-disconnect-cpp/build/
239+
modules/sdk-test-procedure-cpp/build/
240+
modules/sdk-test-view-cpp/build/
237241

238242
# Symlinked output from `nix build`
239243
result

crates/cli/src/subcommands/generate.rs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
use anyhow::Context;
44
use clap::parser::ValueSource;
55
use clap::Arg;
6-
use clap::ArgAction::Set;
6+
use clap::ArgAction::{Set, SetTrue};
77
use fs_err as fs;
8-
use spacetimedb_codegen::{generate, Csharp, Lang, OutputFile, Rust, TypeScript, UnrealCpp, AUTO_GENERATED_PREFIX};
8+
use spacetimedb_codegen::{
9+
generate, private_table_names, CodegenOptions, CodegenVisibility, Csharp, Lang, OutputFile, Rust, TypeScript,
10+
UnrealCpp, AUTO_GENERATED_PREFIX,
11+
};
912
use spacetimedb_lib::de::serde::DeserializeWrapper;
1013
use spacetimedb_lib::{sats, RawModuleDef};
1114
use spacetimedb_schema;
@@ -25,7 +28,7 @@ use std::io::Read;
2528
pub fn cli() -> clap::Command {
2629
clap::Command::new("generate")
2730
.about("Generate client files for a spacetime module.")
28-
.override_usage("spacetime generate --lang <LANG> --out-dir <DIR> [--project-path <DIR> | --bin-path <PATH> | --module-name <MODULE_NAME> | --uproject-dir <DIR>]")
31+
.override_usage("spacetime generate --lang <LANG> --out-dir <DIR> [--project-path <DIR> | --bin-path <PATH> | --module-name <MODULE_NAME> | --uproject-dir <DIR> | --include-private]")
2932
.arg(
3033
Arg::new("wasm_file")
3134
.value_parser(clap::value_parser!(PathBuf))
@@ -109,6 +112,13 @@ pub fn cli() -> clap::Command {
109112
.default_value("")
110113
.help("Options to pass to the build command, for example --build-options='--lint-dir='"),
111114
)
115+
.arg(
116+
Arg::new("include_private")
117+
.long("include-private")
118+
.action(SetTrue)
119+
.default_value("false")
120+
.help("Include private tables and functions in generated code (types are always included)."),
121+
)
112122
.arg(common_args::yes())
113123
.after_help("Run `spacetime help publish` for more detailed information.")
114124
.group(
@@ -137,6 +147,7 @@ pub async fn exec_ex(
137147
let module_name = args.get_one::<String>("module_name");
138148
let force = args.get_flag("force");
139149
let build_options = args.get_one::<String>("build_options").unwrap();
150+
let include_private = args.get_flag("include_private");
140151

141152
if args.value_source("namespace") == Some(ValueSource::CommandLine) && lang != Language::Csharp {
142153
return Err(anyhow::anyhow!("--namespace is only supported with --lang csharp"));
@@ -171,6 +182,16 @@ pub async fn exec_ex(
171182
extract_descriptions(&path).context("could not extract schema")?
172183
};
173184

185+
let private_tables = private_table_names(&module);
186+
if !private_tables.is_empty() && !include_private {
187+
println!("Skipping private tables during codegen: {}.", private_tables.join(", "));
188+
}
189+
190+
let mut options = CodegenOptions::default();
191+
if include_private {
192+
options.visibility = CodegenVisibility::IncludePrivate;
193+
}
194+
174195
fs::create_dir_all(out_dir)?;
175196

176197
let mut paths = BTreeSet::new();
@@ -193,7 +214,7 @@ pub async fn exec_ex(
193214
Language::TypeScript => &TypeScript,
194215
};
195216

196-
for OutputFile { filename, code } in generate(&module, gen_lang) {
217+
for OutputFile { filename, code } in generate(&module, gen_lang, &options) {
197218
let fname = Path::new(&filename);
198219
// If a generator asks for a file in a subdirectory, create the subdirectory first.
199220
if let Some(parent) = fname.parent().filter(|p| !p.as_os_str().is_empty()) {

crates/codegen/examples/regen-cpp-moduledef.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//! Run `cargo run --example regen-cpp-moduledef` to update C++ bindings whenever the module definition changes.
33
44
use fs_err as fs;
5-
use spacetimedb_codegen::{cpp, generate, OutputFile};
5+
use spacetimedb_codegen::{cpp, generate, CodegenOptions, OutputFile};
66
use spacetimedb_lib::db::raw_def::v9::{RawModuleDefV9, RawModuleDefV9Builder};
77
use spacetimedb_schema::def::ModuleDef;
88
use std::path::Path;
@@ -33,6 +33,7 @@ fn main() -> anyhow::Result<()> {
3333
&cpp::Cpp {
3434
namespace: "SpacetimeDB::Internal",
3535
},
36+
&CodegenOptions::default(),
3637
)
3738
.into_iter()
3839
.try_for_each(|OutputFile { filename, code }| {

crates/codegen/examples/regen-csharp-moduledef.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
44
use fs_err as fs;
55
use regex::Regex;
6-
use spacetimedb_codegen::{csharp, generate, OutputFile};
6+
use spacetimedb_codegen::{csharp, generate, CodegenOptions, OutputFile};
77
use spacetimedb_lib::{RawModuleDef, RawModuleDefV8};
88
use spacetimedb_schema::def::ModuleDef;
99
use std::path::Path;
@@ -37,6 +37,7 @@ fn main() -> anyhow::Result<()> {
3737
&csharp::Csharp {
3838
namespace: "SpacetimeDB.Internal",
3939
},
40+
&CodegenOptions::default(),
4041
)
4142
.into_iter()
4243
.try_for_each(|OutputFile { filename, code }| {

crates/codegen/examples/regen-typescript-moduledef.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
use fs_err as fs;
77
use regex::Regex;
8-
use spacetimedb_codegen::{generate, typescript, OutputFile};
8+
use spacetimedb_codegen::{generate, typescript, CodegenOptions, OutputFile};
99
use spacetimedb_lib::db::raw_def::v9::ViewResultHeader;
1010
use spacetimedb_lib::{RawModuleDef, RawModuleDefV8};
1111
use spacetimedb_schema::def::ModuleDef;
@@ -38,7 +38,7 @@ fn main() -> anyhow::Result<()> {
3838
fs::create_dir(dir)?;
3939

4040
let module: ModuleDef = module.try_into()?;
41-
generate(&module, &typescript::TypeScript)
41+
generate(&module, &typescript::TypeScript, &CodegenOptions::default())
4242
.into_iter()
4343
.try_for_each(|OutputFile { filename, code }| {
4444
// Skip the index.ts since we don't need it.

crates/codegen/src/cpp.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Minimal C++ code generation for SpacetimeDB module definitions.
22
//! Generates only schema definitions - framework provides all functionality.
33
4+
use crate::CodegenOptions;
45
use crate::Lang;
56
use crate::OutputFile;
67
use spacetimedb_lib::sats::layout::PrimitiveType;
@@ -557,7 +558,7 @@ impl Lang for Cpp<'_> {
557558
}
558559
}
559560

560-
fn generate_global_files(&self, _module: &ModuleDef) -> Vec<OutputFile> {
561+
fn generate_global_files(&self, _module: &ModuleDef, _options: &CodegenOptions) -> Vec<OutputFile> {
561562
vec![]
562563
}
563564
}

crates/codegen/src/csharp.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::util::{
1111
collect_case, is_reducer_invokable, iter_indexes, iter_reducers, iter_table_names_and_types,
1212
print_auto_generated_file_comment, print_auto_generated_version_comment, type_ref_name,
1313
};
14-
use crate::{indent_scope, OutputFile};
14+
use crate::{indent_scope, CodegenOptions, OutputFile};
1515
use convert_case::{Case, Casing};
1616
use spacetimedb_lib::sats::layout::PrimitiveType;
1717
use spacetimedb_primitives::ColId;
@@ -1010,7 +1010,7 @@ impl Lang for Csharp<'_> {
10101010
}
10111011
}
10121012

1013-
fn generate_global_files(&self, module: &ModuleDef) -> Vec<OutputFile> {
1013+
fn generate_global_files(&self, module: &ModuleDef, options: &CodegenOptions) -> Vec<OutputFile> {
10141014
let mut output = CsharpAutogen::new(
10151015
self.namespace,
10161016
&[
@@ -1048,7 +1048,7 @@ impl Lang for Csharp<'_> {
10481048
indented_block(&mut output, |output| {
10491049
writeln!(output, "public RemoteTables(DbConnection conn)");
10501050
indented_block(output, |output| {
1051-
for (table_name, _) in iter_table_names_and_types(module) {
1051+
for (table_name, _) in iter_table_names_and_types(module, options.visibility) {
10521052
writeln!(
10531053
output,
10541054
"AddTable({} = new(conn));",
@@ -1071,7 +1071,7 @@ impl Lang for Csharp<'_> {
10711071

10721072
writeln!(output, "public sealed class From");
10731073
indented_block(&mut output, |output| {
1074-
for (table_name, product_type_ref) in iter_table_names_and_types(module) {
1074+
for (table_name, product_type_ref) in iter_table_names_and_types(module, options.visibility) {
10751075
let method_name = table_name.deref().to_case(Case::Pascal);
10761076
let row_type = type_ref_name(module, product_type_ref);
10771077
let table_name_lit = format!("{:?}", table_name.deref());
@@ -1176,7 +1176,7 @@ impl Lang for Csharp<'_> {
11761176
writeln!(output, "return update.ReducerCall.ReducerName switch {{");
11771177
{
11781178
indent_scope!(output);
1179-
for reducer in iter_reducers(module) {
1179+
for reducer in iter_reducers(module, options.visibility) {
11801180
let reducer_str_name = &reducer.name;
11811181
let reducer_name = reducer.name.deref().to_case(Case::Pascal);
11821182
writeln!(
@@ -1241,7 +1241,9 @@ impl Lang for Csharp<'_> {
12411241
writeln!(output, "return reducer switch {{");
12421242
{
12431243
indent_scope!(output);
1244-
for reducer_name in iter_reducers(module).map(|r| r.name.deref().to_case(Case::Pascal)) {
1244+
for reducer_name in
1245+
iter_reducers(module, options.visibility).map(|r| r.name.deref().to_case(Case::Pascal))
1246+
{
12451247
writeln!(
12461248
output,
12471249
"Reducer.{reducer_name} args => Reducers.Invoke{reducer_name}(eventContext, args),"

crates/codegen/src/lib.rs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,32 @@ pub use self::csharp::Csharp;
1212
pub use self::rust::Rust;
1313
pub use self::typescript::TypeScript;
1414
pub use self::unrealcpp::UnrealCpp;
15+
pub use util::private_table_names;
16+
pub use util::CodegenVisibility;
1517
pub use util::AUTO_GENERATED_PREFIX;
1618

17-
pub fn generate(module: &ModuleDef, lang: &dyn Lang) -> Vec<OutputFile> {
19+
#[derive(Clone, Copy, Debug)]
20+
pub struct CodegenOptions {
21+
pub visibility: CodegenVisibility,
22+
}
23+
24+
impl Default for CodegenOptions {
25+
fn default() -> Self {
26+
Self {
27+
visibility: CodegenVisibility::OnlyPublic,
28+
}
29+
}
30+
}
31+
32+
pub fn generate(module: &ModuleDef, lang: &dyn Lang, options: &CodegenOptions) -> Vec<OutputFile> {
1833
itertools::chain!(
19-
module.tables().map(|tbl| lang.generate_table_file(module, tbl)),
34+
util::iter_tables(module, options.visibility).map(|tbl| lang.generate_table_file(module, tbl)),
2035
module.views().map(|view| lang.generate_view_file(module, view)),
2136
module.types().flat_map(|typ| lang.generate_type_files(module, typ)),
22-
util::iter_reducers(module).map(|reducer| lang.generate_reducer_file(module, reducer)),
23-
util::iter_procedures(module).map(|procedure| lang.generate_procedure_file(module, procedure)),
24-
lang.generate_global_files(module),
37+
util::iter_reducers(module, options.visibility).map(|reducer| lang.generate_reducer_file(module, reducer)),
38+
util::iter_procedures(module, options.visibility)
39+
.map(|procedure| lang.generate_procedure_file(module, procedure)),
40+
lang.generate_global_files(module, options),
2541
)
2642
.collect()
2743
}
@@ -36,7 +52,7 @@ pub trait Lang {
3652
fn generate_type_files(&self, module: &ModuleDef, typ: &TypeDef) -> Vec<OutputFile>;
3753
fn generate_reducer_file(&self, module: &ModuleDef, reducer: &ReducerDef) -> OutputFile;
3854
fn generate_procedure_file(&self, module: &ModuleDef, procedure: &ProcedureDef) -> OutputFile;
39-
fn generate_global_files(&self, module: &ModuleDef) -> Vec<OutputFile>;
55+
fn generate_global_files(&self, module: &ModuleDef, options: &CodegenOptions) -> Vec<OutputFile>;
4056

4157
fn generate_table_file(&self, module: &ModuleDef, tbl: &TableDef) -> OutputFile {
4258
let schema = TableSchema::from_module_def(module, tbl, (), 0.into())

0 commit comments

Comments
 (0)