Skip to content

Commit f5df540

Browse files
authored
Merge pull request #20 from Cabazure/alias-support
Add support for using aliases
2 parents 44b093b + a8b622f commit f5df540

File tree

7 files changed

+95
-18
lines changed

7 files changed

+95
-18
lines changed

.github/workflows/release.yml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,6 @@ jobs:
5454
reporter: dotnet-trx
5555
fail-on-error: true
5656

57-
- name: 📋 Test Report
58-
uses: dorny/test-reporter@v1
59-
if: always()
60-
with:
61-
name: Test Report
62-
path: "**/test-results.trx"
63-
reporter: dotnet-trx
64-
fail-on-error: true
65-
6657
- name: 📭 Restore coverage history
6758
uses: dawidd6/action-download-artifact@v6
6859
with:

Cabazure.Client.sln

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ EndProject
1616
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{9FD398E9-51D6-4C0E-AD0A-D85A4B9D014F}"
1717
EndProject
1818
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{115ACFA6-5271-4A7B-844D-FFEA0A4F6BB7}"
19+
ProjectSection(SolutionItems) = preProject
20+
test\.editorconfig = test\.editorconfig
21+
EndProjectSection
1922
EndProject
2023
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{3932F1A3-4272-4E08-83F6-E1EB7DCED0FC}"
2124
EndProject
@@ -27,6 +30,23 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AzureRest.Contracts", "samp
2730
EndProject
2831
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AzureRest.TestApp", "samples\AzureRest\AzureRest.TestApp\AzureRest.TestApp.csproj", "{CF5F841E-FC86-441D-8075-49EA5F280AB8}"
2932
EndProject
33+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".config", ".config", "{02EA681E-C7D8-13C7-8484-4AC65E1B71E8}"
34+
ProjectSection(SolutionItems) = preProject
35+
.editorconfig = .editorconfig
36+
Directory.Build.props = Directory.Build.props
37+
LICENSE = LICENSE
38+
README.md = README.md
39+
EndProjectSection
40+
EndProject
41+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".github", ".github", "{02EA681E-C7D8-13C7-8484-4AC65E1B71E8}"
42+
EndProject
43+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "workflows", "workflows", "{02EA681E-C7D8-13C7-8484-4AC65E1B71E8}"
44+
ProjectSection(SolutionItems) = preProject
45+
.github\workflows\ci.yml = .github\workflows\ci.yml
46+
.github\workflows\release-preview.yml = .github\workflows\release-preview.yml
47+
.github\workflows\release.yml = .github\workflows\release.yml
48+
EndProjectSection
49+
EndProject
3050
Global
3151
GlobalSection(SolutionConfigurationPlatforms) = preSolution
3252
Debug|Any CPU = Debug|Any CPU
@@ -79,6 +99,7 @@ Global
7999
{469837C7-D9B8-4867-A1CD-747DBA16213B} = {D2D03799-0BB4-45C1-9D35-60F364C154EE}
80100
{C19CB361-795D-49FF-8821-E4C739EE5020} = {D2D03799-0BB4-45C1-9D35-60F364C154EE}
81101
{CF5F841E-FC86-441D-8075-49EA5F280AB8} = {D2D03799-0BB4-45C1-9D35-60F364C154EE}
102+
{02EA681E-C7D8-13C7-8484-4AC65E1B71E8} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
82103
EndGlobalSection
83104
GlobalSection(ExtensibilityGlobals) = postSolution
84105
SolutionGuid = {3DD1DF42-FFAB-4F35-B264-B128D2778D40}

src/Cabazure.Client/ClientEndpointGenerator.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,21 @@ private static void ProcessEndpoint(
3838

3939
var usingStatements = endpoint
4040
.Usings
41-
.Append("System.Net")
42-
.Append("System.Net.Http")
43-
.Append("Cabazure.Client")
44-
.Append("Cabazure.Client.Builder")
45-
.Where(us => us != endpoint.Namespace)
41+
.Append("using System.Net;")
42+
.Append("using System.Net.Http;")
43+
.Append("using Cabazure.Client;")
44+
.Append("using Cabazure.Client.Builder;")
45+
.Where(us => us != $"using {endpoint.Namespace};")
4646
.Distinct()
47-
.OrderByDescending(us => us.StartsWith("System", StringComparison.Ordinal))
47+
.OrderByDescending(us => us.StartsWith("using System", StringComparison.Ordinal))
48+
.ThenBy(us => us.Contains('='))
4849
.ThenBy(us => us)
4950
.ToArray();
5051
if (usingStatements.Length > 0)
5152
{
5253
foreach (var us in usingStatements)
5354
{
54-
source.AppendLine($"using {us};");
55+
source.AppendLine(us);
5556
}
5657
source.AppendLine();
5758
}
@@ -171,7 +172,7 @@ private static void GenerateEndpointMethod(
171172
""";
172173

173174
var parameters = string.Join(
174-
",",
175+
",",
175176
method.Parameters.Select(p => $"\n{indention} {p}"));
176177

177178
source.AppendLine();

src/Cabazure.Client/Descriptors/EndpointDescriptor.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ public record EndpointDescriptor(
5454

5555
var usings = @interface
5656
.GetUsings()
57-
.Select(u => u.Name?.ToString())
57+
.ToArray()
58+
.Select(u => u.ToString())
5859
.OfType<string>()
5960
.ToImmutableArray();
6061

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//HintName: TestEndpoint.g.cs
2+
// <auto-generated/>
3+
#nullable enable
4+
using System.Net;
5+
using System.Net.Http;
6+
using Cabazure.Client;
7+
using Cabazure.Client.Builder;
8+
using TypeAlias = System.Text.Json.Nodes.JsonArray;
9+
10+
internal partial class TestEndpoint : ITestEndpoint
11+
{
12+
private readonly IHttpClientFactory factory;
13+
private readonly IMessageRequestFactory requestFactory;
14+
15+
public TestEndpoint(
16+
IHttpClientFactory factory,
17+
IMessageRequestFactory requestFactory)
18+
{
19+
this.factory = factory;
20+
this.requestFactory = requestFactory;
21+
}
22+
23+
public async Task<EndpointResponse> ExecuteAsync(
24+
TypeAlias body,
25+
ClientRequestOptions options,
26+
CancellationToken cancellationToken)
27+
{
28+
var client = factory.CreateClient("ClientName");
29+
30+
using var requestMessage = requestFactory
31+
.FromTemplate("ClientName", "/items")
32+
.WithBody(body)
33+
.WithRequestOptions(options)
34+
.Build(HttpMethod.Get);
35+
36+
using var response = await client
37+
.WithRequestOptions(options)
38+
.SendAsync(requestMessage, cancellationToken);
39+
40+
return await requestFactory
41+
.FromResponse("ClientName", response)
42+
.AddSuccessResponse(HttpStatusCode.OK)
43+
.GetAsync(cancellationToken);
44+
}
45+
}
46+
#nullable disable

test/Cabazure.Client.Tests/ClientEndpointGeneratorTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,4 +287,20 @@ public Task<EndpointResponse<string[]>> ExecuteAsync(
287287
CancellationToken cancellationToken);
288288
}
289289
""");
290+
291+
[Fact]
292+
public Task CanGenerate_Using_Alias()
293+
=> TestHelper.VerifyEndpoint("""
294+
using TypeAlias = System.Text.Json.Nodes.JsonArray;
295+
296+
[ClientEndpoint("ClientName")]
297+
public interface ITestEndpoint
298+
{
299+
[Get("/items")]
300+
public Task<EndpointResponse> ExecuteAsync(
301+
[Body] TypeAlias body,
302+
ClientRequestOptions options,
303+
CancellationToken cancellationToken);
304+
}
305+
""");
290306
}

0 commit comments

Comments
 (0)