Support for synchronous lambdas

This commit is contained in:
2024-10-18 02:05:46 +02:00
parent fff6f2c7ba
commit 8789f994bf
3 changed files with 19 additions and 5 deletions
@@ -4,7 +4,7 @@
public class SimpleRoute2
{
[GeneratePost("somethingSimple")]
public async Task<IResult> GetSomething()
public IResult GetSomething()
{
return Results.Ok();
}
@@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>0.8.16</Version>
<Version>0.8.17</Version>
<LangVersion>latest</LangVersion>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>Alexandru Macocian</Authors>
@@ -255,12 +255,26 @@ public class UseRoutesGenerator : IIncrementalGenerator
var variables = string.Join(", ", methodDeclarationSyntax.ParameterList.Parameters
.Select(param => param.Identifier.Text));
return @$"
/*
* Generate an async lambda for Task<IResult> or a synchronous lambda for IResult returns. If neither are matched, do not generate any method
*/
var returnTypeSymbol = semanticModel.GetSymbolInfo(methodDeclarationSyntax.ReturnType).Symbol as ITypeSymbol;
return returnTypeSymbol?.ToDisplayString() switch
{
"System.Threading.Tasks.Task<Microsoft.AspNetCore.Http.IResult>" => @$"
builder.Map{type}(""{pattern}"", async (HttpContext httpContext, {classDeclarationSyntax.Identifier} route{(parameters.Length > 0 ? $", {parameters}" : "")}) =>
{{
var cancellationToken = httpContext.RequestAborted;
return await route.{methodDeclarationSyntax.Identifier}({variables});
}}){routeFilterSb};";
}}){routeFilterSb};",
"Microsoft.AspNetCore.Http.IResult" => @$"
builder.Map{type}(""{pattern}"", (HttpContext httpContext, {classDeclarationSyntax.Identifier} route{(parameters.Length > 0 ? $", {parameters}" : "")}) =>
{{
var cancellationToken = httpContext.RequestAborted;
return route.{methodDeclarationSyntax.Identifier}({variables});
}}){routeFilterSb};",
_ => default
};
}
private static string GetMethodParameters(MethodDeclarationSyntax methodDeclaration)