Skip to content

Commit

Permalink
generate types to SpacetimeDB.Types namespace (#88)
Browse files Browse the repository at this point in the history
* generate types to SpacetimeDB.Types namespace

* Add Reducer partial class to csharp codegen to tag it with the ReducerClass attribute

* Changes based on feedback

* Fix SpacetimeDB tests

* One more smoketest fix

---------

Co-authored-by: Derek Brinkmann <dbrinkmann@citadelstudios.net>
  • Loading branch information
dbrinkmanncw and dbrinkmann-citadel authored Jul 27, 2023
1 parent 83e88d8 commit ad7bc6c
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 16 deletions.
28 changes: 26 additions & 2 deletions crates/cli/src/subcommands/generate/csharp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1393,7 +1393,7 @@ pub fn autogen_csharp_reducer(ctx: &GenCtx, reducer: &ReducerDef, namespace: &st
output.into_inner()
}

pub fn autogen_csharp_globals(items: &[GenItem], namespace: &str) -> Vec<(String, String)> {
pub fn autogen_csharp_globals(items: &[GenItem], namespace: &str) -> Vec<Vec<(String, String)>> {
let reducers: Vec<&ReducerDef> = items
.iter()
.map(|i| {
Expand Down Expand Up @@ -1543,5 +1543,29 @@ pub fn autogen_csharp_globals(items: &[GenItem], namespace: &str) -> Vec<(String
writeln!(output, "}}").unwrap();
}

vec![("ReducerEvent.cs".into(), output.into_inner())]
let mut result = vec![vec![("ReducerEvent.cs".to_string(), output.into_inner())]];

let mut output = CodeIndenter::new(String::new());

writeln!(output, "using SpacetimeDB;").unwrap();

writeln!(output).unwrap();

if use_namespace {
writeln!(output, "namespace {}", namespace).unwrap();
writeln!(output, "{{").unwrap();
output.indent(1);
}

writeln!(output, "[ReducerClass]").unwrap();
writeln!(output, "public partial class Reducer {{ }}").unwrap();

if use_namespace {
output.dedent(1);
writeln!(output, "}}").unwrap();
}

result.push(vec![("Reducer.cs".into(), output.into_inner())]);

result
}
11 changes: 5 additions & 6 deletions crates/cli/src/subcommands/generate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ pub fn cli() -> clap::Command {
)
.arg(
Arg::new("namespace")
.default_value("SpacetimeDB")
.default_value("SpacetimeDB.Types")
.long("namespace")
.short('n')
.help("The namespace that should be used (default is 'SpacetimeDB')"),
.help("The namespace that should be used (default is 'SpacetimeDB.Types')"),
)
.arg(
Arg::new("lang")
Expand Down Expand Up @@ -159,13 +159,12 @@ pub fn generate<'a>(wasm_file: &'a Path, lang: Language, namespace: &'a str) ->
.iter()
.filter_map(|item| item.generate(&ctx, lang, namespace))
.collect();
for global in generate_globals(&ctx, lang, namespace, &items) {
files.push(global);
}
files.extend(generate_globals(&ctx, lang, namespace, &items).into_iter().flatten());

Ok(files)
}

fn generate_globals(ctx: &GenCtx, lang: Language, namespace: &str, items: &[GenItem]) -> Vec<(String, String)> {
fn generate_globals(ctx: &GenCtx, lang: Language, namespace: &str, items: &[GenItem]) -> Vec<Vec<(String, String)>> {
match lang {
Language::Csharp => csharp::autogen_csharp_globals(items, namespace),
Language::TypeScript => typescript::autogen_typescript_globals(ctx, items),
Expand Down
2 changes: 1 addition & 1 deletion crates/cli/src/subcommands/generate/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,6 @@ pub fn autogen_python_reducer(ctx: &GenCtx, reducer: &ReducerDef) -> String {
output.into_inner()
}

pub fn autogen_python_globals(_ctx: &GenCtx, _items: &[GenItem]) -> Vec<(String, String)> {
pub fn autogen_python_globals(_ctx: &GenCtx, _items: &[GenItem]) -> Vec<Vec<(String, String)>> {
vec![] //TODO
}
4 changes: 2 additions & 2 deletions crates/cli/src/subcommands/generate/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,7 @@ pub fn autogen_rust_reducer(ctx: &GenCtx, reducer: &ReducerDef) -> String {
/// to connect to a remote database, and passes the `handle_row_update`
/// and `handle_event` functions so the `BackgroundDbConnection` can spawn workers
/// which use those functions to dispatch on the content of messages.
pub fn autogen_rust_globals(ctx: &GenCtx, items: &[GenItem]) -> Vec<(String, String)> {
pub fn autogen_rust_globals(ctx: &GenCtx, items: &[GenItem]) -> Vec<Vec<(String, String)>> {
let mut output = CodeIndenter::new(String::new());
let out = &mut output;

Expand Down Expand Up @@ -823,7 +823,7 @@ pub fn autogen_rust_globals(ctx: &GenCtx, items: &[GenItem]) -> Vec<(String, Str
// Define `fn connect`.
print_connect_defn(out);

vec![("mod.rs".to_string(), output.into_inner())]
vec![vec![("mod.rs".to_string(), output.into_inner())]]
}

/// Extra imports required by the `mod.rs` file, in addition to the [`SPACETIMEDB_IMPORTS`].
Expand Down
2 changes: 1 addition & 1 deletion crates/cli/src/subcommands/generate/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1312,6 +1312,6 @@ pub fn autogen_typescript_reducer(ctx: &GenCtx, reducer: &ReducerDef) -> String
output.into_inner()
}

pub fn autogen_typescript_globals(_ctx: &GenCtx, _items: &[GenItem]) -> Vec<(String, String)> {
pub fn autogen_typescript_globals(_ctx: &GenCtx, _items: &[GenItem]) -> Vec<Vec<(String, String)>> {
vec![] //TODO
}
4 changes: 2 additions & 2 deletions test/tests/namespace-custom-cs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ NAMESPACE=$(random_string)
run_test cargo run generate --out-dir "${TMP_DIR}" --lang cs --namespace "${NAMESPACE}" --project-path "${PROJECT_PATH}"

LINES="$(grep -r -o "namespace ${NAMESPACE}" "${TMP_DIR}" | wc -l | tr -d ' ')"
if [ "${LINES}" != 4 ] ; then
if [ "${LINES}" != 5 ] ; then
echo "FOUND: ${LINES} EXPECTED: "
exit 1
fi

LINES="$(grep -r -o "using SpacetimeDB;" "${TMP_DIR}" | wc -l | tr -d ' ')"
if [ "${LINES}" != 4 ] ; then
if [ "${LINES}" != 5 ] ; then
echo "FOUND: ${LINES} EXPECTED: "
exit 1
fi
Expand Down
4 changes: 2 additions & 2 deletions test/tests/namespace-spacetimedb-cs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ set -euox pipefail
source "./test/lib.include"

TMP_DIR=$(mktemp -d)
NAMESPACE=SpacetimeDB
NAMESPACE=SpacetimeDB.Types

run_test cargo run generate --out-dir "${TMP_DIR}" --lang cs --project-path "${PROJECT_PATH}"

LINES="$(grep -r -o "namespace ${NAMESPACE}" "${TMP_DIR}" | wc -l | tr -d ' ')"
if [ "${LINES}" != 4 ] ; then
if [ "${LINES}" != 5 ] ; then
echo "FOUND: ${LINES} EXPECTED: "
exit 1
fi
Expand Down

0 comments on commit ad7bc6c

Please sign in to comment.