Skip to content

Commit 0a7512d

Browse files
committed
[release/v0.12.0-beta]: Manually apply open PR #1707: c# client generate
1 parent aff09eb commit 0a7512d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+2768
-1749
lines changed

crates/bindings-csharp/BSATN.Codegen/Utils.cs

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -184,29 +184,48 @@ IEnumerable<T> values
184184
return sb;
185185
}
186186

187-
private static object? ResolveConstant(TypedConstant constant) =>
188-
constant.Kind switch
187+
private static object? ResolveConstant(TypedConstant constant, System.Type targetType)
188+
{
189+
if (constant.Kind == TypedConstantKind.Array)
189190
{
190-
TypedConstantKind.Array => constant.Values.Select(ResolveConstant).ToArray(),
191-
_ => constant.Value,
192-
};
191+
// We can't use LINQ ToArray() here because it doesn't support dynamic Type
192+
// and will build `object[]` instead of the desired `T[]`.
193+
var elementType = targetType.GetElementType();
194+
var array = Array.CreateInstance(elementType, constant.Values.Length);
195+
for (var i = 0; i < constant.Values.Length; i++)
196+
{
197+
array.SetValue(ResolveConstant(constant.Values[i], elementType), i);
198+
}
199+
return array;
200+
}
201+
return constant.Value;
202+
}
193203

194204
public static T ParseAs<T>(this AttributeData attrData, System.Type? type = null)
195205
where T : Attribute
196206
{
197207
type ??= typeof(T);
198-
var ctorArgs = attrData.ConstructorArguments.Select(ResolveConstant).ToArray();
208+
199209
// For now only support attributes with a single constructor.
200210
//
201211
// Proper overload resolution is complicated due to implicit casts
202212
// (in particular, enums are represented as integers in the attribute data),
203213
// which prevent APIs like `Activator.CreateInstance` from finding the constructor.
204214
//
205215
// Expand logic in the future if it ever becomes actually necessary.
206-
var attr = (T)type.GetConstructors().Single().Invoke(ctorArgs);
216+
var ctor = type.GetConstructors().Single();
217+
218+
var ctorArgs = attrData
219+
.ConstructorArguments.Zip(
220+
ctor.GetParameters().Select(param => param.ParameterType),
221+
ResolveConstant
222+
)
223+
.ToArray();
224+
var attr = (T)ctor.Invoke(ctorArgs);
207225
foreach (var arg in attrData.NamedArguments)
208226
{
209-
type.GetProperty(arg.Key).SetValue(attr, ResolveConstant(arg.Value));
227+
var prop = type.GetProperty(arg.Key);
228+
prop.SetValue(attr, ResolveConstant(arg.Value, prop.PropertyType));
210229
}
211230
return attr;
212231
}

crates/bindings-csharp/Codegen.Tests/fixtures/diag/Lib.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,3 +367,11 @@ public partial struct TestDuplicateTableName { }
367367
)]
368368
[SpacetimeDB.Table(Name = "TestIncompatibleSchedule2")]
369369
public partial struct TestIncompatibleSchedule { }
370+
371+
[SpacetimeDB.Table]
372+
[SpacetimeDB.Index.BTree]
373+
public partial struct TestIndexWithoutColumns { }
374+
375+
[SpacetimeDB.Table]
376+
[SpacetimeDB.Index.BTree(Columns = [])]
377+
public partial struct TestIndexWithEmptyColumns { }

crates/bindings-csharp/Codegen.Tests/fixtures/diag/snapshots/ExtraCompilationErrors.verified.txt

Lines changed: 119 additions & 47 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)