Skip to content

Commit

Permalink
feat: generate code that uses Array.Empty where possible (#1599)
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyMakkison committed Nov 17, 2023
1 parent af399f0 commit 66edaaa
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 43 deletions.
14 changes: 11 additions & 3 deletions InterfaceStubGenerator.Shared/InterfaceStubGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -413,11 +413,19 @@ void ProcessRefitMethod(StringBuilder source, IMethodSymbol methodSymbol, bool i
genericList.Add($"typeof({typeParam.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)})");
}

var genericString = genericList.Count > 0 ? $", new global::System.Type[] {{ {string.Join(", ", genericList)} }}" : string.Empty;
var argumentsArrayString = argList.Count == 0
? "global::System.Array.Empty<object>()"
: $"new object[] {{ {string.Join(", ", argList)} }}";

var parameterTypesArrayString = typeList.Count == 0
? "global::System.Array.Empty<global::System.Type>()"
: $"new global::System.Type[] {{ {string.Join(", ", typeList)} }}";

var genericString = genericList.Count > 0 ? $", new global::System.Type[] {{ {string.Join(", ", genericList)} }}" : string.Empty;

source.Append(@$"
var ______arguments = new object[] {{ {string.Join(", ", argList)} }};
var ______func = requestBuilder.BuildRestResultFuncForMethod(""{methodSymbol.Name}"", new global::System.Type[] {{ {string.Join(", ", typeList)} }}{genericString} );
var ______arguments = {argumentsArrayString};
var ______func = requestBuilder.BuildRestResultFuncForMethod(""{methodSymbol.Name}"", {parameterTypesArrayString}{genericString} );
try
{{
{@return}({returnType})______func(this.Client, ______arguments){configureAwait};
Expand Down
80 changes: 40 additions & 40 deletions Refit.Tests/InterfaceStubGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,8 @@ public RefitTestsIGitHubApi(global::System.Net.Http.HttpClient client, global::R
/// <inheritdoc />
public async global::System.Threading.Tasks.Task<global::System.Net.Http.HttpResponseMessage> GetIndex()
{
var ______arguments = new object[] { };
var ______func = requestBuilder.BuildRestResultFuncForMethod(""GetIndex"", new global::System.Type[] { } );
var ______arguments = global::System.Array.Empty<object>();
var ______func = requestBuilder.BuildRestResultFuncForMethod(""GetIndex"", global::System.Array.Empty<global::System.Type>() );
try
{
return await ((global::System.Threading.Tasks.Task<global::System.Net.Http.HttpResponseMessage>)______func(this.Client, ______arguments)).ConfigureAwait(false);
Expand All @@ -296,8 +296,8 @@ public RefitTestsIGitHubApi(global::System.Net.Http.HttpClient client, global::R
/// <inheritdoc />
public global::System.IObservable<string> GetIndexObservable()
{
var ______arguments = new object[] { };
var ______func = requestBuilder.BuildRestResultFuncForMethod(""GetIndexObservable"", new global::System.Type[] { } );
var ______arguments = global::System.Array.Empty<object>();
var ______func = requestBuilder.BuildRestResultFuncForMethod(""GetIndexObservable"", global::System.Array.Empty<global::System.Type>() );
try
{
return (global::System.IObservable<string>)______func(this.Client, ______arguments);
Expand All @@ -311,8 +311,8 @@ public RefitTestsIGitHubApi(global::System.Net.Http.HttpClient client, global::R
/// <inheritdoc />
public async global::System.Threading.Tasks.Task<global::Refit.Tests.User> NothingToSeeHere()
{
var ______arguments = new object[] { };
var ______func = requestBuilder.BuildRestResultFuncForMethod(""NothingToSeeHere"", new global::System.Type[] { } );
var ______arguments = global::System.Array.Empty<object>();
var ______func = requestBuilder.BuildRestResultFuncForMethod(""NothingToSeeHere"", global::System.Array.Empty<global::System.Type>() );
try
{
return await ((global::System.Threading.Tasks.Task<global::Refit.Tests.User>)______func(this.Client, ______arguments)).ConfigureAwait(false);
Expand All @@ -326,8 +326,8 @@ public RefitTestsIGitHubApi(global::System.Net.Http.HttpClient client, global::R
/// <inheritdoc />
public async global::System.Threading.Tasks.Task<global::Refit.ApiResponse<global::Refit.Tests.User>> NothingToSeeHereWithMetadata()
{
var ______arguments = new object[] { };
var ______func = requestBuilder.BuildRestResultFuncForMethod(""NothingToSeeHereWithMetadata"", new global::System.Type[] { } );
var ______arguments = global::System.Array.Empty<object>();
var ______func = requestBuilder.BuildRestResultFuncForMethod(""NothingToSeeHereWithMetadata"", global::System.Array.Empty<global::System.Type>() );
try
{
return await ((global::System.Threading.Tasks.Task<global::Refit.ApiResponse<global::Refit.Tests.User>>)______func(this.Client, ______arguments)).ConfigureAwait(false);
Expand Down Expand Up @@ -476,8 +476,8 @@ public RefitTestsIGitHubApi(global::System.Net.Http.HttpClient client, global::R
/// <inheritdoc />
async global::System.Threading.Tasks.Task<global::System.Net.Http.HttpResponseMessage> global::Refit.Tests.IGitHubApi.GetIndex()
{
var ______arguments = new object[] { };
var ______func = requestBuilder.BuildRestResultFuncForMethod(""GetIndex"", new global::System.Type[] { } );
var ______arguments = global::System.Array.Empty<object>();
var ______func = requestBuilder.BuildRestResultFuncForMethod(""GetIndex"", global::System.Array.Empty<global::System.Type>() );
try
{
return await ((global::System.Threading.Tasks.Task<global::System.Net.Http.HttpResponseMessage>)______func(this.Client, ______arguments)).ConfigureAwait(false);
Expand All @@ -491,8 +491,8 @@ public RefitTestsIGitHubApi(global::System.Net.Http.HttpClient client, global::R
/// <inheritdoc />
global::System.IObservable<string> global::Refit.Tests.IGitHubApi.GetIndexObservable()
{
var ______arguments = new object[] { };
var ______func = requestBuilder.BuildRestResultFuncForMethod(""GetIndexObservable"", new global::System.Type[] { } );
var ______arguments = global::System.Array.Empty<object>();
var ______func = requestBuilder.BuildRestResultFuncForMethod(""GetIndexObservable"", global::System.Array.Empty<global::System.Type>() );
try
{
return (global::System.IObservable<string>)______func(this.Client, ______arguments);
Expand All @@ -506,8 +506,8 @@ public RefitTestsIGitHubApi(global::System.Net.Http.HttpClient client, global::R
/// <inheritdoc />
async global::System.Threading.Tasks.Task<global::Refit.Tests.User> global::Refit.Tests.IGitHubApi.NothingToSeeHere()
{
var ______arguments = new object[] { };
var ______func = requestBuilder.BuildRestResultFuncForMethod(""NothingToSeeHere"", new global::System.Type[] { } );
var ______arguments = global::System.Array.Empty<object>();
var ______func = requestBuilder.BuildRestResultFuncForMethod(""NothingToSeeHere"", global::System.Array.Empty<global::System.Type>() );
try
{
return await ((global::System.Threading.Tasks.Task<global::Refit.Tests.User>)______func(this.Client, ______arguments)).ConfigureAwait(false);
Expand All @@ -521,8 +521,8 @@ public RefitTestsIGitHubApi(global::System.Net.Http.HttpClient client, global::R
/// <inheritdoc />
async global::System.Threading.Tasks.Task<global::Refit.ApiResponse<global::Refit.Tests.User>> global::Refit.Tests.IGitHubApi.NothingToSeeHereWithMetadata()
{
var ______arguments = new object[] { };
var ______func = requestBuilder.BuildRestResultFuncForMethod(""NothingToSeeHereWithMetadata"", new global::System.Type[] { } );
var ______arguments = global::System.Array.Empty<object>();
var ______func = requestBuilder.BuildRestResultFuncForMethod(""NothingToSeeHereWithMetadata"", global::System.Array.Empty<global::System.Type>() );
try
{
return await ((global::System.Threading.Tasks.Task<global::Refit.ApiResponse<global::Refit.Tests.User>>)______func(this.Client, ______arguments)).ConfigureAwait(false);
Expand Down Expand Up @@ -632,8 +632,8 @@ public RefitTestsIGitHubApiDisposable(global::System.Net.Http.HttpClient client,
/// <inheritdoc />
public async global::System.Threading.Tasks.Task RefitMethod()
{
var ______arguments = new object[] { };
var ______func = requestBuilder.BuildRestResultFuncForMethod(""RefitMethod"", new global::System.Type[] { } );
var ______arguments = global::System.Array.Empty<object>();
var ______func = requestBuilder.BuildRestResultFuncForMethod(""RefitMethod"", global::System.Array.Empty<global::System.Type>() );
try
{
await ((global::System.Threading.Tasks.Task)______func(this.Client, ______arguments)).ConfigureAwait(false);
Expand All @@ -647,8 +647,8 @@ public RefitTestsIGitHubApiDisposable(global::System.Net.Http.HttpClient client,
/// <inheritdoc />
async global::System.Threading.Tasks.Task global::Refit.Tests.IGitHubApiDisposable.RefitMethod()
{
var ______arguments = new object[] { };
var ______func = requestBuilder.BuildRestResultFuncForMethod(""RefitMethod"", new global::System.Type[] { } );
var ______arguments = global::System.Array.Empty<object>();
var ______func = requestBuilder.BuildRestResultFuncForMethod(""RefitMethod"", global::System.Array.Empty<global::System.Type>() );
try
{
await ((global::System.Threading.Tasks.Task)______func(this.Client, ______arguments)).ConfigureAwait(false);
Expand Down Expand Up @@ -779,8 +779,8 @@ public RefitTestsTestNestedINestedGitHubApi(global::System.Net.Http.HttpClient c
/// <inheritdoc />
public async global::System.Threading.Tasks.Task<global::System.Net.Http.HttpResponseMessage> GetIndex()
{
var ______arguments = new object[] { };
var ______func = requestBuilder.BuildRestResultFuncForMethod(""GetIndex"", new global::System.Type[] { } );
var ______arguments = global::System.Array.Empty<object>();
var ______func = requestBuilder.BuildRestResultFuncForMethod(""GetIndex"", global::System.Array.Empty<global::System.Type>() );
try
{
return await ((global::System.Threading.Tasks.Task<global::System.Net.Http.HttpResponseMessage>)______func(this.Client, ______arguments)).ConfigureAwait(false);
Expand All @@ -794,8 +794,8 @@ public RefitTestsTestNestedINestedGitHubApi(global::System.Net.Http.HttpClient c
/// <inheritdoc />
public global::System.IObservable<string> GetIndexObservable()
{
var ______arguments = new object[] { };
var ______func = requestBuilder.BuildRestResultFuncForMethod(""GetIndexObservable"", new global::System.Type[] { } );
var ______arguments = global::System.Array.Empty<object>();
var ______func = requestBuilder.BuildRestResultFuncForMethod(""GetIndexObservable"", global::System.Array.Empty<global::System.Type>() );
try
{
return (global::System.IObservable<string>)______func(this.Client, ______arguments);
Expand All @@ -809,8 +809,8 @@ public RefitTestsTestNestedINestedGitHubApi(global::System.Net.Http.HttpClient c
/// <inheritdoc />
public async global::System.Threading.Tasks.Task NothingToSeeHere()
{
var ______arguments = new object[] { };
var ______func = requestBuilder.BuildRestResultFuncForMethod(""NothingToSeeHere"", new global::System.Type[] { } );
var ______arguments = global::System.Array.Empty<object>();
var ______func = requestBuilder.BuildRestResultFuncForMethod(""NothingToSeeHere"", global::System.Array.Empty<global::System.Type>() );
try
{
await ((global::System.Threading.Tasks.Task)______func(this.Client, ______arguments)).ConfigureAwait(false);
Expand Down Expand Up @@ -899,8 +899,8 @@ public RefitTestsTestNestedINestedGitHubApi(global::System.Net.Http.HttpClient c
/// <inheritdoc />
async global::System.Threading.Tasks.Task<global::System.Net.Http.HttpResponseMessage> global::Refit.Tests.TestNested.INestedGitHubApi.GetIndex()
{
var ______arguments = new object[] { };
var ______func = requestBuilder.BuildRestResultFuncForMethod(""GetIndex"", new global::System.Type[] { } );
var ______arguments = global::System.Array.Empty<object>();
var ______func = requestBuilder.BuildRestResultFuncForMethod(""GetIndex"", global::System.Array.Empty<global::System.Type>() );
try
{
return await ((global::System.Threading.Tasks.Task<global::System.Net.Http.HttpResponseMessage>)______func(this.Client, ______arguments)).ConfigureAwait(false);
Expand All @@ -914,8 +914,8 @@ public RefitTestsTestNestedINestedGitHubApi(global::System.Net.Http.HttpClient c
/// <inheritdoc />
global::System.IObservable<string> global::Refit.Tests.TestNested.INestedGitHubApi.GetIndexObservable()
{
var ______arguments = new object[] { };
var ______func = requestBuilder.BuildRestResultFuncForMethod(""GetIndexObservable"", new global::System.Type[] { } );
var ______arguments = global::System.Array.Empty<object>();
var ______func = requestBuilder.BuildRestResultFuncForMethod(""GetIndexObservable"", global::System.Array.Empty<global::System.Type>() );
try
{
return (global::System.IObservable<string>)______func(this.Client, ______arguments);
Expand All @@ -929,8 +929,8 @@ public RefitTestsTestNestedINestedGitHubApi(global::System.Net.Http.HttpClient c
/// <inheritdoc />
async global::System.Threading.Tasks.Task global::Refit.Tests.TestNested.INestedGitHubApi.NothingToSeeHere()
{
var ______arguments = new object[] { };
var ______func = requestBuilder.BuildRestResultFuncForMethod(""NothingToSeeHere"", new global::System.Type[] { } );
var ______arguments = global::System.Array.Empty<object>();
var ______func = requestBuilder.BuildRestResultFuncForMethod(""NothingToSeeHere"", global::System.Array.Empty<global::System.Type>() );
try
{
await ((global::System.Threading.Tasks.Task)______func(this.Client, ______arguments)).ConfigureAwait(false);
Expand Down Expand Up @@ -1060,8 +1060,8 @@ public IServiceWithoutNamespace(global::System.Net.Http.HttpClient client, globa
/// <inheritdoc />
public async global::System.Threading.Tasks.Task GetRoot()
{
var ______arguments = new object[] { };
var ______func = requestBuilder.BuildRestResultFuncForMethod(""GetRoot"", new global::System.Type[] { } );
var ______arguments = global::System.Array.Empty<object>();
var ______func = requestBuilder.BuildRestResultFuncForMethod(""GetRoot"", global::System.Array.Empty<global::System.Type>() );
try
{
await ((global::System.Threading.Tasks.Task)______func(this.Client, ______arguments)).ConfigureAwait(false);
Expand All @@ -1075,8 +1075,8 @@ public IServiceWithoutNamespace(global::System.Net.Http.HttpClient client, globa
/// <inheritdoc />
public async global::System.Threading.Tasks.Task PostRoot()
{
var ______arguments = new object[] { };
var ______func = requestBuilder.BuildRestResultFuncForMethod(""PostRoot"", new global::System.Type[] { } );
var ______arguments = global::System.Array.Empty<object>();
var ______func = requestBuilder.BuildRestResultFuncForMethod(""PostRoot"", global::System.Array.Empty<global::System.Type>() );
try
{
await ((global::System.Threading.Tasks.Task)______func(this.Client, ______arguments)).ConfigureAwait(false);
Expand All @@ -1090,8 +1090,8 @@ public IServiceWithoutNamespace(global::System.Net.Http.HttpClient client, globa
/// <inheritdoc />
async global::System.Threading.Tasks.Task global::IServiceWithoutNamespace.GetRoot()
{
var ______arguments = new object[] { };
var ______func = requestBuilder.BuildRestResultFuncForMethod(""GetRoot"", new global::System.Type[] { } );
var ______arguments = global::System.Array.Empty<object>();
var ______func = requestBuilder.BuildRestResultFuncForMethod(""GetRoot"", global::System.Array.Empty<global::System.Type>() );
try
{
await ((global::System.Threading.Tasks.Task)______func(this.Client, ______arguments)).ConfigureAwait(false);
Expand All @@ -1105,8 +1105,8 @@ public IServiceWithoutNamespace(global::System.Net.Http.HttpClient client, globa
/// <inheritdoc />
async global::System.Threading.Tasks.Task global::IServiceWithoutNamespace.PostRoot()
{
var ______arguments = new object[] { };
var ______func = requestBuilder.BuildRestResultFuncForMethod(""PostRoot"", new global::System.Type[] { } );
var ______arguments = global::System.Array.Empty<object>();
var ______func = requestBuilder.BuildRestResultFuncForMethod(""PostRoot"", global::System.Array.Empty<global::System.Type>() );
try
{
await ((global::System.Threading.Tasks.Task)______func(this.Client, ______arguments)).ConfigureAwait(false);
Expand Down

0 comments on commit 66edaaa

Please sign in to comment.