Skip to content

Commit c8cac1d

Browse files
committed
Add sub-module mounts support + codegen
1 parent d31301a commit c8cac1d

17 files changed

Lines changed: 1420 additions & 81 deletions

File tree

crates/bindings-cpp/include/spacetimedb/internal/autogen/RawModuleDef.g.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
#include <memory>
1313
#include "../autogen_base.h"
1414
#include "spacetimedb/bsatn/bsatn.h"
15+
#include "RawModuleDefV10.g.h"
1516
#include "RawModuleDefV8.g.h"
1617
#include "RawModuleDefV9.g.h"
17-
#include "RawModuleDefV10.g.h"
1818

1919
namespace SpacetimeDB::Internal {
2020

crates/bindings-cpp/include/spacetimedb/internal/autogen/RawModuleDefV10Section.g.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@
2525
#include "RawTableDefV10.g.h"
2626
#include "RawRowLevelSecurityDefV9.g.h"
2727
#include "RawHttpRouteDefV10.g.h"
28+
#include "RawModuleMountV10.g.h"
2829

2930
namespace SpacetimeDB::Internal {
3031

31-
SPACETIMEDB_INTERNAL_TAGGED_ENUM(RawModuleDefV10Section, SpacetimeDB::Internal::Typespace, std::vector<SpacetimeDB::Internal::RawTypeDefV10>, std::vector<SpacetimeDB::Internal::RawTableDefV10>, std::vector<SpacetimeDB::Internal::RawReducerDefV10>, std::vector<SpacetimeDB::Internal::RawProcedureDefV10>, std::vector<SpacetimeDB::Internal::RawViewDefV10>, std::vector<SpacetimeDB::Internal::RawScheduleDefV10>, std::vector<SpacetimeDB::Internal::RawLifeCycleReducerDefV10>, std::vector<SpacetimeDB::Internal::RawRowLevelSecurityDefV9>, SpacetimeDB::Internal::CaseConversionPolicy, SpacetimeDB::Internal::ExplicitNames, std::vector<SpacetimeDB::Internal::RawHttpHandlerDefV10>, std::vector<SpacetimeDB::Internal::RawHttpRouteDefV10>)
32+
SPACETIMEDB_INTERNAL_TAGGED_ENUM(RawModuleDefV10Section, SpacetimeDB::Internal::Typespace, std::vector<SpacetimeDB::Internal::RawTypeDefV10>, std::vector<SpacetimeDB::Internal::RawTableDefV10>, std::vector<SpacetimeDB::Internal::RawReducerDefV10>, std::vector<SpacetimeDB::Internal::RawProcedureDefV10>, std::vector<SpacetimeDB::Internal::RawViewDefV10>, std::vector<SpacetimeDB::Internal::RawScheduleDefV10>, std::vector<SpacetimeDB::Internal::RawLifeCycleReducerDefV10>, std::vector<SpacetimeDB::Internal::RawRowLevelSecurityDefV9>, SpacetimeDB::Internal::CaseConversionPolicy, SpacetimeDB::Internal::ExplicitNames, std::vector<SpacetimeDB::Internal::RawHttpHandlerDefV10>, std::vector<SpacetimeDB::Internal::RawHttpRouteDefV10>, std::vector<SpacetimeDB::Internal::RawModuleMountV10>)
3233
} // namespace SpacetimeDB::Internal
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE
2+
// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD.
3+
4+
// This was generated using spacetimedb codegen.
5+
6+
#pragma once
7+
8+
#include <cstdint>
9+
#include <string>
10+
#include <vector>
11+
#include <optional>
12+
#include <memory>
13+
#include "../autogen_base.h"
14+
#include "spacetimedb/bsatn/bsatn.h"
15+
16+
// Forward declaration breaks the circular include chain:
17+
// RawModuleMountV10 -> RawModuleDefV10 -> RawModuleDefV10Section -> RawModuleMountV10
18+
namespace SpacetimeDB::Internal { struct RawModuleDefV10; }
19+
20+
namespace SpacetimeDB::Internal {
21+
22+
SPACETIMEDB_INTERNAL_PRODUCT_TYPE(RawModuleMountV10) {
23+
std::string namespace_; // renamed: 'namespace' is a C++ keyword
24+
std::shared_ptr<SpacetimeDB::Internal::RawModuleDefV10> module; // shared_ptr breaks infinite-size recursion
25+
26+
void bsatn_serialize(::SpacetimeDB::bsatn::Writer& writer) const {
27+
::SpacetimeDB::bsatn::serialize(writer, namespace_);
28+
if (module) ::SpacetimeDB::bsatn::serialize(writer, *module);
29+
}
30+
bool operator==(const RawModuleMountV10& o) const noexcept {
31+
if (namespace_ != o.namespace_) return false;
32+
if (module && o.module) return *module == *o.module;
33+
return !module && !o.module;
34+
}
35+
bool operator!=(const RawModuleMountV10& o) const noexcept { return !(*this == o); }
36+
};
37+
} // namespace SpacetimeDB::Internal

crates/bindings-cpp/include/spacetimedb/internal/autogen/RawReducerDefV9.g.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
#include <memory>
1313
#include "../autogen_base.h"
1414
#include "spacetimedb/bsatn/bsatn.h"
15-
#include "Lifecycle.g.h"
1615
#include "ProductType.g.h"
16+
#include "Lifecycle.g.h"
1717

1818
namespace SpacetimeDB::Internal {
1919

crates/bindings-csharp/Runtime/Internal/Autogen/RawModuleDefV10Section.g.cs

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/bindings-csharp/Runtime/Internal/Autogen/RawModuleMountV10.g.cs

Lines changed: 36 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/bindings-typescript/src/lib/autogen/types.ts

Lines changed: 51 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/bindings-typescript/src/lib/schema.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,22 @@ import {
77
} from './algebraic_type';
88
import type {
99
CaseConversionPolicy,
10+
ExplicitNames,
11+
RawHttpHandlerDefV10,
12+
RawHttpRouteDefV10,
13+
RawLifeCycleReducerDefV10,
14+
RawModuleMountV10,
1015
RawModuleDefV10,
1116
RawModuleDefV10Section,
17+
RawProcedureDefV10,
18+
RawReducerDefV10,
19+
RawRowLevelSecurityDefV9,
20+
RawScheduleDefV10,
1221
RawScopedTypeNameV10,
1322
RawTableDefV10,
23+
RawTypeDefV10,
24+
RawViewDefV10,
25+
Typespace,
1426
} from './autogen/types';
1527
import type { UntypedIndex } from './indexes';
1628
import type { UntypedTableDef } from './table';
@@ -42,6 +54,7 @@ export type TableNamesOf<S extends UntypedSchemaDef> = Values<
4254
*/
4355
export type UntypedSchemaDef = {
4456
tables: Record<string, UntypedTableDef>;
57+
namespaces?: Record<string, UntypedSchemaDef>;
4558
};
4659

4760
/**
@@ -174,7 +187,20 @@ type CompoundTypeCache = Map<
174187
>;
175188

176189
export type ModuleDef = {
177-
[S in RawModuleDefV10Section as Uncapitalize<S['tag']>]: S['value'];
190+
typespace: Typespace;
191+
types: RawTypeDefV10[];
192+
tables: RawTableDefV10[];
193+
reducers: RawReducerDefV10[];
194+
procedures: RawProcedureDefV10[];
195+
views: RawViewDefV10[];
196+
schedules: RawScheduleDefV10[];
197+
lifeCycleReducers: RawLifeCycleReducerDefV10[];
198+
httpHandlers: RawHttpHandlerDefV10[];
199+
httpRoutes: RawHttpRouteDefV10[];
200+
rowLevelSecurity: RawRowLevelSecurityDefV9[];
201+
caseConversionPolicy: CaseConversionPolicy;
202+
explicitNames: ExplicitNames;
203+
mounts: RawModuleMountV10[];
178204
};
179205

180206
type Section = RawModuleDefV10Section;
@@ -201,6 +227,7 @@ export class ModuleContext {
201227
explicitNames: {
202228
entries: [],
203229
},
230+
mounts: [],
204231
};
205232

206233
get moduleDef(): ModuleDef {
@@ -259,9 +286,19 @@ export class ModuleContext {
259286
value: module.caseConversionPolicy,
260287
}
261288
);
289+
push(
290+
module.mounts && {
291+
tag: 'Mounts',
292+
value: module.mounts,
293+
}
294+
);
262295
return { sections };
263296
}
264297

298+
addMount(mount: RawModuleMountV10) {
299+
this.#moduleDef.mounts.push(mount);
300+
}
301+
265302
/**
266303
* Set the case conversion policy for this module.
267304
* Called by the settings mechanism.

crates/codegen/src/cpp.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,53 @@ impl<'opts> Cpp<'opts> {
231231
writeln!(output, "}};").unwrap();
232232
}
233233

234+
fn generate_raw_module_mount_v10_special(&self) -> String {
235+
// RawModuleMountV10 is special for two reasons:
236+
// 1. Its `namespace` field is a C++ keyword, renamed to `namespace_`.
237+
// 2. It contains `RawModuleDefV10` which creates a circular include chain:
238+
// RawModuleMountV10 → RawModuleDefV10 → RawModuleDefV10Section → RawModuleMountV10
239+
// We break this with a forward declaration and shared_ptr (which only needs a declaration).
240+
r#"// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE
241+
// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD.
242+
243+
// This was generated using spacetimedb codegen.
244+
245+
#pragma once
246+
247+
#include <cstdint>
248+
#include <string>
249+
#include <vector>
250+
#include <optional>
251+
#include <memory>
252+
#include "../autogen_base.h"
253+
#include "spacetimedb/bsatn/bsatn.h"
254+
255+
// Forward declaration breaks the circular include chain:
256+
// RawModuleMountV10 -> RawModuleDefV10 -> RawModuleDefV10Section -> RawModuleMountV10
257+
namespace SpacetimeDB::Internal { struct RawModuleDefV10; }
258+
259+
namespace SpacetimeDB::Internal {
260+
261+
SPACETIMEDB_INTERNAL_PRODUCT_TYPE(RawModuleMountV10) {
262+
std::string namespace_; // renamed: 'namespace' is a C++ keyword
263+
std::shared_ptr<SpacetimeDB::Internal::RawModuleDefV10> module; // shared_ptr breaks infinite-size recursion
264+
265+
void bsatn_serialize(::SpacetimeDB::bsatn::Writer& writer) const {
266+
::SpacetimeDB::bsatn::serialize(writer, namespace_);
267+
if (module) ::SpacetimeDB::bsatn::serialize(writer, *module);
268+
}
269+
bool operator==(const RawModuleMountV10& o) const noexcept {
270+
if (namespace_ != o.namespace_) return false;
271+
if (module && o.module) return *module == *o.module;
272+
return !module && !o.module;
273+
}
274+
bool operator!=(const RawModuleMountV10& o) const noexcept { return !(*this == o); }
275+
};
276+
} // namespace SpacetimeDB::Internal
277+
"#
278+
.to_string()
279+
}
280+
234281
// Generate minimal sum type (TaggedEnum only)
235282
fn write_sum_type(&self, output: &mut String, module: &ModuleDef, type_name: &str, sum: &SumTypeDef) {
236283
// Special case: Generate proper tagged enum for RawIndexAlgorithm with data variants
@@ -529,6 +576,17 @@ impl Lang for Cpp<'_> {
529576
}];
530577
}
531578

579+
// Special handling for RawModuleMountV10:
580+
// (1) its `namespace` field is a C++ keyword; (2) its `module` field creates a
581+
// circular include chain through RawModuleDefV10 → RawModuleDefV10Section.
582+
// We break both with a forward declaration and shared_ptr.
583+
if name.to_string() == "RawModuleMountV10" {
584+
return vec![OutputFile {
585+
filename: format!("{name}.g.h"),
586+
code: self.generate_raw_module_mount_v10_special(),
587+
}];
588+
}
589+
532590
self.write_standard_includes(&mut output);
533591

534592
// Add includes for dependencies

0 commit comments

Comments
 (0)