Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<Compile Include="$(SharedSourceRoot)RoslynUtils\Parsability.cs" LinkBase="Shared"/>
<Compile Include="$(SharedSourceRoot)RoslynUtils\ParsabilityMethod.cs" LinkBase="Shared"/>
<Compile Include="$(SharedSourceRoot)RoslynUtils\ParsabilityHelper.cs" LinkBase="Shared"/>
<Compile Include="$(SharedSourceRoot)RoslynUtils\CodeWriter.cs" LinkBase="Shared" />
</ItemGroup>

</Project>
51 changes: 28 additions & 23 deletions src/Http/Http.Extensions/gen/RequestDelegateGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.AspNetCore.App.Analyzers.Infrastructure;
Expand Down Expand Up @@ -86,9 +88,8 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
handler.Method);
}

{{endpoint.EmitRequestHandler()}}
{{endpoint.EmitFilteredRequestHandler()}}

{{endpoint.EmitRequestHandler(baseIndent: 5)}}
{{endpoint.EmitFilteredRequestHandler(baseIndent: 5)}}
RequestDelegate targetDelegate = filteredInvocation is null ? RequestHandler : RequestHandlerFiltered;
var metadata = inferredMetadataResult?.EndpointMetadata ?? ReadOnlyCollection<object>.Empty;
return new RequestDelegateResult(targetDelegate, metadata);
Expand All @@ -101,28 +102,32 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
{
var dedupedByDelegate = endpoints.Distinct(EndpointDelegateComparer.Instance);
var code = new StringBuilder();
using var stringWriter = new StringWriter(CultureInfo.InvariantCulture);
using var codeWriter = new CodeWriter(stringWriter, baseIndent: 2);
foreach (var endpoint in dedupedByDelegate)
{
code.AppendLine($$"""
internal static global::Microsoft.AspNetCore.Builder.RouteHandlerBuilder {{endpoint.HttpMethod}}(
this global::Microsoft.AspNetCore.Routing.IEndpointRouteBuilder endpoints,
[global::System.Diagnostics.CodeAnalysis.StringSyntax("Route")] string pattern,
global::{{endpoint.EmitHandlerDelegateType()}} handler,
[global::System.Runtime.CompilerServices.CallerFilePath] string filePath = "",
[global::System.Runtime.CompilerServices.CallerLineNumber]int lineNumber = 0)
{
return global::Microsoft.AspNetCore.Http.Generated.GeneratedRouteBuilderExtensionsCore.MapCore(
endpoints,
pattern,
handler,
{{endpoint.EmitVerb()}},
filePath,
lineNumber);
}
""");
}

return code.ToString();
codeWriter.WriteLine($"internal static global::Microsoft.AspNetCore.Builder.RouteHandlerBuilder {endpoint.HttpMethod}(");
codeWriter.Indent++;
codeWriter.WriteLine("this global::Microsoft.AspNetCore.Routing.IEndpointRouteBuilder endpoints,");
codeWriter.WriteLine(@"[global::System.Diagnostics.CodeAnalysis.StringSyntax(""Route"")] string pattern,");
codeWriter.WriteLine($"global::{endpoint.EmitHandlerDelegateType()} handler,");
codeWriter.WriteLine(@"[global::System.Runtime.CompilerServices.CallerFilePath] string filePath = """",");
codeWriter.WriteLine("[global::System.Runtime.CompilerServices.CallerLineNumber]int lineNumber = 0)");
codeWriter.Indent--;
codeWriter.StartBlock();
codeWriter.WriteLine("return global::Microsoft.AspNetCore.Http.Generated.GeneratedRouteBuilderExtensionsCore.MapCore(");
codeWriter.Indent++;
codeWriter.WriteLine("endpoints,");
codeWriter.WriteLine("pattern,");
codeWriter.WriteLine("handler,");
codeWriter.WriteLine($"{endpoint.EmitVerb()},");
codeWriter.WriteLine("filePath,");
codeWriter.WriteLine("lineNumber);");
codeWriter.Indent--;
codeWriter.EndBlock();
}

return stringWriter.ToString();
});

var thunksAndEndpoints = thunks.Collect().Combine(stronglyTypedEndpointDefinitions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1570,7 +1570,7 @@ internal static class GenerateRouteBuilderEndpoints
private static readonly string[] DeleteVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Delete };
private static readonly string[] PatchVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Patch };

{{endpoints}}
{{endpoints}}
}
""" : string.Empty;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,54 +3,44 @@

using System;
using System.Linq;
using System.Globalization;
using System.IO;
using System.Text;

namespace Microsoft.AspNetCore.Http.Generators.StaticRouteHandlerModel.Emitters;
internal static class EndpointEmitter
{
internal static string EmitParameterPreparation(this Endpoint endpoint)
internal static string EmitParameterPreparation(this Endpoint endpoint, int baseIndent = 0)
{
var parameterPreparationBuilder = new StringBuilder();
using var stringWriter = new StringWriter(CultureInfo.InvariantCulture);
using var parameterPreparationBuilder = new CodeWriter(stringWriter, baseIndent);

for (var parameterIndex = 0; parameterIndex < endpoint.Parameters.Length; parameterIndex++)
foreach (var parameter in endpoint.Parameters)
{
var parameter = endpoint.Parameters[parameterIndex];

var parameterPreparationCode = parameter switch
{
{
Source: EndpointParameterSource.SpecialType
} => parameter.EmitSpecialParameterPreparation(),
{
Source: EndpointParameterSource.Query or EndpointParameterSource.Header,
} => parameter.EmitQueryOrHeaderParameterPreparation(),
{
Source: EndpointParameterSource.Route,
} => parameter.EmitRouteParameterPreparation(),
{
Source: EndpointParameterSource.RouteOrQuery
} => parameter.EmitRouteOrQueryParameterPreparation(),
{
Source: EndpointParameterSource.JsonBody
} => parameter.EmitJsonBodyParameterPreparationString(),
{
Source: EndpointParameterSource.Service
} => parameter.EmitServiceParameterPreparation(),
_ => throw new Exception("Unreachable!")
};

// To avoid having two newlines after the block of parameter handling code.
if (parameterIndex < endpoint.Parameters.Length - 1)
{
parameterPreparationBuilder.AppendLine(parameterPreparationCode);
}
else
switch (parameter)
{
parameterPreparationBuilder.Append(parameterPreparationCode);
case { Source: EndpointParameterSource.SpecialType }:
parameter.EmitSpecialParameterPreparation(parameterPreparationBuilder);
break;
case { Source: EndpointParameterSource.Query or EndpointParameterSource.Header }:
parameter.EmitQueryOrHeaderParameterPreparation(parameterPreparationBuilder);
break;
case { Source: EndpointParameterSource.Route }:
parameter.EmitRouteParameterPreparation(parameterPreparationBuilder);
break;
case { Source: EndpointParameterSource.RouteOrQuery }:
parameter.EmitRouteOrQueryParameterPreparation(parameterPreparationBuilder);
break;
case { Source: EndpointParameterSource.JsonBody }:
parameter.EmitJsonBodyParameterPreparationString(parameterPreparationBuilder);
break;
case { Source: EndpointParameterSource.Service }:
parameter.EmitServiceParameterPreparation(parameterPreparationBuilder);
break;
}
}

return parameterPreparationBuilder.ToString();
return stringWriter.ToString();
}

public static string EmitArgumentList(this Endpoint endpoint) => string.Join(", ", endpoint.Parameters.Select(p => p.EmitArgument()));
Expand Down
Loading