Skip to content

Commit 4e53b8a

Browse files
committed
Add more types to filterable table
1 parent d8a5d93 commit 4e53b8a

File tree

6 files changed

+116
-7
lines changed

6 files changed

+116
-7
lines changed

crates/codegen/tests/snapshots/codegen__codegen_csharp.snap

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1570,6 +1570,42 @@ namespace SpacetimeDB
15701570

15711571
public readonly BlobIndex Blob;
15721572

1573+
public sealed class ConnectionIdIndex : BTreeIndexBase<SpacetimeDB.ConnectionId>
1574+
{
1575+
protected override SpacetimeDB.ConnectionId GetKey(Filterable row) => row.ConnectionId;
1576+
1577+
public ConnectionIdIndex(FilterableHandle table) : base(table) { }
1578+
}
1579+
1580+
public readonly ConnectionIdIndex ConnectionId;
1581+
1582+
public sealed class IdentityIndex : BTreeIndexBase<SpacetimeDB.Identity>
1583+
{
1584+
protected override SpacetimeDB.Identity GetKey(Filterable row) => row.Identity;
1585+
1586+
public IdentityIndex(FilterableHandle table) : base(table) { }
1587+
}
1588+
1589+
public readonly IdentityIndex Identity;
1590+
1591+
public sealed class StrIndex : BTreeIndexBase<string>
1592+
{
1593+
protected override string GetKey(Filterable row) => row.Str;
1594+
1595+
public StrIndex(FilterableHandle table) : base(table) { }
1596+
}
1597+
1598+
public readonly StrIndex Str;
1599+
1600+
public sealed class TestCIndex : BTreeIndexBase<NamespaceTestC>
1601+
{
1602+
protected override NamespaceTestC GetKey(Filterable row) => row.TestC;
1603+
1604+
public TestCIndex(FilterableHandle table) : base(table) { }
1605+
}
1606+
1607+
public readonly TestCIndex TestC;
1608+
15731609
public sealed class TimeDurationIndex : BTreeIndexBase<SpacetimeDB.TimeDuration>
15741610
{
15751611
protected override SpacetimeDB.TimeDuration GetKey(Filterable row) => row.TimeDuration;
@@ -1591,6 +1627,10 @@ namespace SpacetimeDB
15911627
internal FilterableHandle(DbConnection conn) : base(conn)
15921628
{
15931629
Blob = new(this);
1630+
ConnectionId = new(this);
1631+
Identity = new(this);
1632+
Str = new(this);
1633+
TestC = new(this);
15941634
TimeDuration = new(this);
15951635
Timestamp = new(this);
15961636
}
@@ -2185,6 +2225,14 @@ namespace SpacetimeDB
21852225
[DataContract]
21862226
public sealed partial class Filterable
21872227
{
2228+
[DataMember(Name = "str")]
2229+
public string Str;
2230+
[DataMember(Name = "identity")]
2231+
public SpacetimeDB.Identity Identity;
2232+
[DataMember(Name = "connection_id")]
2233+
public SpacetimeDB.ConnectionId ConnectionId;
2234+
[DataMember(Name = "test_c")]
2235+
public NamespaceTestC TestC;
21882236
[DataMember(Name = "timestamp")]
21892237
public SpacetimeDB.Timestamp Timestamp;
21902238
[DataMember(Name = "time_duration")]
@@ -2193,18 +2241,27 @@ namespace SpacetimeDB
21932241
public System.Collections.Generic.List<byte> Blob;
21942242

21952243
public Filterable(
2244+
string Str,
2245+
SpacetimeDB.Identity Identity,
2246+
SpacetimeDB.ConnectionId ConnectionId,
2247+
NamespaceTestC TestC,
21962248
SpacetimeDB.Timestamp Timestamp,
21972249
SpacetimeDB.TimeDuration TimeDuration,
21982250
System.Collections.Generic.List<byte> Blob
21992251
)
22002252
{
2253+
this.Str = Str;
2254+
this.Identity = Identity;
2255+
this.ConnectionId = ConnectionId;
2256+
this.TestC = TestC;
22012257
this.Timestamp = Timestamp;
22022258
this.TimeDuration = TimeDuration;
22032259
this.Blob = Blob;
22042260
}
22052261

22062262
public Filterable()
22072263
{
2264+
this.Str = "";
22082265
this.Blob = new();
22092266
}
22102267
}

crates/codegen/tests/snapshots/codegen__codegen_rust.snap

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,7 @@ use spacetimedb_sdk::__codegen::{
814814
__ws,
815815
};
816816
use super::filterable_type::Filterable;
817+
use super::namespace_test_c_type::NamespaceTestC;
817818

818819
/// Table handle for the table `filterable`.
819820
///
@@ -916,10 +917,15 @@ use spacetimedb_sdk::__codegen::{
916917
__ws,
917918
};
918919

920+
use super::namespace_test_c_type::NamespaceTestC;
919921

920922
#[derive(__lib::ser::Serialize, __lib::de::Deserialize, Clone, PartialEq, Debug)]
921923
#[sats(crate = __lib)]
922924
pub struct Filterable {
925+
pub str: String,
926+
pub identity: __sdk::Identity,
927+
pub connection_id: __sdk::ConnectionId,
928+
pub test_c: NamespaceTestC,
923929
pub timestamp: __sdk::Timestamp,
924930
pub time_duration: __sdk::TimeDuration,
925931
pub blob: Vec::<u8>,
@@ -4615,9 +4621,9 @@ use spacetimedb_sdk::__codegen::{
46154621
__ws,
46164622
};
46174623

4624+
use super::namespace_test_c_type::NamespaceTestC;
46184625
use super::test_a_type::TestA;
46194626
use super::test_b_type::TestB;
4620-
use super::namespace_test_c_type::NamespaceTestC;
46214627
use super::namespace_test_f_type::NamespaceTestF;
46224628

46234629
#[derive(__lib::ser::Serialize, __lib::de::Deserialize, Clone, PartialEq, Debug)]

crates/codegen/tests/snapshots/codegen__codegen_typescript.snap

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,8 @@ import {
554554
type SubscriptionEventContextInterface,
555555
} from "@clockworklabs/spacetimedb-sdk";
556556
import { Filterable } from "./filterable_type";
557+
import { NamespaceTestC as __NamespaceTestC } from "./namespace_test_c_type";
558+
557559
import { type EventContext, type Reducer, RemoteReducers, RemoteTables } from ".";
558560

559561
/**
@@ -633,7 +635,13 @@ import {
633635
type ReducerEventContextInterface,
634636
type SubscriptionEventContextInterface,
635637
} from "@clockworklabs/spacetimedb-sdk";
638+
import { NamespaceTestC as __NamespaceTestC } from "./namespace_test_c_type";
639+
636640
export type Filterable = {
641+
str: string,
642+
identity: Identity,
643+
connectionId: ConnectionId,
644+
testC: __NamespaceTestC,
637645
timestamp: Timestamp,
638646
timeDuration: TimeDuration,
639647
blob: Uint8Array,
@@ -649,6 +657,10 @@ export namespace Filterable {
649657
*/
650658
export function getTypeScriptAlgebraicType(): AlgebraicType {
651659
return AlgebraicType.createProductType([
660+
new ProductTypeElement("str", AlgebraicType.createStringType()),
661+
new ProductTypeElement("identity", AlgebraicType.createIdentityType()),
662+
new ProductTypeElement("connectionId", AlgebraicType.createConnectionIdType()),
663+
new ProductTypeElement("testC", __NamespaceTestC.getTypeScriptAlgebraicType()),
652664
new ProductTypeElement("timestamp", AlgebraicType.createTimestampType()),
653665
new ProductTypeElement("timeDuration", AlgebraicType.createTimeDurationType()),
654666
new ProductTypeElement("blob", AlgebraicType.createArrayType(AlgebraicType.createU8Type())),
@@ -4037,9 +4049,9 @@ import {
40374049
type SubscriptionEventContextInterface,
40384050
} from "@clockworklabs/spacetimedb-sdk";
40394051

4052+
import { NamespaceTestC as __NamespaceTestC } from "./namespace_test_c_type";
40404053
import { TestA as __TestA } from "./test_a_type";
40414054
import { TestB as __TestB } from "./test_b_type";
4042-
import { NamespaceTestC as __NamespaceTestC } from "./namespace_test_c_type";
40434055
import { NamespaceTestF as __NamespaceTestF } from "./namespace_test_f_type";
40444056

40454057
export type Test = {

crates/lib/src/filterable_value.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ use spacetimedb_sats::{hash::Hash, i256, u256, Serialize};
1919
/// - [`Identity`].
2020
/// - [`ConnectionId`].
2121
/// - [`Hash`](struct@Hash).
22+
/// - [`Timestamp`](struct@Timestamp).
23+
/// - [`TimeDuration`](struct@TimeDuration).
2224
/// - No-payload enums annotated with `#[derive(SpacetimeType)]`.
2325
/// No-payload enums are sometimes called "plain," "simple" or "C-style."
2426
/// They are enums where no variant has any payload data.

modules/module-test-cs/Lib.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,15 @@ public partial struct Player
175175
[Table(Name = "filterable")]
176176
public partial struct Filterable
177177
{
178+
// Elided the trivial types as `int's, bool`...
179+
[Index.BTree]
180+
public String str;
181+
[Index.BTree]
182+
public Identity identity;
183+
[Index.BTree]
184+
public ConnectionId connection_id;
185+
[Index.BTree]
186+
public TestC test_c;
178187
[Index.BTree]
179188
public Timestamp timestamp;
180189
[Index.BTree]
@@ -429,10 +438,13 @@ public static void test_btree_index_args(ReducerContext ctx)
429438
ctx.Db.test_e.name.Delete("str");
430439

431440
// Single-column indexes on `Filterable` table:
432-
var _a1 = ctx.Db.filterable.timestamp.Filter(new Timestamp(0));
433-
var _a2 = ctx.Db.filterable.time_duration.Filter(new TimeDuration(0));
434-
var _a3 = ctx.Db.filterable.blob.Filter(new byte[] { 0x01, 0x02, 0x03 });
435-
441+
var _a1 = ctx.Db.filterable.str.Filter("string");
442+
var _a2 = ctx.Db.filterable.identity.Filter(Identity.FromHexString("0x0"));
443+
var _a3 = ctx.Db.filterable.test_c.Filter(TestC.Foo);
444+
var _a4 = ctx.Db.filterable.connection_id.Filter(ConnectionId.Random());
445+
var _a5 = ctx.Db.filterable.timestamp.Filter(new Timestamp(0));
446+
var _a6 = ctx.Db.filterable.time_duration.Filter(new TimeDuration(0));
447+
var _a7 = ctx.Db.filterable.blob.Filter([0x01, 0x02, 0x03]);
436448
// For the multi‑column index on points, assume the API offers overloads that accept ranges.
437449
var mci = ctx.Db.points.multi_column_index;
438450
var _a = mci.Filter(0L);

modules/module-test/src/lib.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,23 @@ pub struct Player {
161161
name: String,
162162
}
163163

164-
/// Extra table for checking implementation of `FilterableValue` trait
164+
/// Extra table for checking implementation of `FilterableValue`.
165+
///
166+
/// See [`FilterableValue`](spacetimedb::spacetimedb_lib::FilterableValue) for more details.
165167
#[spacetimedb::table(name = filterable)]
166168
pub struct Filterable {
169+
// Elided the trivial types as `int's, bool`...
170+
#[index(btree)]
171+
str: String,
172+
#[index(btree)]
173+
identity: Identity,
174+
#[index(btree)]
175+
connection_id: ConnectionId,
176+
// Not available in C# yet, and we need to mirror the table definition.
177+
// #[index(btree)]
178+
// hash: Hash,
179+
#[index(btree)]
180+
test_c: TestC,
167181
#[index(btree)]
168182
timestamp: Timestamp,
169183
#[index(btree)]
@@ -380,6 +394,12 @@ fn test_btree_index_args(ctx: &ReducerContext) {
380394
// ctx.db.test_e().name().delete(string); // SHOULD FAIL
381395

382396
// Single-column indexes on `Filterable` table:
397+
let _ = ctx.db.filterable().str().filter("string");
398+
let _ = ctx.db.filterable().identity().filter(Identity::ZERO);
399+
let _ = ctx.db.filterable().identity().filter(&Identity::ZERO);
400+
let _ = ctx.db.filterable().connection_id().filter(ConnectionId::ZERO);
401+
let _ = ctx.db.filterable().connection_id().filter(&ConnectionId::ZERO);
402+
let _ = ctx.db.filterable().test_c().filter(TestC::Foo);
383403
let _ = ctx
384404
.db
385405
.filterable()

0 commit comments

Comments
 (0)