Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 23 additions & 14 deletions crates/bindings-cpp/include/spacetimedb/table_with_constraints.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,21 +210,30 @@ struct MultiColumnIndexTag {
// Constraint Concepts
// =============================================================================

namespace detail {
template<typename T>
struct filterable_value_impl
: std::bool_constant<
std::integral<T> ||
std::same_as<T, std::string> ||
std::same_as<T, SpacetimeDB::Identity> ||
std::same_as<T, SpacetimeDB::ConnectionId> ||
std::same_as<T, SpacetimeDB::Timestamp> ||
std::same_as<T, SpacetimeDB::Uuid> ||
std::same_as<T, SpacetimeDB::I128> ||
std::same_as<T, SpacetimeDB::U128> ||
std::same_as<T, SpacetimeDB::I256> ||
std::same_as<T, SpacetimeDB::U256> ||
std::same_as<T, SpacetimeDB::i256> ||
std::same_as<T, SpacetimeDB::u256> ||
std::is_enum_v<T>> {};

template<typename T>
struct filterable_value_impl<std::optional<T>> : filterable_value_impl<std::remove_cvref_t<T>> {};
}

template<typename T>
concept FilterableValue =
std::integral<T> ||
std::same_as<T, std::string> ||
std::same_as<T, SpacetimeDB::Identity> ||
std::same_as<T, SpacetimeDB::ConnectionId> ||
std::same_as<T, SpacetimeDB::Timestamp> ||
std::same_as<T, SpacetimeDB::Uuid> ||
std::same_as<T, SpacetimeDB::I128> ||
std::same_as<T, SpacetimeDB::U128> ||
std::same_as<T, SpacetimeDB::I256> ||
std::same_as<T, SpacetimeDB::U256> ||
std::same_as<T, SpacetimeDB::i256> ||
std::same_as<T, SpacetimeDB::u256> ||
std::is_enum_v<T>;
concept FilterableValue = detail::filterable_value_impl<std::remove_cvref_t<T>>::value;

template<typename T>
concept AutoIncrementable =
Expand Down
27 changes: 27 additions & 0 deletions crates/bindings-csharp/Codegen.Tests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,33 @@ public static void TestNullableBTreeIndex(ReducerContext ctx)
_ = ctx.Db.NullableBTreeIndex.AccountId.Filter(new Bound<uint?>(null, 99));
}
}

[SpacetimeDB.Table]
public partial struct NullableUniqueIndex
{
[SpacetimeDB.PrimaryKey]
public uint Id;

[SpacetimeDB.Unique]
public uint? AccountId;

[SpacetimeDB.Unique]
public string? Name;

[SpacetimeDB.Unique]
public SpacetimeDB.Uuid? ExternalId;

[SpacetimeDB.Reducer]
public static void TestNullableUniqueIndex(ReducerContext ctx)
{
_ = ctx.Db.NullableUniqueIndex.AccountId.Find((uint?)null);
_ = ctx.Db.NullableUniqueIndex.AccountId.Find((uint?)55);
_ = ctx.Db.NullableUniqueIndex.Name.Find((string?)null);
_ = ctx.Db.NullableUniqueIndex.Name.Find("name");
_ = ctx.Db.NullableUniqueIndex.ExternalId.Find((SpacetimeDB.Uuid?)null);
_ = ctx.Db.NullableUniqueIndex.ExternalId.Find(SpacetimeDB.Uuid.NIL);
}
}
""";

var parseOptions = new CSharpParseOptions(fixture.SampleCompilation.LanguageVersion);
Expand Down
24 changes: 18 additions & 6 deletions crates/bindings-csharp/Codegen/Module.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,22 @@ public static bool IsNoPayloadEnum(ITypeSymbol type)
&& variants.All(field => field.Type.ToString() == "SpacetimeDB.Unit");
}

public static bool IsEquatable(ITypeSymbol type) =>
(
IsInteger(type)
private static ITypeSymbol UnwrapNullable(ITypeSymbol type) =>
type switch
{
INamedTypeSymbol
{
OriginalDefinition.SpecialType: SpecialType.System_Nullable_T
} nullable => nullable.TypeArguments[0],
_ when type.NullableAnnotation == NullableAnnotation.Annotated =>
type.WithNullableAnnotation(NullableAnnotation.None),
_ => type,
};

public static bool IsEquatable(ITypeSymbol type)
{
type = UnwrapNullable(type);
return IsInteger(type)
|| IsNoPayloadEnum(type)
|| type.SpecialType switch
{
Expand All @@ -173,9 +186,8 @@ public static bool IsEquatable(ITypeSymbol type) =>
or "SpacetimeDB.Timestamp"
or "SpacetimeDB.Uuid",
_ => false,
}
)
&& type.NullableAnnotation != NullableAnnotation.Annotated;
};
}
}

/// <summary>
Expand Down
19 changes: 15 additions & 4 deletions crates/codegen/src/csharp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -561,10 +561,12 @@ impl Lang for Csharp<'_> {
(csharp_field_name_pascal, csharp_field_type)
};

let (row_to_key, key_type) = match columns.as_singleton() {
let (row_to_key, key_type, nullable_single_column) = 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())
let field_type = field_type.to_string();
let nullable_single_column = field_type.ends_with('?');
Comment thread
JasonAtClockwork marked this conversation as resolved.
Outdated
(format!("row.{field_name}"), field_type, nullable_single_column)
}
None => {
let mut key_accessors = Vec::new();
Expand All @@ -576,6 +578,7 @@ impl Lang for Csharp<'_> {
(
format!("({})", key_accessors.join(", ")),
format!("({})", key_type_elems.join(", ")),
false,
)
}
};
Expand All @@ -585,10 +588,18 @@ impl Lang for Csharp<'_> {
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"
if nullable_single_column {
"NullableUniqueIndexBase"
} else {
"UniqueIndexBase"
}
} else {
csharp_index_class_name += "Index";
"BTreeIndexBase"
if nullable_single_column {
"NullableBTreeIndexBase"
} else {
"BTreeIndexBase"
}
};

writeln!(
Expand Down
Loading
Loading