SpacetimeDB icon indicating copy to clipboard operation
SpacetimeDB copied to clipboard

C# and Rust modules have different server-side reducer names

Open kazimuth opened this issue 11 months ago • 8 comments

Noticed this while playing with Blackholio.

Swapping between the C# and Rust versions of the modules, the generated code is ALMOST identical. In the C# client code, the user can always call reducers like .EnterGame() and have it work.

The difference is that, if the C# version of the module is used, under the hood, .EnterGame() sends a message containing the string "EnterGame". On the other hand, if the Rust version of the module is used, under the hood, .EnterGame() send a message containing the string "enter_game". This means that the user needs to regenerate client code when swapping between the modules, despite there not being any user-facing API changes.

If we want to fix this, we could canonicalize reducer names in a module to either a snake_case or a CamelCase convention.

kazimuth avatar Jan 23 '25 18:01 kazimuth

Swapping between the C# and Rust versions of the modules, the generated code is ALMOST identical. In the C# client code, the user can always call reducers like .EnterGame() and have it work.

FWIW you could just rename C# methods to match Rust ones - that's what we do for ModuleDef equivalence comparisons between C#/Rust modules in tests.

RReverser avatar Jan 27 '25 16:01 RReverser

In the future we may choose to add a feature like this that allows users to choose the server-side reducer name, e.g.:

#[reducer(name = EnterGame)]
fn enter_game()

bfops avatar Feb 18 '25 20:02 bfops

@gefjon do you have thoughts about this?

bfops avatar Feb 18 '25 20:02 bfops

Nope!

gefjon avatar Feb 18 '25 20:02 gefjon

if we run out of time we'll move this to P2, but we think we should do this.

@cloutiertyler asked to assign this to @Centril (we think @coolreader18 could probably do this quickly as a backup)

bfops avatar Feb 18 '25 20:02 bfops

~~We are going to do canonicalize to snake_case and we're going to try to do this if we have time.~~ ~~We are not going to canonicalize this. In the future we are going to allow you to specify a name field for reducers~~

JK we are not going to canonicalize this. We are going to have all module libraries submit snake_case names by default as the name.

In the future, you will be able to set the name that the library provides to be whatever you want:

#[reducer(name = "EnterGame")]
fn enter_game()
#[Reducer(name = "EnterGame")]
void EnterGame()

cloutiertyler avatar Feb 18 '25 20:02 cloutiertyler

We think this #[reducer(...)] syntax already exists, at least in Rust. We should confirm in C#.

bfops avatar Feb 18 '25 20:02 bfops

WONTFIX. Down the line, we may make it so that spacetime call and friends do automatic case-conversion behind the scenes, "do-what-i-mean" style, so long as there's no ambiguity caused by name collisions. We will also do something to make it so that modules by default submit snake-case names. What specifically we will do is yet to be determined.

gefjon avatar Feb 25 '25 19:02 gefjon

We'll address this in Blackholio by not checking in the bindings, that way there's a mandatory user step of "generate the bindings"

bfops avatar Jul 28 '25 18:07 bfops