Skip to content

Commit

Permalink
Simplify reducer args construction
Browse files Browse the repository at this point in the history
  • Loading branch information
RReverser committed Dec 3, 2024
1 parent d0386b2 commit 3f5a30c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
9 changes: 4 additions & 5 deletions crates/cli/src/subcommands/generate/csharp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -656,12 +656,12 @@ pub fn autogen_csharp_globals(ctx: &GenCtx, items: &[GenItem], namespace: &str)
let delegate_separator = if !reducer.args.is_empty() { ", " } else { "" };

let mut func_params: String = String::new();
let mut field_inits: String = String::new();
let mut func_args: String = String::new();

for (arg_i, arg) in reducer.args.iter().enumerate() {
if arg_i != 0 {
func_params.push_str(", ");
field_inits.push_str(", ");
func_args.push_str(", ");
}

let name = arg
Expand All @@ -670,10 +670,9 @@ pub fn autogen_csharp_globals(ctx: &GenCtx, items: &[GenItem], namespace: &str)
.unwrap_or_else(|| panic!("reducer args should have names: {func_name}"));
let arg_type_str = ty_fmt(ctx, &arg.algebraic_type, namespace);
let arg_name = name.to_case(Case::Camel);
let field_name = name.to_case(Case::Pascal);

write!(func_params, "{arg_type_str} {arg_name}").unwrap();
write!(field_inits, "{field_name} = {arg_name}").unwrap();
write!(func_args, "{arg_name}").unwrap();
}

writeln!(
Expand All @@ -690,7 +689,7 @@ pub fn autogen_csharp_globals(ctx: &GenCtx, items: &[GenItem], namespace: &str)
indented_block(output, |output| {
writeln!(
output,
"conn.InternalCallReducer(new Reducer.{func_name_pascal_case} {{ {field_inits} }}, this.SetCallReducerFlags.{func_name_pascal_case}Flags);"
"conn.InternalCallReducer(new Reducer.{func_name_pascal_case}({func_args}), this.SetCallReducerFlags.{func_name_pascal_case}Flags);"
);
});
writeln!(output);
Expand Down
16 changes: 8 additions & 8 deletions crates/cli/tests/snapshots/codegen__codegen_csharp.snap
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ namespace SpacetimeDB

public void AddPlayer(string name)
{
conn.InternalCallReducer(new Reducer.AddPlayer { Name = name }, this.SetCallReducerFlags.AddPlayerFlags);
conn.InternalCallReducer(new Reducer.AddPlayer(name), this.SetCallReducerFlags.AddPlayerFlags);
}

public bool InvokeAddPlayer(EventContext ctx, Reducer.AddPlayer args)
Expand All @@ -737,7 +737,7 @@ namespace SpacetimeDB

public void AddPrivate(string name)
{
conn.InternalCallReducer(new Reducer.AddPrivate { Name = name }, this.SetCallReducerFlags.AddPrivateFlags);
conn.InternalCallReducer(new Reducer.AddPrivate(name), this.SetCallReducerFlags.AddPrivateFlags);
}

public bool InvokeAddPrivate(EventContext ctx, Reducer.AddPrivate args)
Expand All @@ -754,7 +754,7 @@ namespace SpacetimeDB

public void DeletePlayer(ulong id)
{
conn.InternalCallReducer(new Reducer.DeletePlayer { Id = id }, this.SetCallReducerFlags.DeletePlayerFlags);
conn.InternalCallReducer(new Reducer.DeletePlayer(id), this.SetCallReducerFlags.DeletePlayerFlags);
}

public bool InvokeDeletePlayer(EventContext ctx, Reducer.DeletePlayer args)
Expand All @@ -771,7 +771,7 @@ namespace SpacetimeDB

public void DeletePlayersByName(string name)
{
conn.InternalCallReducer(new Reducer.DeletePlayersByName { Name = name }, this.SetCallReducerFlags.DeletePlayersByNameFlags);
conn.InternalCallReducer(new Reducer.DeletePlayersByName(name), this.SetCallReducerFlags.DeletePlayersByNameFlags);
}

public bool InvokeDeletePlayersByName(EventContext ctx, Reducer.DeletePlayersByName args)
Expand All @@ -788,7 +788,7 @@ namespace SpacetimeDB

public void QueryPrivate()
{
conn.InternalCallReducer(new Reducer.QueryPrivate { }, this.SetCallReducerFlags.QueryPrivateFlags);
conn.InternalCallReducer(new Reducer.QueryPrivate(), this.SetCallReducerFlags.QueryPrivateFlags);
}

public bool InvokeQueryPrivate(EventContext ctx, Reducer.QueryPrivate args)
Expand All @@ -804,7 +804,7 @@ namespace SpacetimeDB

public void RepeatingTest(SpacetimeDB.RepeatingTestArg arg)
{
conn.InternalCallReducer(new Reducer.RepeatingTest { Arg = arg }, this.SetCallReducerFlags.RepeatingTestFlags);
conn.InternalCallReducer(new Reducer.RepeatingTest(arg), this.SetCallReducerFlags.RepeatingTestFlags);
}

public bool InvokeRepeatingTest(EventContext ctx, Reducer.RepeatingTest args)
Expand All @@ -821,7 +821,7 @@ namespace SpacetimeDB

public void Test(SpacetimeDB.TestA arg, SpacetimeDB.TestB arg2, SpacetimeDB.Namespace.Types.TestC arg3, SpacetimeDB.Namespace.TestF arg4)
{
conn.InternalCallReducer(new Reducer.Test { Arg = arg, Arg2 = arg2, Arg3 = arg3, Arg4 = arg4 }, this.SetCallReducerFlags.TestFlags);
conn.InternalCallReducer(new Reducer.Test(arg, arg2, arg3, arg4), this.SetCallReducerFlags.TestFlags);
}

public bool InvokeTest(EventContext ctx, Reducer.Test args)
Expand All @@ -841,7 +841,7 @@ namespace SpacetimeDB

public void TestBtreeIndexArgs()
{
conn.InternalCallReducer(new Reducer.TestBtreeIndexArgs { }, this.SetCallReducerFlags.TestBtreeIndexArgsFlags);
conn.InternalCallReducer(new Reducer.TestBtreeIndexArgs(), this.SetCallReducerFlags.TestBtreeIndexArgsFlags);
}

public bool InvokeTestBtreeIndexArgs(EventContext ctx, Reducer.TestBtreeIndexArgs args)
Expand Down

0 comments on commit 3f5a30c

Please sign in to comment.