Skip to content

Commit

Permalink
[release/v0.12.0-beta]: Manually apply open PR #1707: c# client generate
Browse files Browse the repository at this point in the history
  • Loading branch information
bfops committed Oct 2, 2024
1 parent 1681425 commit b5bdfe7
Show file tree
Hide file tree
Showing 52 changed files with 2,745 additions and 1,749 deletions.
35 changes: 27 additions & 8 deletions crates/bindings-csharp/BSATN.Codegen/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,29 +184,48 @@ IEnumerable<T> values
return sb;
}

private static object? ResolveConstant(TypedConstant constant) =>
constant.Kind switch
private static object? ResolveConstant(TypedConstant constant, System.Type targetType)
{
if (constant.Kind == TypedConstantKind.Array)
{
TypedConstantKind.Array => constant.Values.Select(ResolveConstant).ToArray(),
_ => constant.Value,
};
// We can't use LINQ ToArray() here because it doesn't support dynamic Type
// and will build `object[]` instead of the desired `T[]`.
var elementType = targetType.GetElementType();
var array = Array.CreateInstance(elementType, constant.Values.Length);
for (var i = 0; i < constant.Values.Length; i++)
{
array.SetValue(ResolveConstant(constant.Values[i], elementType), i);
}
return array;
}
return constant.Value;
}

public static T ParseAs<T>(this AttributeData attrData, System.Type? type = null)
where T : Attribute
{
type ??= typeof(T);
var ctorArgs = attrData.ConstructorArguments.Select(ResolveConstant).ToArray();

// For now only support attributes with a single constructor.
//
// Proper overload resolution is complicated due to implicit casts
// (in particular, enums are represented as integers in the attribute data),
// which prevent APIs like `Activator.CreateInstance` from finding the constructor.
//
// Expand logic in the future if it ever becomes actually necessary.
var attr = (T)type.GetConstructors().Single().Invoke(ctorArgs);
var ctor = type.GetConstructors().Single();

var ctorArgs = attrData
.ConstructorArguments.Zip(
ctor.GetParameters().Select(param => param.ParameterType),
ResolveConstant
)
.ToArray();
var attr = (T)ctor.Invoke(ctorArgs);
foreach (var arg in attrData.NamedArguments)
{
type.GetProperty(arg.Key).SetValue(attr, ResolveConstant(arg.Value));
var prop = type.GetProperty(arg.Key);
prop.SetValue(attr, ResolveConstant(arg.Value, prop.PropertyType));
}
return attr;
}
Expand Down
7 changes: 7 additions & 0 deletions crates/bindings-csharp/Codegen.Tests/fixtures/diag/Lib.cs
Original file line number Diff line number Diff line change
Expand Up @@ -367,3 +367,10 @@ public partial struct TestDuplicateTableName { }
)]
[SpacetimeDB.Table(Name = "TestIncompatibleSchedule2")]
public partial struct TestIncompatibleSchedule { }

[SpacetimeDB.Index.BTree]
public partial struct TestIndexWithoutColumns { }

[SpacetimeDB.Table]
[SpacetimeDB.Index.BTree(Columns = [])]
public partial struct TestIndexWithEmptyColumns { }

Large diffs are not rendered by default.

Loading

0 comments on commit b5bdfe7

Please sign in to comment.