@@ -5,6 +5,7 @@ use std::fmt::{self, Write};
5
5
use std:: ops:: Deref ;
6
6
7
7
use convert_case:: { Case , Casing } ;
8
+ use itertools:: Itertools ;
8
9
use spacetimedb_lib:: sats:: { AlgebraicType , AlgebraicTypeRef , ArrayType , ProductType , SumType } ;
9
10
use spacetimedb_lib:: ReducerDef ;
10
11
use spacetimedb_primitives:: ColList ;
@@ -505,7 +506,7 @@ fn autogen_csharp_access_funcs_for_struct(
505
506
}
506
507
}
507
508
508
- pub fn autogen_csharp_reducer ( ctx : & GenCtx , reducer : & ReducerDef , namespace : & str ) -> String {
509
+ pub fn autogen_csharp_reducer ( ctx : & GenCtx , reducer_idx : usize , reducer : & ReducerDef , namespace : & str ) -> String {
509
510
let func_name = & * reducer. name ;
510
511
let func_name_pascal_case = func_name. to_case ( Case :: Pascal ) ;
511
512
@@ -515,7 +516,7 @@ pub fn autogen_csharp_reducer(ctx: &GenCtx, reducer: &ReducerDef, namespace: &st
515
516
writeln ! ( output, "[SpacetimeDB.Type]" ) ;
516
517
writeln ! ( output, "public partial class {func_name_pascal_case} : IReducerArgs" ) ;
517
518
indented_block ( & mut output, |output| {
518
- writeln ! ( output, "string IReducerArgs.ReducerName => \" {func_name} \" ;" ) ;
519
+ writeln ! ( output, "uint IReducerArgs.ReducerIndex => {reducer_idx} ;" ) ;
519
520
if !reducer. args . is_empty ( ) {
520
521
writeln ! ( output) ;
521
522
}
@@ -542,13 +543,17 @@ pub fn autogen_csharp_reducer(ctx: &GenCtx, reducer: &ReducerDef, namespace: &st
542
543
pub fn autogen_csharp_globals ( ctx : & GenCtx , items : & [ GenItem ] , namespace : & str ) -> Vec < ( String , String ) > {
543
544
let mut results = Vec :: new ( ) ;
544
545
545
- let tables = items. iter ( ) . filter_map ( |i| {
546
- if let GenItem :: Table ( table) = i {
547
- Some ( table)
548
- } else {
549
- None
550
- }
551
- } ) ;
546
+ let tables: Vec < _ > = items
547
+ . iter ( )
548
+ . filter_map ( |i| {
549
+ if let GenItem :: Table ( table) = i {
550
+ Some ( table)
551
+ } else {
552
+ None
553
+ }
554
+ } )
555
+ . sorted_by_key ( |t| & t. schema . table_name )
556
+ . collect ( ) ;
552
557
553
558
let reducers: Vec < & ReducerDef > = items
554
559
. iter ( )
@@ -559,6 +564,7 @@ pub fn autogen_csharp_globals(ctx: &GenCtx, items: &[GenItem], namespace: &str)
559
564
None
560
565
}
561
566
} )
567
+ . sorted_by_key ( |i| & i. name )
562
568
. collect ( ) ;
563
569
let reducer_names: Vec < String > = reducers
564
570
. iter ( )
@@ -569,7 +575,7 @@ pub fn autogen_csharp_globals(ctx: &GenCtx, items: &[GenItem], namespace: &str)
569
575
570
576
writeln ! ( output, "public sealed class RemoteTables" ) ;
571
577
indented_block ( & mut output, |output| {
572
- for table in tables {
578
+ for & table in & tables {
573
579
let schema = & table. schema ;
574
580
let name = & schema. table_name ;
575
581
let csharp_name = name. as_ref ( ) . to_case ( Case :: Pascal ) ;
@@ -766,8 +772,6 @@ pub fn autogen_csharp_globals(ctx: &GenCtx, items: &[GenItem], namespace: &str)
766
772
writeln ! ( output, "{reducer_name} {reducer_name}," ) ;
767
773
}
768
774
writeln ! ( output, "Unit StdbNone," ) ;
769
- writeln ! ( output, "Unit StdbIdentityConnected," ) ;
770
- writeln ! ( output, "Unit StdbIdentityDisconnected" ) ;
771
775
}
772
776
writeln ! ( output, ")>;" ) ;
773
777
@@ -787,43 +791,33 @@ pub fn autogen_csharp_globals(ctx: &GenCtx, items: &[GenItem], namespace: &str)
787
791
writeln ! ( output, "Reducers = new(this, this.SetReducerFlags);" ) ;
788
792
writeln ! ( output) ;
789
793
790
- for item in items {
791
- if let GenItem :: Table ( table) = item {
792
- writeln ! (
793
- output,
794
- "clientDB.AddTable<{table_type}>(\" {table_name}\" , Db.{csharp_table_name});" ,
795
- table_type = csharp_typename( ctx, table. data) ,
796
- table_name = table. schema. table_name,
797
- csharp_table_name = table. schema. table_name. as_ref( ) . to_case( Case :: Pascal )
798
- ) ;
799
- }
794
+ for ( table_idx, & table) in tables. iter ( ) . enumerate ( ) {
795
+ writeln ! (
796
+ output,
797
+ "clientDB.AddTable<{table_type}>({table_idx}, Db.{csharp_table_name});" ,
798
+ table_type = csharp_typename( ctx, table. data) ,
799
+ csharp_table_name = table. schema. table_name. as_ref( ) . to_case( Case :: Pascal )
800
+ ) ;
800
801
}
801
802
} ) ;
802
803
writeln ! ( output) ;
803
804
804
- writeln ! ( output, "protected override Reducer ToReducer(TransactionUpdate update)" ) ;
805
+ writeln ! (
806
+ output,
807
+ "protected override Reducer ToReducer(uint reducerIdx, TransactionUpdate update)"
808
+ ) ;
805
809
indented_block ( output, |output| {
806
810
writeln ! ( output, "var encodedArgs = update.ReducerCall.Args;" ) ;
807
- writeln ! ( output, "return update.ReducerCall.ReducerName switch {{" ) ;
811
+ writeln ! ( output, "return reducerIdx switch {{" ) ;
808
812
{
809
813
indent_scope ! ( output) ;
810
- for ( reducer, reducer_name) in std:: iter:: zip ( & reducers, & reducer_names) {
811
- let reducer_str_name = & reducer. name ;
814
+ for ( reducer_idx, reducer_name) in reducer_names. iter ( ) . enumerate ( ) {
812
815
writeln ! (
813
816
output,
814
- "\" {reducer_str_name} \" => new Reducer.{reducer_name}(BSATNHelpers.Decode<{reducer_name}>(encodedArgs)),"
817
+ "{reducer_idx} => new Reducer.{reducer_name}(BSATNHelpers.Decode<{reducer_name}>(encodedArgs)),"
815
818
) ;
816
819
}
817
- writeln ! ( output, "\" <none>\" => new Reducer.StdbNone(default)," ) ;
818
- writeln ! (
819
- output,
820
- "\" __identity_connected__\" => new Reducer.StdbIdentityConnected(default),"
821
- ) ;
822
- writeln ! (
823
- output,
824
- "\" __identity_disconnected__\" => new Reducer.StdbIdentityDisconnected(default),"
825
- ) ;
826
- writeln ! ( output, "\" \" => new Reducer.StdbNone(default)," ) ; //Transaction from CLI command
820
+ writeln ! ( output, "4294967295 => new Reducer.StdbNone(default)," ) ;
827
821
writeln ! (
828
822
output,
829
823
r#"var reducer => throw new ArgumentOutOfRangeException("Reducer", $"Unknown reducer {{reducer}}")"#
@@ -855,9 +849,7 @@ pub fn autogen_csharp_globals(ctx: &GenCtx, items: &[GenItem], namespace: &str)
855
849
"Reducer.{reducer_name}(var args) => Reducers.Invoke{reducer_name}(eventContext, args),"
856
850
) ;
857
851
}
858
- writeln ! ( output, "Reducer.StdbNone or" ) ;
859
- writeln ! ( output, "Reducer.StdbIdentityConnected or" ) ;
860
- writeln ! ( output, "Reducer.StdbIdentityDisconnected => true," ) ;
852
+ writeln ! ( output, "Reducer.StdbNone => true," ) ;
861
853
writeln ! (
862
854
output,
863
855
r#"_ => throw new ArgumentOutOfRangeException("Reducer", $"Unknown reducer {{reducer}}")"#
0 commit comments