spacetimedb-codegen 1.3.0

Client codegen for SpacetimeDB
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
// Note: the generated code depends on APIs and interfaces from crates/bindings-csharp/BSATN.Runtime.
use super::util::fmt_fn;

use std::fmt::{self, Write};
use std::ops::Deref;

use super::code_indenter::CodeIndenter;
use super::Lang;
use crate::indent_scope;
use crate::util::{
    collect_case, is_reducer_invokable, iter_indexes, iter_reducers, iter_tables, print_auto_generated_file_comment,
    type_ref_name,
};
use convert_case::{Case, Casing};
use spacetimedb_lib::sats::layout::PrimitiveType;
use spacetimedb_primitives::ColId;
use spacetimedb_schema::def::{BTreeAlgorithm, IndexAlgorithm, ModuleDef, TableDef, TypeDef};
use spacetimedb_schema::identifier::Identifier;
use spacetimedb_schema::schema::{Schema, TableSchema};
use spacetimedb_schema::type_for_generate::{
    AlgebraicTypeDef, AlgebraicTypeUse, PlainEnumTypeDef, ProductTypeDef, SumTypeDef, TypespaceForGenerate,
};

const INDENT: &str = "    ";

const REDUCER_EVENTS: &str = r#"
    public interface IRemoteDbContext : IDbContext<RemoteTables, RemoteReducers, SetReducerFlags, SubscriptionBuilder> {
        public event Action<ReducerEventContext, Exception>? OnUnhandledReducerError;
    }

    public sealed class EventContext : IEventContext, IRemoteDbContext
    {
        private readonly DbConnection conn;

        /// <summary>
        /// The event that caused this callback to run.
        /// </summary>
        public readonly Event<Reducer> Event;

        /// <summary>
        /// Access to tables in the client cache, which stores a read-only replica of the remote database state.
        ///
        /// The returned <c>DbView</c> will have a method to access each table defined by the module.
        /// </summary>
        public RemoteTables Db => conn.Db;
        /// <summary>
        /// Access to reducers defined by the module.
        ///
        /// The returned <c>RemoteReducers</c> will have a method to invoke each reducer defined by the module,
        /// plus methods for adding and removing callbacks on each of those reducers.
        /// </summary>
        public RemoteReducers Reducers => conn.Reducers;
        /// <summary>
        /// Access to setters for per-reducer flags.
        ///
        /// The returned <c>SetReducerFlags</c> will have a method to invoke,
        /// for each reducer defined by the module,
        /// which call-flags for the reducer can be set.
        /// </summary>
        public SetReducerFlags SetReducerFlags => conn.SetReducerFlags;
        /// <summary>
        /// Returns <c>true</c> if the connection is active, i.e. has not yet disconnected.
        /// </summary>
        public bool IsActive => conn.IsActive;
        /// <summary>
        /// Close the connection.
        ///
        /// Throws an error if the connection is already closed.
        /// </summary>
        public void Disconnect() {
            conn.Disconnect();
        }
        /// <summary>
        /// Start building a subscription.
        /// </summary>
        /// <returns>A builder-pattern constructor for subscribing to queries,
        /// causing matching rows to be replicated into the client cache.</returns>
        public SubscriptionBuilder SubscriptionBuilder() => conn.SubscriptionBuilder();
        /// <summary>
        /// Get the <c>Identity</c> of this connection.
        ///
        /// This method returns null if the connection was constructed anonymously
        /// and we have not yet received our newly-generated <c>Identity</c> from the host.
        /// </summary>
        public Identity? Identity => conn.Identity;
        /// <summary>
        /// Get this connection's <c>ConnectionId</c>.
        /// </summary>
        public ConnectionId ConnectionId => conn.ConnectionId;
        /// <summary>
        /// Register a callback to be called when a reducer with no handler returns an error.
        /// </summary>
        public event Action<ReducerEventContext, Exception>? OnUnhandledReducerError {
            add => Reducers.InternalOnUnhandledReducerError += value;
            remove => Reducers.InternalOnUnhandledReducerError -= value;
        }

        internal EventContext(DbConnection conn, Event<Reducer> Event)
        {
            this.conn = conn;
            this.Event = Event;
        }
    }

    public sealed class ReducerEventContext : IReducerEventContext, IRemoteDbContext
    {
        private readonly DbConnection conn;
        /// <summary>
        /// The reducer event that caused this callback to run.
        /// </summary>
        public readonly ReducerEvent<Reducer> Event;

        /// <summary>
        /// Access to tables in the client cache, which stores a read-only replica of the remote database state.
        ///
        /// The returned <c>DbView</c> will have a method to access each table defined by the module.
        /// </summary>
        public RemoteTables Db => conn.Db;
        /// <summary>
        /// Access to reducers defined by the module.
        ///
        /// The returned <c>RemoteReducers</c> will have a method to invoke each reducer defined by the module,
        /// plus methods for adding and removing callbacks on each of those reducers.
        /// </summary>
        public RemoteReducers Reducers => conn.Reducers;
        /// <summary>
        /// Access to setters for per-reducer flags.
        ///
        /// The returned <c>SetReducerFlags</c> will have a method to invoke,
        /// for each reducer defined by the module,
        /// which call-flags for the reducer can be set.
        /// </summary>
        public SetReducerFlags SetReducerFlags => conn.SetReducerFlags;
        /// <summary>
        /// Returns <c>true</c> if the connection is active, i.e. has not yet disconnected.
        /// </summary>
        public bool IsActive => conn.IsActive;
        /// <summary>
        /// Close the connection.
        ///
        /// Throws an error if the connection is already closed.
        /// </summary>
        public void Disconnect() {
            conn.Disconnect();
        }
        /// <summary>
        /// Start building a subscription.
        /// </summary>
        /// <returns>A builder-pattern constructor for subscribing to queries,
        /// causing matching rows to be replicated into the client cache.</returns>
        public SubscriptionBuilder SubscriptionBuilder() => conn.SubscriptionBuilder();
        /// <summary>
        /// Get the <c>Identity</c> of this connection.
        ///
        /// This method returns null if the connection was constructed anonymously
        /// and we have not yet received our newly-generated <c>Identity</c> from the host.
        /// </summary>
        public Identity? Identity => conn.Identity;
        /// <summary>
        /// Get this connection's <c>ConnectionId</c>.
        /// </summary>
        public ConnectionId ConnectionId => conn.ConnectionId;
        /// <summary>
        /// Register a callback to be called when a reducer with no handler returns an error.
        /// </summary>
        public event Action<ReducerEventContext, Exception>? OnUnhandledReducerError {
            add => Reducers.InternalOnUnhandledReducerError += value;
            remove => Reducers.InternalOnUnhandledReducerError -= value;
        }

        internal ReducerEventContext(DbConnection conn, ReducerEvent<Reducer> reducerEvent)
        {
            this.conn = conn;
            Event = reducerEvent;
        }
    }

    public sealed class ErrorContext : IErrorContext, IRemoteDbContext
    {
        private readonly DbConnection conn;
        /// <summary>
        /// The <c>Exception</c> that caused this error callback to be run.
        /// </summary>
        public readonly Exception Event;
        Exception IErrorContext.Event {
            get {
                return Event;
            }
        }

        /// <summary>
        /// Access to tables in the client cache, which stores a read-only replica of the remote database state.
        ///
        /// The returned <c>DbView</c> will have a method to access each table defined by the module.
        /// </summary>
        public RemoteTables Db => conn.Db;
        /// <summary>
        /// Access to reducers defined by the module.
        ///
        /// The returned <c>RemoteReducers</c> will have a method to invoke each reducer defined by the module,
        /// plus methods for adding and removing callbacks on each of those reducers.
        /// </summary>
        public RemoteReducers Reducers => conn.Reducers;
        /// <summary>
        /// Access to setters for per-reducer flags.
        ///
        /// The returned <c>SetReducerFlags</c> will have a method to invoke,
        /// for each reducer defined by the module,
        /// which call-flags for the reducer can be set.
        /// </summary>
        public SetReducerFlags SetReducerFlags => conn.SetReducerFlags;
        /// <summary>
        /// Returns <c>true</c> if the connection is active, i.e. has not yet disconnected.
        /// </summary>
        public bool IsActive => conn.IsActive;
        /// <summary>
        /// Close the connection.
        ///
        /// Throws an error if the connection is already closed.
        /// </summary>
        public void Disconnect() {
            conn.Disconnect();
        }
        /// <summary>
        /// Start building a subscription.
        /// </summary>
        /// <returns>A builder-pattern constructor for subscribing to queries,
        /// causing matching rows to be replicated into the client cache.</returns>
        public SubscriptionBuilder SubscriptionBuilder() => conn.SubscriptionBuilder();
        /// <summary>
        /// Get the <c>Identity</c> of this connection.
        ///
        /// This method returns null if the connection was constructed anonymously
        /// and we have not yet received our newly-generated <c>Identity</c> from the host.
        /// </summary>
        public Identity? Identity => conn.Identity;
        /// <summary>
        /// Get this connection's <c>ConnectionId</c>.
        /// </summary>
        public ConnectionId ConnectionId => conn.ConnectionId;
        /// <summary>
        /// Register a callback to be called when a reducer with no handler returns an error.
        /// </summary>
        public event Action<ReducerEventContext, Exception>? OnUnhandledReducerError {
            add => Reducers.InternalOnUnhandledReducerError += value;
            remove => Reducers.InternalOnUnhandledReducerError -= value;
        }

        internal ErrorContext(DbConnection conn, Exception error)
        {
            this.conn = conn;
            Event = error;
        }
    }

    public sealed class SubscriptionEventContext : ISubscriptionEventContext, IRemoteDbContext
    {
        private readonly DbConnection conn;

        /// <summary>
        /// Access to tables in the client cache, which stores a read-only replica of the remote database state.
        ///
        /// The returned <c>DbView</c> will have a method to access each table defined by the module.
        /// </summary>
        public RemoteTables Db => conn.Db;
        /// <summary>
        /// Access to reducers defined by the module.
        ///
        /// The returned <c>RemoteReducers</c> will have a method to invoke each reducer defined by the module,
        /// plus methods for adding and removing callbacks on each of those reducers.
        /// </summary>
        public RemoteReducers Reducers => conn.Reducers;
        /// <summary>
        /// Access to setters for per-reducer flags.
        ///
        /// The returned <c>SetReducerFlags</c> will have a method to invoke,
        /// for each reducer defined by the module,
        /// which call-flags for the reducer can be set.
        /// </summary>
        public SetReducerFlags SetReducerFlags => conn.SetReducerFlags;
        /// <summary>
        /// Returns <c>true</c> if the connection is active, i.e. has not yet disconnected.
        /// </summary>
        public bool IsActive => conn.IsActive;
        /// <summary>
        /// Close the connection.
        ///
        /// Throws an error if the connection is already closed.
        /// </summary>
        public void Disconnect() {
            conn.Disconnect();
        }
        /// <summary>
        /// Start building a subscription.
        /// </summary>
        /// <returns>A builder-pattern constructor for subscribing to queries,
        /// causing matching rows to be replicated into the client cache.</returns>
        public SubscriptionBuilder SubscriptionBuilder() => conn.SubscriptionBuilder();
        /// <summary>
        /// Get the <c>Identity</c> of this connection.
        ///
        /// This method returns null if the connection was constructed anonymously
        /// and we have not yet received our newly-generated <c>Identity</c> from the host.
        /// </summary>
        public Identity? Identity => conn.Identity;
        /// <summary>
        /// Get this connection's <c>ConnectionId</c>.
        /// </summary>
        public ConnectionId ConnectionId => conn.ConnectionId;
        /// <summary>
        /// Register a callback to be called when a reducer with no handler returns an error.
        /// </summary>
        public event Action<ReducerEventContext, Exception>? OnUnhandledReducerError {
            add => Reducers.InternalOnUnhandledReducerError += value;
            remove => Reducers.InternalOnUnhandledReducerError -= value;
        }

        internal SubscriptionEventContext(DbConnection conn)
        {
            this.conn = conn;
        }
    }

    /// <summary>
    /// Builder-pattern constructor for subscription queries.
    /// </summary>
    public sealed class SubscriptionBuilder
    {
        private readonly IDbConnection conn;

        private event Action<SubscriptionEventContext>? Applied;
        private event Action<ErrorContext, Exception>? Error;

        /// <summary>
        /// Private API, use <c>conn.SubscriptionBuilder()</c> instead.
        /// </summary>
        public SubscriptionBuilder(IDbConnection conn)
        {
            this.conn = conn;
        }

        /// <summary>
        /// Register a callback to run when the subscription is applied.
        /// </summary>
        public SubscriptionBuilder OnApplied(
            Action<SubscriptionEventContext> callback
        )
        {
            Applied += callback;
            return this;
        }

        /// <summary>
        /// Register a callback to run when the subscription fails.
        ///
        /// Note that this callback may run either when attempting to apply the subscription,
        /// in which case <c>Self::on_applied</c> will never run,
        /// or later during the subscription's lifetime if the module's interface changes,
        /// in which case <c>Self::on_applied</c> may have already run.
        /// </summary>
        public SubscriptionBuilder OnError(
            Action<ErrorContext, Exception> callback
        )
        {
            Error += callback;
            return this;
        }

        /// <summary>
        /// Subscribe to the following SQL queries.
        ///
        /// This method returns immediately, with the data not yet added to the DbConnection.
        /// The provided callbacks will be invoked once the data is returned from the remote server.
        /// Data from all the provided queries will be returned at the same time.
        ///
        /// See the SpacetimeDB SQL docs for more information on SQL syntax:
        /// <a href="https://spacetimedb.com/docs/sql">https://spacetimedb.com/docs/sql</a>
        /// </summary>
        public SubscriptionHandle Subscribe(
            string[] querySqls
        ) => new(conn, Applied, Error, querySqls);

        /// <summary>
        /// Subscribe to all rows from all tables.
        ///
        /// This method is intended as a convenience
        /// for applications where client-side memory use and network bandwidth are not concerns.
        /// Applications where these resources are a constraint
        /// should register more precise queries via <c>Self.Subscribe</c>
        /// in order to replicate only the subset of data which the client needs to function.
        ///
        /// This method should not be combined with <c>Self.Subscribe</c> on the same <c>DbConnection</c>.
        /// A connection may either <c>Self.Subscribe</c> to particular queries,
        /// or <c>Self.SubscribeToAllTables</c>, but not both.
        /// Attempting to call <c>Self.Subscribe</c>
        /// on a <c>DbConnection</c> that has previously used <c>Self.SubscribeToAllTables</c>,
        /// or vice versa, may misbehave in any number of ways,
        /// including dropping subscriptions, corrupting the client cache, or panicking.
        /// </summary>
        public void SubscribeToAllTables()
        {
            // Make sure we use the legacy handle constructor here, even though there's only 1 query.
            // We drop the error handler, since it can't be called for legacy subscriptions.
            new SubscriptionHandle(
                conn,
                Applied,
                new string[] { "SELECT * FROM *" }
            );
        }
    }

    public sealed class SubscriptionHandle : SubscriptionHandleBase<SubscriptionEventContext, ErrorContext> {
        /// <summary>
        /// Internal API. Construct <c>SubscriptionHandle</c>s using <c>conn.SubscriptionBuilder</c>.
        /// </summary>
        public SubscriptionHandle(IDbConnection conn, Action<SubscriptionEventContext>? onApplied, string[] querySqls) : base(conn, onApplied, querySqls)
        { }

        /// <summary>
        /// Internal API. Construct <c>SubscriptionHandle</c>s using <c>conn.SubscriptionBuilder</c>.
        /// </summary>
        public SubscriptionHandle(
            IDbConnection conn,
            Action<SubscriptionEventContext>? onApplied,
            Action<ErrorContext, Exception>? onError,
            string[] querySqls
        ) : base(conn, onApplied, onError, querySqls)
        { }
    }
"#;

pub struct Csharp<'opts> {
    pub namespace: &'opts str,
}

impl Lang for Csharp<'_> {
    fn table_filename(&self, _module: &ModuleDef, table: &TableDef) -> String {
        format!("Tables/{}.g.cs", table.name.deref().to_case(Case::Pascal))
    }

    fn type_filename(&self, type_name: &spacetimedb_schema::def::ScopedTypeName) -> String {
        format!("Types/{}.g.cs", collect_case(Case::Pascal, type_name.name_segments()))
    }

    fn reducer_filename(&self, reducer_name: &Identifier) -> String {
        format!("Reducers/{}.g.cs", reducer_name.deref().to_case(Case::Pascal))
    }

    fn generate_table(&self, module: &ModuleDef, table: &TableDef) -> String {
        let mut output = CsharpAutogen::new(
            self.namespace,
            &[
                "SpacetimeDB.BSATN",
                "SpacetimeDB.ClientApi",
                "System.Collections.Generic",
                "System.Runtime.Serialization",
            ],
        );

        writeln!(output, "public sealed partial class RemoteTables");
        indented_block(&mut output, |output| {
            let schema = TableSchema::from_module_def(module, table, (), 0.into())
                .validated()
                .expect("Failed to generate table due to validation errors");
            let csharp_table_name = table.name.deref().to_case(Case::Pascal);
            let csharp_table_class_name = csharp_table_name.clone() + "Handle";
            let table_type = type_ref_name(module, table.product_type_ref);

            writeln!(
                output,
                "public sealed class {csharp_table_class_name} : RemoteTableHandle<EventContext, {table_type}>"
            );
            indented_block(output, |output| {
                writeln!(
                    output,
                    "protected override string RemoteTableName => \"{}\";",
                    table.name
                );
                writeln!(output);

                // If this is a table, we want to generate event accessor and indexes
                let product_type = module.typespace_for_generate()[table.product_type_ref]
                    .as_product()
                    .unwrap();

                let mut index_names = Vec::new();

                for idx in iter_indexes(table) {
                    let Some(accessor_name) = idx.accessor_name.as_ref() else {
                        // If there is no accessor name, we shouldn't generate a client-side index accessor.
                        continue;
                    };

                    match &idx.algorithm {
                        IndexAlgorithm::BTree(BTreeAlgorithm { columns }) => {
                            let get_csharp_field_name_and_type = |col_pos: ColId| {
                                let (field_name, field_type) = &product_type.elements[col_pos.idx()];
                                let csharp_field_name_pascal = field_name.deref().to_case(Case::Pascal);
                                let csharp_field_type = ty_fmt(module, field_type);
                                (csharp_field_name_pascal, csharp_field_type)
                            };

                            let (row_to_key, key_type) = match columns.as_singleton() {
                                Some(col_pos) => {
                                    let (field_name, field_type) = get_csharp_field_name_and_type(col_pos);
                                    (format!("row.{field_name}"), field_type.to_string())
                                }
                                None => {
                                    let mut key_accessors = Vec::new();
                                    let mut key_type_elems = Vec::new();
                                    for (field_name, field_type) in columns.iter().map(get_csharp_field_name_and_type) {
                                        key_accessors.push(format!("row.{field_name}"));
                                        key_type_elems.push(format!("{field_type} {field_name}"));
                                    }
                                    (
                                        format!("({})", key_accessors.join(", ")),
                                        format!("({})", key_type_elems.join(", ")),
                                    )
                                }
                            };

                            let csharp_index_name = accessor_name.deref().to_case(Case::Pascal);

                            let mut csharp_index_class_name = csharp_index_name.clone();
                            let csharp_index_base_class_name = if schema.is_unique(columns) {
                                csharp_index_class_name += "UniqueIndex";
                                "UniqueIndexBase"
                            } else {
                                csharp_index_class_name += "Index";
                                "BTreeIndexBase"
                            };

                            writeln!(output, "public sealed class {csharp_index_class_name} : {csharp_index_base_class_name}<{key_type}>");
                            indented_block(output, |output| {
                                writeln!(
                                    output,
                                    "protected override {key_type} GetKey({table_type} row) => {row_to_key};"
                                );
                                writeln!(output);
                                writeln!(output, "public {csharp_index_class_name}({csharp_table_class_name} table) : base(table) {{ }}");
                            });
                            writeln!(output);
                            writeln!(output, "public readonly {csharp_index_class_name} {csharp_index_name};");
                            writeln!(output);

                            index_names.push(csharp_index_name);
                        }
                        _ => todo!(),
                    }
                }

                writeln!(
                    output,
                    "internal {csharp_table_class_name}(DbConnection conn) : base(conn)"
                );
                indented_block(output, |output| {
                    for csharp_index_name in &index_names {
                        writeln!(output, "{csharp_index_name} = new(this);");
                    }
                });

                if let Some(primary_col_index) = schema.pk() {
                    writeln!(output);
                    writeln!(
                        output,
                        "protected override object GetPrimaryKey({table_type} row) => row.{col_name_pascal_case};",
                        col_name_pascal_case = primary_col_index.col_name.deref().to_case(Case::Pascal)
                    );
                }
            });
            writeln!(output);
            writeln!(output, "public readonly {csharp_table_class_name} {csharp_table_name};");
        });

        output.into_inner()
    }

    fn generate_type(&self, module: &ModuleDef, typ: &TypeDef) -> String {
        let name = collect_case(Case::Pascal, typ.name.name_segments());
        match &module.typespace_for_generate()[typ.ty] {
            AlgebraicTypeDef::Sum(sum) => autogen_csharp_sum(module, name, sum, self.namespace),
            AlgebraicTypeDef::Product(prod) => autogen_csharp_tuple(module, name, prod, self.namespace),
            AlgebraicTypeDef::PlainEnum(plain_enum) => autogen_csharp_plain_enum(name, plain_enum, self.namespace),
        }
    }

    fn generate_reducer(&self, module: &ModuleDef, reducer: &spacetimedb_schema::def::ReducerDef) -> String {
        let mut output = CsharpAutogen::new(
            self.namespace,
            &[
                "SpacetimeDB.ClientApi",
                "System.Collections.Generic",
                "System.Runtime.Serialization",
            ],
        );

        writeln!(output, "public sealed partial class RemoteReducers : RemoteBase");
        indented_block(&mut output, |output| {
            let func_name_pascal_case = reducer.name.deref().to_case(Case::Pascal);
            let delegate_separator = if reducer.params_for_generate.elements.is_empty() {
                ""
            } else {
                ", "
            };

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

            for (arg_i, (arg_name, arg_ty)) in reducer.params_for_generate.into_iter().enumerate() {
                if arg_i != 0 {
                    func_params.push_str(", ");
                    func_args.push_str(", ");
                }

                let arg_type_str = ty_fmt(module, arg_ty);
                let arg_name = arg_name.deref().to_case(Case::Camel);

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

            writeln!(
                output,
                "public delegate void {func_name_pascal_case}Handler(ReducerEventContext ctx{delegate_separator}{func_params});"
            );
            writeln!(
                output,
                "public event {func_name_pascal_case}Handler? On{func_name_pascal_case};"
            );
            writeln!(output);

            if is_reducer_invokable(reducer) {
                writeln!(output, "public void {func_name_pascal_case}({func_params})");
                indented_block(output, |output| {
                    writeln!(
                        output,
                        "conn.InternalCallReducer(new Reducer.{func_name_pascal_case}({func_args}), this.SetCallReducerFlags.{func_name_pascal_case}Flags);"
                    );
                });
                writeln!(output);
            }

            writeln!(
                output,
                "public bool Invoke{func_name_pascal_case}(ReducerEventContext ctx, Reducer.{func_name_pascal_case} args)"
            );
            indented_block(output, |output| {
                writeln!(output, "if (On{func_name_pascal_case} == null)");
                indented_block(output, |output| {
                    writeln!(output, "if (InternalOnUnhandledReducerError != null)");
                    indented_block(output, |output| {
                        writeln!(output, "switch(ctx.Event.Status)");
                        indented_block(output, |output| {
                            writeln!(output, "case Status.Failed(var reason): InternalOnUnhandledReducerError(ctx, new Exception(reason)); break;");
                            writeln!(output, "case Status.OutOfEnergy(var _): InternalOnUnhandledReducerError(ctx, new Exception(\"out of energy\")); break;");
                        });
                    });
                    writeln!(output, "return false;");
                });

                writeln!(output, "On{func_name_pascal_case}(");
                // Write out arguments one per line
                {
                    indent_scope!(output);
                    write!(output, "ctx");
                    for (arg_name, _) in &reducer.params_for_generate {
                        writeln!(output, ",");
                        let arg_name = arg_name.deref().to_case(Case::Pascal);
                        write!(output, "args.{arg_name}");
                    }
                    writeln!(output);
                }
                writeln!(output, ");");
                writeln!(output, "return true;");
            });
        });

        writeln!(output);

        writeln!(output, "public abstract partial class Reducer");
        indented_block(&mut output, |output| {
            autogen_csharp_product_common(
                module,
                output,
                reducer.name.deref().to_case(Case::Pascal),
                &reducer.params_for_generate,
                "Reducer, IReducerArgs",
                |output| {
                    if !reducer.params_for_generate.elements.is_empty() {
                        writeln!(output);
                    }
                    writeln!(output, "string IReducerArgs.ReducerName => \"{}\";", reducer.name);
                },
            );
        });

        if is_reducer_invokable(reducer) {
            writeln!(output);
            writeln!(output, "public sealed partial class SetReducerFlags");
            indented_block(&mut output, |output| {
                let func_name_pascal_case = reducer.name.deref().to_case(Case::Pascal);
                writeln!(output, "internal CallReducerFlags {func_name_pascal_case}Flags;");
                writeln!(output, "public void {func_name_pascal_case}(CallReducerFlags flags) => {func_name_pascal_case}Flags = flags;");
            });
        }

        output.into_inner()
    }

    fn generate_globals(&self, module: &ModuleDef) -> Vec<(String, String)> {
        let mut output = CsharpAutogen::new(
            self.namespace,
            &[
                "SpacetimeDB.ClientApi",
                "System.Collections.Generic",
                "System.Runtime.Serialization",
            ],
        );

        writeln!(output, "public sealed partial class RemoteReducers : RemoteBase");
        indented_block(&mut output, |output| {
            writeln!(
                output,
                "internal RemoteReducers(DbConnection conn, SetReducerFlags flags) : base(conn) => SetCallReducerFlags = flags;"
            );
            writeln!(output, "internal readonly SetReducerFlags SetCallReducerFlags;");
            writeln!(
                output,
                "internal event Action<ReducerEventContext, Exception>? InternalOnUnhandledReducerError;"
            )
        });
        writeln!(output);

        writeln!(output, "public sealed partial class RemoteTables : RemoteTablesBase");
        indented_block(&mut output, |output| {
            writeln!(output, "public RemoteTables(DbConnection conn)");
            indented_block(output, |output| {
                for table in iter_tables(module) {
                    writeln!(
                        output,
                        "AddTable({} = new(conn));",
                        table.name.deref().to_case(Case::Pascal)
                    );
                }
            });
        });
        writeln!(output);

        writeln!(output, "public sealed partial class SetReducerFlags {{ }}");

        writeln!(output, "{REDUCER_EVENTS}");

        writeln!(output, "public abstract partial class Reducer");
        indented_block(&mut output, |output| {
            // Prevent instantiation of this class from outside.
            writeln!(output, "private Reducer() {{ }}");
        });
        writeln!(output);

        writeln!(
            output,
            "public sealed class DbConnection : DbConnectionBase<DbConnection, RemoteTables, Reducer>"
        );
        indented_block(&mut output, |output: &mut CodeIndenter<String>| {
            writeln!(output, "public override RemoteTables Db {{ get; }}");
            writeln!(output, "public readonly RemoteReducers Reducers;");
            writeln!(output, "public readonly SetReducerFlags SetReducerFlags = new();");
            writeln!(output);

            writeln!(output, "public DbConnection()");
            indented_block(output, |output| {
                writeln!(output, "Db = new(this);");
                writeln!(output, "Reducers = new(this, SetReducerFlags);");
            });
            writeln!(output);

            writeln!(output, "protected override Reducer ToReducer(TransactionUpdate update)");
            indented_block(output, |output| {
                writeln!(output, "var encodedArgs = update.ReducerCall.Args;");
                writeln!(output, "return update.ReducerCall.ReducerName switch {{");
                {
                    indent_scope!(output);
                    for reducer in iter_reducers(module) {
                        let reducer_str_name = &reducer.name;
                        let reducer_name = reducer.name.deref().to_case(Case::Pascal);
                        writeln!(
                            output,
                            "\"{reducer_str_name}\" => BSATNHelpers.Decode<Reducer.{reducer_name}>(encodedArgs),"
                        );
                    }
                    writeln!(
                        output,
                        r#"var reducer => throw new ArgumentOutOfRangeException("Reducer", $"Unknown reducer {{reducer}}")"#
                    );
                }
                writeln!(output, "}};");
            });
            writeln!(output);

            writeln!(
                output,
                "protected override IEventContext ToEventContext(Event<Reducer> Event) =>"
            );
            writeln!(output, "new EventContext(this, Event);");
            writeln!(output);

            writeln!(
                output,
                "protected override IReducerEventContext ToReducerEventContext(ReducerEvent<Reducer> reducerEvent) =>"
            );
            writeln!(output, "new ReducerEventContext(this, reducerEvent);");
            writeln!(output);

            writeln!(
                output,
                "protected override ISubscriptionEventContext MakeSubscriptionEventContext() =>"
            );
            writeln!(output, "new SubscriptionEventContext(this);");
            writeln!(output);

            writeln!(
                output,
                "protected override IErrorContext ToErrorContext(Exception exception) =>"
            );
            writeln!(output, "new ErrorContext(this, exception);");
            writeln!(output);

            writeln!(
                output,
                "protected override bool Dispatch(IReducerEventContext context, Reducer reducer)"
            );
            indented_block(output, |output| {
                writeln!(output, "var eventContext = (ReducerEventContext)context;");
                writeln!(output, "return reducer switch {{");
                {
                    indent_scope!(output);
                    for reducer_name in iter_reducers(module).map(|r| r.name.deref().to_case(Case::Pascal)) {
                        writeln!(
                            output,
                            "Reducer.{reducer_name} args => Reducers.Invoke{reducer_name}(eventContext, args),"
                        );
                    }
                    writeln!(
                        output,
                        r#"_ => throw new ArgumentOutOfRangeException("Reducer", $"Unknown reducer {{reducer}}")"#
                    );
                }
                writeln!(output, "}};");
            });
            writeln!(output);

            writeln!(output, "public SubscriptionBuilder SubscriptionBuilder() => new(this);");
            writeln!(
                output,
                "public event Action<ReducerEventContext, Exception> OnUnhandledReducerError"
            );
            indented_block(output, |output| {
                writeln!(output, "add => Reducers.InternalOnUnhandledReducerError += value;");
                writeln!(output, "remove => Reducers.InternalOnUnhandledReducerError -= value;");
            });
        });

        vec![("SpacetimeDBClient.g.cs".to_owned(), output.into_inner())]
    }
}

fn ty_fmt<'a>(module: &'a ModuleDef, ty: &'a AlgebraicTypeUse) -> impl fmt::Display + 'a {
    fmt_fn(move |f| match ty {
        AlgebraicTypeUse::Identity => f.write_str("SpacetimeDB.Identity"),
        AlgebraicTypeUse::ConnectionId => f.write_str("SpacetimeDB.ConnectionId"),
        AlgebraicTypeUse::ScheduleAt => f.write_str("SpacetimeDB.ScheduleAt"),
        AlgebraicTypeUse::Timestamp => f.write_str("SpacetimeDB.Timestamp"),
        AlgebraicTypeUse::TimeDuration => f.write_str("SpacetimeDB.TimeDuration"),
        AlgebraicTypeUse::Unit => f.write_str("SpacetimeDB.Unit"),
        AlgebraicTypeUse::Option(inner_ty) => write!(f, "{}?", ty_fmt(module, inner_ty)),
        AlgebraicTypeUse::Array(elem_ty) => write!(f, "System.Collections.Generic.List<{}>", ty_fmt(module, elem_ty)),
        AlgebraicTypeUse::String => f.write_str("string"),
        AlgebraicTypeUse::Ref(r) => f.write_str(&type_ref_name(module, *r)),
        AlgebraicTypeUse::Primitive(prim) => f.write_str(match prim {
            PrimitiveType::Bool => "bool",
            PrimitiveType::I8 => "sbyte",
            PrimitiveType::U8 => "byte",
            PrimitiveType::I16 => "short",
            PrimitiveType::U16 => "ushort",
            PrimitiveType::I32 => "int",
            PrimitiveType::U32 => "uint",
            PrimitiveType::I64 => "long",
            PrimitiveType::U64 => "ulong",
            PrimitiveType::I128 => "I128",
            PrimitiveType::U128 => "U128",
            PrimitiveType::I256 => "I256",
            PrimitiveType::U256 => "U256",
            PrimitiveType::F32 => "float",
            PrimitiveType::F64 => "double",
        }),
        AlgebraicTypeUse::Never => unimplemented!(),
    })
}

fn default_init(ctx: &TypespaceForGenerate, ty: &AlgebraicTypeUse) -> Option<&'static str> {
    match ty {
        // Options (`T?`) have a default value of null which is fine for us.
        AlgebraicTypeUse::Option(_) => None,
        AlgebraicTypeUse::Ref(r) => match &ctx[*r] {
            // TODO: generate some proper default here (what would it be for tagged enums?).
            AlgebraicTypeDef::Sum(_) => Some("null!"),
            // Simple enums have their own default (variant with value of zero).
            AlgebraicTypeDef::PlainEnum(_) => None,
            AlgebraicTypeDef::Product(_) => Some("new()"),
        },
        // See Sum(_) handling above.
        AlgebraicTypeUse::ScheduleAt => Some("null!"),
        AlgebraicTypeUse::Array(_) => Some("new()"),
        // Strings must have explicit default value of "".
        AlgebraicTypeUse::String => Some(r#""""#),
        // Primitives are initialized to zero automatically.
        AlgebraicTypeUse::Primitive(_) => None,
        // these are structs, they are initialized to zero-filled automatically
        AlgebraicTypeUse::Unit
        | AlgebraicTypeUse::Identity
        | AlgebraicTypeUse::ConnectionId
        | AlgebraicTypeUse::Timestamp
        | AlgebraicTypeUse::TimeDuration => None,
        AlgebraicTypeUse::Never => unimplemented!("never types are not yet supported in C# output"),
    }
}

struct CsharpAutogen {
    output: CodeIndenter<String>,
}

impl Deref for CsharpAutogen {
    type Target = CodeIndenter<String>;

    fn deref(&self) -> &Self::Target {
        &self.output
    }
}

impl std::ops::DerefMut for CsharpAutogen {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.output
    }
}

impl CsharpAutogen {
    pub fn new(namespace: &str, extra_usings: &[&str]) -> Self {
        let mut output = CodeIndenter::new(String::new(), INDENT);

        print_auto_generated_file_comment(&mut output);

        writeln!(output, "#nullable enable");
        writeln!(output);

        writeln!(output, "using System;");
        // Don't emit `using SpacetimeDB;` if we are going to be nested in the SpacetimeDB namespace.
        if namespace
            .split('.')
            .next()
            .expect("split always returns at least one string")
            != "SpacetimeDB"
        {
            writeln!(output, "using SpacetimeDB;");
        }
        for extra_using in extra_usings {
            writeln!(output, "using {extra_using};");
        }
        writeln!(output);

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

        Self { output }
    }

    pub fn into_inner(mut self) -> String {
        self.dedent(1);
        writeln!(self, "}}");

        self.output.into_inner()
    }
}

fn autogen_csharp_sum(module: &ModuleDef, sum_type_name: String, sum_type: &SumTypeDef, namespace: &str) -> String {
    let mut output = CsharpAutogen::new(namespace, &[]);

    writeln!(output, "[SpacetimeDB.Type]");
    write!(
        output,
        "public partial record {sum_type_name} : SpacetimeDB.TaggedEnum<("
    );
    {
        indent_scope!(output);
        for (i, (variant_name, variant_ty)) in sum_type.variants.iter().enumerate() {
            if i != 0 {
                write!(output, ",");
            }
            writeln!(output);
            write!(output, "{} {variant_name}", ty_fmt(module, variant_ty));
        }
        // If we have fewer than 2 variants, we need to add some dummy variants to make the tuple work.
        match sum_type.variants.len() {
            0 => {
                writeln!(output);
                writeln!(output, "SpacetimeDB.Unit _Reserved1,");
                write!(output, "SpacetimeDB.Unit _Reserved2");
            }
            1 => {
                writeln!(output, ",");
                write!(output, "SpacetimeDB.Unit _Reserved");
            }
            _ => {}
        }
    }
    writeln!(output);
    writeln!(output, ")>;");

    output.into_inner()
}

fn autogen_csharp_plain_enum(enum_type_name: String, enum_type: &PlainEnumTypeDef, namespace: &str) -> String {
    let mut output = CsharpAutogen::new(namespace, &[]);

    writeln!(output, "[SpacetimeDB.Type]");
    writeln!(output, "public enum {enum_type_name}");
    indented_block(&mut output, |output| {
        for variant in &*enum_type.variants {
            writeln!(output, "{variant},");
        }
    });

    output.into_inner()
}

fn autogen_csharp_tuple(module: &ModuleDef, name: String, tuple: &ProductTypeDef, namespace: &str) -> String {
    let mut output = CsharpAutogen::new(
        namespace,
        &["System.Collections.Generic", "System.Runtime.Serialization"],
    );

    autogen_csharp_product_common(module, &mut output, name, tuple, "", |_| {});

    output.into_inner()
}

fn autogen_csharp_product_common(
    module: &ModuleDef,
    output: &mut CodeIndenter<String>,
    name: String,
    product_type: &ProductTypeDef,
    base: &str,
    extra_body: impl FnOnce(&mut CodeIndenter<String>),
) {
    writeln!(output, "[SpacetimeDB.Type]");
    writeln!(output, "[DataContract]");
    write!(output, "public sealed partial class {name}");
    if !base.is_empty() {
        write!(output, " : {base}");
    }
    writeln!(output);
    indented_block(output, |output| {
        let fields = product_type
            .into_iter()
            .map(|(orig_name, ty)| {
                writeln!(output, "[DataMember(Name = \"{orig_name}\")]");

                let field_name = orig_name.deref().to_case(Case::Pascal);
                let ty = ty_fmt(module, ty).to_string();

                writeln!(output, "public {ty} {field_name};");

                (field_name, ty)
            })
            .collect::<Vec<_>>();

        // If we don't have any fields, the default constructor is fine, otherwise we need to generate our own.
        if !fields.is_empty() {
            writeln!(output);

            // Generate fully-parameterized constructor.
            write!(output, "public {name}(");
            if fields.len() > 1 {
                writeln!(output);
            }
            {
                indent_scope!(output);
                for (i, (field_name, ty)) in fields.iter().enumerate() {
                    if i != 0 {
                        writeln!(output, ",");
                    }
                    write!(output, "{ty} {field_name}");
                }
            }
            if fields.len() > 1 {
                writeln!(output);
            }
            writeln!(output, ")");
            indented_block(output, |output| {
                for (field_name, _ty) in fields.iter() {
                    writeln!(output, "this.{field_name} = {field_name};");
                }
            });
            writeln!(output);

            // Generate default constructor.
            writeln!(output, "public {name}()");
            indented_block(output, |output| {
                for ((field_name, _ty), (_field, field_ty)) in fields.iter().zip(product_type) {
                    if let Some(default) = default_init(module.typespace_for_generate(), field_ty) {
                        writeln!(output, "this.{field_name} = {default};");
                    }
                }
            });
        }

        extra_body(output);
    });
}

fn indented_block<R>(output: &mut CodeIndenter<String>, f: impl FnOnce(&mut CodeIndenter<String>) -> R) -> R {
    writeln!(output, "{{");
    let res = f(&mut output.indented(1));
    writeln!(output, "}}");
    res
}