[C#] Add support for IEnumerable<T> for Views' return types#4486
[C#] Add support for IEnumerable<T> for Views' return types#4486
Conversation
Signed-off-by: Ryan <r.ekhoff@clockworklabs.io>
| && !ReturnType.BSATNName.Contains("SpacetimeDB.BSATN.RefOption") | ||
| && !ReturnType.BSATNName.Contains("SpacetimeDB.BSATN.List") |
There was a problem hiding this comment.
Do both of these options implement IEnumerable? If so, we could just assert IEnumerable or IQuery.
There was a problem hiding this comment.
No, neither actually implement IEnumerable themselves. Technically this is just validating that these are the BSATN representations. IQuery<T> also does not implement IEnumerable itself.
But if I understand the question, since we already check a bool for ReturnsQuery and ReturnsEnumerable, are you saying why don't we utilize that information in the test?
We could do that, but we'd miss checking for T? (the Option type return).
So I'd need to check something like
bool isOption =
ReturnType.BSATNName.Contains("SpacetimeDB.BSATN.ValueOption")
|| ReturnType.BSATNName.Contains("SpacetimeDB.BSATN.RefOption");And then could change the check to something like:
if (!ReturnsQuery && !ReturnsEnumerable && !ReturnsOption)
{
diag.Report(ErrorDescriptor.ViewInvalidReturn, methodSyntax);
}If you'd prefer, and find this to be more clear.
There was a problem hiding this comment.
Yeah let's do that. I was hoping that T? would be IEnumerable too, but we can at least have IEnumerable subsume List<T> to avoid a redundant check.
There was a problem hiding this comment.
Requested updates made
| OriginalDefinition: var listDefinition, | ||
| TypeArguments.Length: 1, | ||
| } | ||
| && listDefinition.ToString() == "System.Collections.Generic.List<T>" |
There was a problem hiding this comment.
Do we still need this check or does IEnumerable make this one obsolete?
There was a problem hiding this comment.
Yes, we still need it, because the List<T> is a valid return type. If we don't have that as a valid return type, any places where List<T> is returned, it will report that as an error. Even if there is a conversion between List<T> and IEnumerable, we accept both at this point.
This is the implementation to resolve #4424
Description of Changes
IEnumerable<T>as a valid C# View return type in codegen, so view implementations can return query results without forcingToList(). The generator now detectsIEnumerable<T>, serializes it via the list serializer.IEnumerable<T>rather than checking it is unsupported (which we had added when removing support during the transition away fromQuery<T>usage.API and ABI breaking changes
No breaking changes. This is additive support for an existing .NET interface.
Expected complexity level and risk
2 - Low
Testing
dotnet testsin C# module bindings.