Add collection return support to ScanForTypes attribute#55
Add collection return support to ScanForTypes attribute#55Dreamescaper merged 3 commits intomainfrom
Conversation
…ble<T>) Co-authored-by: Dreamescaper <17177729+Dreamescaper@users.noreply.github.com>
| { | ||
| public static partial global::System.Type[] GetServiceTypes() | ||
| { | ||
| return [typeof(global::GeneratorTests.MyService1), typeof(global::GeneratorTests.MyService2)]; |
There was a problem hiding this comment.
@copilot
Update to use multiline collection expression, e.g.
public static partial global::System.Type[] GetServiceTypes()
{
return [
typeof(global::GeneratorTests.MyService1),
typeof(global::GeneratorTests.MyService2)
];
}
There was a problem hiding this comment.
Updated in a8286a9. GenerateCollectionSource now emits:
return [
typeof(global::GeneratorTests.MyService1),
typeof(global::GeneratorTests.MyService2)
];
All test expectations updated to match.
| ? GenerateRegistrationsSource(method, registrations) | ||
| : GenerateCustomHandlingSource(method, customHandling); | ||
| : method.ReturnTypeIsCollection | ||
| ? GenerateCollectionSource(method, collectionItems) |
There was a problem hiding this comment.
@copilot
replace nested ternary with switch expression
There was a problem hiding this comment.
Replaced in a8286a9:
string source = (registrations.Count, method.ReturnTypeIsCollection) switch
{
( > 0, _) => GenerateRegistrationsSource(method, registrations),
(_, true) => GenerateCollectionSource(method, collectionItems),
_ => GenerateCustomHandlingSource(method, customHandling)
};| typesFound = true; | ||
|
|
||
| if (attribute.CustomHandler != null) | ||
| var implementationTypeName = implementationType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat); |
There was a problem hiding this comment.
@copilot
extract collection handling and custom handler to private methods
There was a problem hiding this comment.
Done in a8286a9. Extracted to AddCollectionItems and AddCustomHandlerItems private methods. The loop body is now:
if (method.ReturnTypeIsCollection)
AddCollectionItems(implementationType, matchedTypes, attribute, method, collectionItems);
else if (attribute.CustomHandler != null)
AddCustomHandlerItems(implementationType, matchedTypes, attribute, customHandlers);
else
{ /* registration logic */ }…thods, README Co-authored-by: Dreamescaper <17177729+Dreamescaper@users.noreply.github.com> Agent-Logs-Url: https://github.com/Dreamescaper/ServiceScan.SourceGenerator/sessions/a26f24d8-be32-4167-9a0b-2eaed9dd023e
Updated in a8286a9. Added two new examples to the README: "Get all matched types as a collection" (returning |
ScanForTypesmethods can now returnType[]orIEnumerable<Type>(no Handler needed) to get the matched types directly, or returnTResponse[]/IEnumerable<TResponse>when a Handler mappingT → TResponseis provided. This feature is exclusive toScanForTypes;GenerateServiceRegistrationsis unaffected.New capabilities
Changes
MethodModel— addedReturnTypeIsCollection/CollectionElementTypeNamefields and aGetCollectionReturnInfo()helper that detects array andIEnumerable<T>return types usingSpecialType.System_Collections_Generic_IEnumerable_T.MethodImplementationModel— addedCollectionItems(EquatableArray<string>) holding the pre-built expression strings for each collection element.ParseHandlerMethodModel— relaxes the "Handler required" rule when the return type isType[]/IEnumerable<Type>; validates Handler return type matches the collection element type for mapped returns.FindServicesToRegister— populatesCollectionItemswithtypeof(T)expressions (no-Handler path) or handler invocation expressions (Handler path).DependencyInjectionGenerator— addsGenerateCollectionSource()emitting a C# collection expression (return [...]); routes generation accordingly.DiagnosticDescriptors— new diagnosticDI0015(WrongHandlerReturnTypeForCollectionReturn) when the Handler's return type doesn't match the collection element type.Original prompt
📍 Connect Copilot coding agent with Jira, Azure Boards or Linear to delegate work to Copilot in one click without leaving your project management tool.