Skip to content

Commit

Permalink
Fix indention of generated code
Browse files Browse the repository at this point in the history
  • Loading branch information
rickykaare committed Aug 9, 2024
1 parent 8679b23 commit c36232a
Show file tree
Hide file tree
Showing 14 changed files with 347 additions and 308 deletions.
108 changes: 69 additions & 39 deletions src/Cabazure.Client/ClientEndpointGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,36 +56,39 @@ private static void ProcessEndpoint(
source.AppendLine();
}

var indention = "";
if (endpoint.Namespace != null)
{
source.AppendLine($"namespace {endpoint.Namespace}");
source.AppendLine($"{{");
indention = " ";
}

source.AppendLine($$"""
internal partial class {{endpoint.ClassName}} : {{endpoint.InterfaceName}}
{
private readonly IHttpClientFactory factory;
private readonly IMessageRequestFactory requestFactory;

public {{endpoint.ClassName}}(
IHttpClientFactory factory,
IMessageRequestFactory requestFactory)
{
this.factory = factory;
this.requestFactory = requestFactory;
}
{{indention}}internal partial class {{endpoint.ClassName}} : {{endpoint.InterfaceName}}
{{indention}}{
{{indention}} private readonly IHttpClientFactory factory;
{{indention}} private readonly IMessageRequestFactory requestFactory;
{{indention}}
{{indention}} public {{endpoint.ClassName}}(
{{indention}} IHttpClientFactory factory,
{{indention}} IMessageRequestFactory requestFactory)
{{indention}} {
{{indention}} this.factory = factory;
{{indention}} this.requestFactory = requestFactory;
{{indention}} }
""");

foreach (var method in endpoint.Methods)
{
GenerateEndpointMethod(
source,
method,
endpoint.ClientName);
endpoint.ClientName,
indention);
}

source.AppendLine(" }");
source.AppendLine($"{indention}}}");
if (endpoint.Namespace != null)
{
source.AppendLine($"}}");
Expand All @@ -100,35 +103,54 @@ internal partial class {{endpoint.ClassName}} : {{endpoint.InterfaceName}}
private static void GenerateEndpointMethod(
StringBuilder source,
EndpointMethodDescriptor method,
string clientName)
string clientName,
string indention)
{
var clientOptions = new StringBuilder();
var requestOptions = new StringBuilder();

foreach (var p in method.PathParameters)
{
requestOptions.Append($"\n .WithPathParameter(\"{p.Name}\", {GetParameterValue(p)})");
requestOptions.Append($"""

{indention} .WithPathParameter("{p.Name}", {GetParameterValue(p)})
""");
}

foreach (var p in method.QueryParameters)
{
requestOptions.Append($"\n .WithQueryParameter(\"{p.Name}\", {GetParameterValue(p)})");
requestOptions.Append($"""

{indention} .WithQueryParameter("{p.Name}", {GetParameterValue(p)})
""");
}

foreach (var p in method.HeaderParameters)
{
requestOptions.Append($"\n .WithHeader(\"{p.Name}\", {GetParameterValue(p)})");
requestOptions.Append($"""

{indention} .WithHeader("{p.Name}", {GetParameterValue(p)})
""");
}

if (method.BodyParameter is { } b)
{
requestOptions.Append($"\n .WithBody({b})");
requestOptions.Append($"""

{indention} .WithBody({b})
""");
}

if (method.OptionsParameter is { } o)
{
clientOptions.Append($"\n .WithRequestOptions({o})");
requestOptions.Append($"\n .WithRequestOptions({o})");
clientOptions.Append($"""

{indention} .WithRequestOptions({o})
""");
requestOptions.Append($"""

{indention} .WithRequestOptions({o})
""");
}

var cancellationToken = method.CancellationTokenParameter
Expand All @@ -142,27 +164,35 @@ private static void GenerateEndpointMethod(

var resultConversion = method.ResponseType == null
? null
: $"\n response => new {method.ResponseType}(response),\n ";
: $"""

source.AppendLine($$"""

{{method.Signature}}
{
var client = factory.CreateClient("{{clientName}}");

using var requestMessage = requestFactory
.FromTemplate("{{clientName}}", "{{method.RouteTemplate}}"){{requestOptions}}
.Build(HttpMethod.{{method.HttpMethod}});
{indention} response => new {method.ResponseType}(response),
{indention}
""";

using var response = await client{{clientOptions}}
.SendAsync(requestMessage, {{cancellationToken}});
var parameters = string.Join(
",",
method.Parameters.Select(p => $"\n{indention} {p}"));

return await requestFactory
.FromResponse("{{clientName}}", response)
.AddSuccessResponse{{resultGeneric}}(HttpStatusCode.OK)
.GetAsync({{resultConversion}}{{cancellationToken}});
}
""");
source.AppendLine();
source.AppendLine($$"""
{{indention}} public async {{method.ReturnType}} {{method.Name}}({{parameters}})
{{indention}} {
{{indention}} var client = factory.CreateClient("{{clientName}}");
{{indention}}
{{indention}} using var requestMessage = requestFactory
{{indention}} .FromTemplate("{{clientName}}", "{{method.RouteTemplate}}"){{requestOptions}}
{{indention}} .Build(HttpMethod.{{method.HttpMethod}});
{{indention}}
{{indention}} using var response = await client{{clientOptions}}
{{indention}} .SendAsync(requestMessage, {{cancellationToken}});
{{indention}}
{{indention}} return await requestFactory
{{indention}} .FromResponse("{{clientName}}", response)
{{indention}} .AddSuccessResponse{{resultGeneric}}(HttpStatusCode.OK)
{{indention}} .GetAsync({{resultConversion}}{{cancellationToken}});
{{indention}} }
""");
}

private static string GetParameterValue(EndpointParameter parameter)
Expand Down
10 changes: 6 additions & 4 deletions src/Cabazure.Client/Descriptors/EndpointMethodDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ namespace Cabazure.Client.SourceGenerator.Descriptors;

public record EndpointMethodDescriptor(
string Name,
string Signature,
string? ResultType,
string[] Parameters,
string ReturnType,
string? ResponseType,
string? ResultType,
string HttpMethod,
string RouteTemplate,
string? OptionsParameter,
Expand Down Expand Up @@ -164,9 +165,10 @@ public record EndpointMethodDescriptor(

return new EndpointMethodDescriptor(
method.Identifier.ValueText,
$"public async {method.ReturnType} {method.Identifier}{method.ParameterList}",
returnType,
method.ParameterList.Parameters.Select(p => $"{p.Type} {p.Identifier}").ToArray(),
method.ReturnType.ToString(),
responseType,
returnType,
httpMethod,
routeTemplate,
optionsParameter,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,14 @@ private static void AddEmbeddedSources(GeneratorPostInitializationContext contex
{
var hintName = sourceFile.Substring(prefix.Length);
var stream = assembly.GetManifestResourceStream(sourceFile);
context.AddSource(hintName, SourceText.From(stream, canBeEmbedded: true));
context.AddSource(
hintName,
SourceText.From(
stream,
encoding: System.Text.Encoding.UTF8,
checksumAlgorithm: SourceHashAlgorithm.Sha1,
throwIfBinaryDetected: true,
canBeEmbedded: true));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,37 @@
using Cabazure.Client;
using Cabazure.Client.Builder;

internal partial class TestEndpoint : ITestEndpoint
{
private readonly IHttpClientFactory factory;
private readonly IMessageRequestFactory requestFactory;
internal partial class TestEndpoint : ITestEndpoint
{
private readonly IHttpClientFactory factory;
private readonly IMessageRequestFactory requestFactory;

public TestEndpoint(
IHttpClientFactory factory,
IMessageRequestFactory requestFactory)
{
this.factory = factory;
this.requestFactory = requestFactory;
}
public TestEndpoint(
IHttpClientFactory factory,
IMessageRequestFactory requestFactory)
{
this.factory = factory;
this.requestFactory = requestFactory;
}

public async Task<EndpointResponse<string[]>> ExecuteAsync(
public async Task<EndpointResponse<string[]>> ExecuteAsync(
CancellationToken cancellationToken)
{
var client = factory.CreateClient("ClientName");
{
var client = factory.CreateClient("ClientName");

using var requestMessage = requestFactory
.FromTemplate("ClientName", "/items")
.Build(HttpMethod.Get);
using var requestMessage = requestFactory
.FromTemplate("ClientName", "/items")
.Build(HttpMethod.Get);

using var response = await client
.SendAsync(requestMessage, cancellationToken);
using var response = await client
.SendAsync(requestMessage, cancellationToken);

return await requestFactory
.FromResponse("ClientName", response)
.AddSuccessResponse<string[]>(HttpStatusCode.OK)
.GetAsync(
response => new EndpointResponse<string[]>(response),
cancellationToken);
}
return await requestFactory
.FromResponse("ClientName", response)
.AddSuccessResponse<string[]>(HttpStatusCode.OK)
.GetAsync(
response => new EndpointResponse<string[]>(response),
cancellationToken);
}
}
#nullable disable
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ internal partial class TestEndpoint : ITestEndpoint
{
private readonly IHttpClientFactory factory;
private readonly IMessageRequestFactory requestFactory;

public TestEndpoint(
IHttpClientFactory factory,
IMessageRequestFactory requestFactory)
Expand All @@ -22,20 +22,20 @@ public TestEndpoint(
}

public async Task<EndpointResponse<string[]>> ExecuteAsync(
ClientRequestOptions options,
CancellationToken cancellationToken)
ClientRequestOptions options,
CancellationToken cancellationToken)
{
var client = factory.CreateClient("String");

using var requestMessage = requestFactory
.FromTemplate("String", "/routes")
.WithRequestOptions(options)
.Build(HttpMethod.Get);

using var response = await client
.WithRequestOptions(options)
.SendAsync(requestMessage, cancellationToken);

return await requestFactory
.FromResponse("String", response)
.AddSuccessResponse<string[]>(HttpStatusCode.OK)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,39 @@
using Cabazure.Client;
using Cabazure.Client.Builder;

internal partial class TestEndpoint : ITestEndpoint
{
private readonly IHttpClientFactory factory;
private readonly IMessageRequestFactory requestFactory;
internal partial class TestEndpoint : ITestEndpoint
{
private readonly IHttpClientFactory factory;
private readonly IMessageRequestFactory requestFactory;

public TestEndpoint(
IHttpClientFactory factory,
IMessageRequestFactory requestFactory)
{
this.factory = factory;
this.requestFactory = requestFactory;
}
public TestEndpoint(
IHttpClientFactory factory,
IMessageRequestFactory requestFactory)
{
this.factory = factory;
this.requestFactory = requestFactory;
}

public async Task<EndpointResponse<string[]>> ExecuteAsync(
public async Task<EndpointResponse<string[]>> ExecuteAsync(
ClientRequestOptions options)
{
var client = factory.CreateClient("ClientName");
{
var client = factory.CreateClient("ClientName");

using var requestMessage = requestFactory
.FromTemplate("ClientName", "/items")
.WithRequestOptions(options)
.Build(HttpMethod.Get);
using var requestMessage = requestFactory
.FromTemplate("ClientName", "/items")
.WithRequestOptions(options)
.Build(HttpMethod.Get);

using var response = await client
.WithRequestOptions(options)
.SendAsync(requestMessage, CancellationToken.None);
using var response = await client
.WithRequestOptions(options)
.SendAsync(requestMessage, CancellationToken.None);

return await requestFactory
.FromResponse("ClientName", response)
.AddSuccessResponse<string[]>(HttpStatusCode.OK)
.GetAsync(
response => new EndpointResponse<string[]>(response),
CancellationToken.None);
}
return await requestFactory
.FromResponse("ClientName", response)
.AddSuccessResponse<string[]>(HttpStatusCode.OK)
.GetAsync(
response => new EndpointResponse<string[]>(response),
CancellationToken.None);
}
}
#nullable disable
Loading

0 comments on commit c36232a

Please sign in to comment.