Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: run csharpier #1617

Merged
merged 1 commit into from
Nov 26, 2023
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
17 changes: 10 additions & 7 deletions InterfaceStubGenerator.Shared/ITypeSymbolExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,27 @@ public static IEnumerable<ITypeSymbol> GetBaseTypesAndThis(this ITypeSymbol? typ
// Determine if "type" inherits from "baseType", ignoring constructed types, optionally including interfaces,
// dealing only with original types.
public static bool InheritsFromOrEquals(
this ITypeSymbol type, ITypeSymbol baseType, bool includeInterfaces)
this ITypeSymbol type,
ITypeSymbol baseType,
bool includeInterfaces
)
{
if (!includeInterfaces)
{
return InheritsFromOrEquals(type, baseType);
}

return type.GetBaseTypesAndThis().Concat(type.AllInterfaces).Any(t => t.Equals(baseType, SymbolEqualityComparer.Default));
return type.GetBaseTypesAndThis()
.Concat(type.AllInterfaces)
.Any(t => t.Equals(baseType, SymbolEqualityComparer.Default));
}


// Determine if "type" inherits from "baseType", ignoring constructed types and interfaces, dealing
// only with original types.
public static bool InheritsFromOrEquals(
this ITypeSymbol type, ITypeSymbol baseType)
public static bool InheritsFromOrEquals(this ITypeSymbol type, ITypeSymbol baseType)
{
return type.GetBaseTypesAndThis().Any(t => t.Equals(baseType, SymbolEqualityComparer.Default));
return type.GetBaseTypesAndThis()
.Any(t => t.Equals(baseType, SymbolEqualityComparer.Default));
}

}
}
458 changes: 308 additions & 150 deletions InterfaceStubGenerator.Shared/InterfaceStubGenerator.cs

Large diffs are not rendered by default.

150 changes: 113 additions & 37 deletions Refit.Benchmarks/EndToEndBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,33 @@ public class EndToEndBenchmark
private const string Host = "https://github.com";
private SystemTextJsonContentSerializer systemTextJsonContentSerializer;
private NewtonsoftJsonContentSerializer newtonsoftJsonContentSerializer;
private readonly IDictionary<int, IEnumerable<User>> users = new Dictionary<int, IEnumerable<User>>();
private readonly IDictionary<SerializationStrategy, IDictionary<HttpStatusCode ,IGitHubService>> refitClient = new Dictionary<SerializationStrategy, IDictionary<HttpStatusCode ,IGitHubService>>
private readonly IDictionary<int, IEnumerable<User>> users =
new Dictionary<int, IEnumerable<User>>();
private readonly IDictionary<
SerializationStrategy,
IDictionary<HttpStatusCode, IGitHubService>
> refitClient = new Dictionary<
SerializationStrategy,
IDictionary<HttpStatusCode, IGitHubService>
>
{
{SerializationStrategy.SystemTextJson, new Dictionary<HttpStatusCode, IGitHubService>()},
{SerializationStrategy.NewtonsoftJson, new Dictionary<HttpStatusCode, IGitHubService>()}
{
SerializationStrategy.SystemTextJson,
new Dictionary<HttpStatusCode, IGitHubService>()
},
{
SerializationStrategy.NewtonsoftJson,
new Dictionary<HttpStatusCode, IGitHubService>()
}
};

private readonly IDictionary<HttpVerb, HttpMethod> httpMethod = new Dictionary<HttpVerb, HttpMethod>
private readonly IDictionary<HttpVerb, HttpMethod> httpMethod = new Dictionary<
HttpVerb,
HttpMethod
>
{
{HttpVerb.Get, HttpMethod.Get}, {HttpVerb.Post, HttpMethod.Post}
{ HttpVerb.Get, HttpMethod.Get },
{ HttpVerb.Post, HttpMethod.Post }
};

private const int TenUsers = 10;
Expand All @@ -41,26 +58,57 @@ public enum HttpVerb
[GlobalSetup]
public Task SetupAsync()
{

systemTextJsonContentSerializer = new SystemTextJsonContentSerializer();
refitClient[SerializationStrategy.SystemTextJson][HttpStatusCode.OK] = RestService.For<IGitHubService>(Host, new RefitSettings(systemTextJsonContentSerializer)
{
HttpMessageHandlerFactory = () => new StaticFileHttpResponseHandler("system-text-json-10-users.json", HttpStatusCode.OK)
});
refitClient[SerializationStrategy.SystemTextJson][HttpStatusCode.InternalServerError] = RestService.For<IGitHubService>(Host, new RefitSettings(systemTextJsonContentSerializer)
{
HttpMessageHandlerFactory = () => new StaticFileHttpResponseHandler("system-text-json-10-users.json", HttpStatusCode.InternalServerError)
});
refitClient[SerializationStrategy.SystemTextJson][HttpStatusCode.OK] =
RestService.For<IGitHubService>(
Host,
new RefitSettings(systemTextJsonContentSerializer)
{
HttpMessageHandlerFactory = () =>
new StaticFileHttpResponseHandler(
"system-text-json-10-users.json",
HttpStatusCode.OK
)
}
);
refitClient[SerializationStrategy.SystemTextJson][HttpStatusCode.InternalServerError] =
RestService.For<IGitHubService>(
Host,
new RefitSettings(systemTextJsonContentSerializer)
{
HttpMessageHandlerFactory = () =>
new StaticFileHttpResponseHandler(
"system-text-json-10-users.json",
HttpStatusCode.InternalServerError
)
}
);

newtonsoftJsonContentSerializer = new NewtonsoftJsonContentSerializer();
refitClient[SerializationStrategy.NewtonsoftJson][HttpStatusCode.OK] = RestService.For<IGitHubService>(Host, new RefitSettings(newtonsoftJsonContentSerializer)
{
HttpMessageHandlerFactory = () => new StaticFileHttpResponseHandler("newtonsoft-json-10-users.json", System.Net.HttpStatusCode.OK)
});
refitClient[SerializationStrategy.NewtonsoftJson][HttpStatusCode.InternalServerError] = RestService.For<IGitHubService>(Host, new RefitSettings(newtonsoftJsonContentSerializer)
{
HttpMessageHandlerFactory = () => new StaticFileHttpResponseHandler("newtonsoft-json-10-users.json", System.Net.HttpStatusCode.InternalServerError)
});
refitClient[SerializationStrategy.NewtonsoftJson][HttpStatusCode.OK] =
RestService.For<IGitHubService>(
Host,
new RefitSettings(newtonsoftJsonContentSerializer)
{
HttpMessageHandlerFactory = () =>
new StaticFileHttpResponseHandler(
"newtonsoft-json-10-users.json",
System.Net.HttpStatusCode.OK
)
}
);
refitClient[SerializationStrategy.NewtonsoftJson][HttpStatusCode.InternalServerError] =
RestService.For<IGitHubService>(
Host,
new RefitSettings(newtonsoftJsonContentSerializer)
{
HttpMessageHandlerFactory = () =>
new StaticFileHttpResponseHandler(
"newtonsoft-json-10-users.json",
System.Net.HttpStatusCode.InternalServerError
)
}
);

users[TenUsers] = autoFixture.CreateMany<User>(TenUsers);

Expand Down Expand Up @@ -94,7 +142,9 @@ public async Task Task_Async()
await refitClient[Serializer][HttpStatusCode].GetUsersTaskAsync();
break;
case HttpVerb.Post:
await refitClient[Serializer][HttpStatusCode].PostUsersTaskAsync(users[ModelCount]);
await refitClient[Serializer][HttpStatusCode].PostUsersTaskAsync(
users[ModelCount]
);
break;
default:
throw new ArgumentOutOfRangeException(nameof(Verb));
Expand All @@ -114,9 +164,13 @@ public async Task<string> TaskString_Async()
switch (Verb)
{
case HttpVerb.Get:
return await refitClient[Serializer][HttpStatusCode].GetUsersTaskStringAsync();
return await refitClient[Serializer][
HttpStatusCode
].GetUsersTaskStringAsync();
case HttpVerb.Post:
return await refitClient[Serializer][HttpStatusCode].PostUsersTaskStringAsync(users[ModelCount]);
return await refitClient[Serializer][
HttpStatusCode
].PostUsersTaskStringAsync(users[ModelCount]);
default:
throw new ArgumentOutOfRangeException(nameof(Verb));
}
Expand All @@ -137,9 +191,13 @@ public async Task<Stream> TaskStream_Async()
switch (Verb)
{
case HttpVerb.Get:
return await refitClient[Serializer][HttpStatusCode].GetUsersTaskStreamAsync();
return await refitClient[Serializer][
HttpStatusCode
].GetUsersTaskStreamAsync();
case HttpVerb.Post:
return await refitClient[Serializer][HttpStatusCode].PostUsersTaskStreamAsync(users[ModelCount]);
return await refitClient[Serializer][
HttpStatusCode
].PostUsersTaskStreamAsync(users[ModelCount]);
default:
throw new ArgumentOutOfRangeException(nameof(Verb));
}
Expand All @@ -160,9 +218,13 @@ public async Task<HttpContent> TaskHttpContent_Async()
switch (Verb)
{
case HttpVerb.Get:
return await refitClient[Serializer][HttpStatusCode].GetUsersTaskHttpContentAsync();
return await refitClient[Serializer][
HttpStatusCode
].GetUsersTaskHttpContentAsync();
case HttpVerb.Post:
return await refitClient[Serializer][HttpStatusCode].PostUsersTaskHttpContentAsync(users[ModelCount]);
return await refitClient[Serializer][
HttpStatusCode
].PostUsersTaskHttpContentAsync(users[ModelCount]);
default:
throw new ArgumentOutOfRangeException(nameof(Verb));
}
Expand All @@ -181,9 +243,13 @@ public async Task<HttpResponseMessage> TaskHttpResponseMessage_Async()
switch (Verb)
{
case HttpVerb.Get:
return await refitClient[Serializer][HttpStatusCode].GetUsersTaskHttpResponseMessageAsync();
return await refitClient[Serializer][
HttpStatusCode
].GetUsersTaskHttpResponseMessageAsync();
case HttpVerb.Post:
return await refitClient[Serializer][HttpStatusCode].PostUsersTaskHttpResponseMessageAsync(users[ModelCount]);
return await refitClient[Serializer][
HttpStatusCode
].PostUsersTaskHttpResponseMessageAsync(users[ModelCount]);
default:
throw new ArgumentOutOfRangeException(nameof(Verb));
}
Expand All @@ -195,9 +261,13 @@ public IObservable<HttpResponseMessage> ObservableHttpResponseMessage()
switch (Verb)
{
case HttpVerb.Get:
return refitClient[Serializer][HttpStatusCode].GetUsersObservableHttpResponseMessage();
return refitClient[Serializer][
HttpStatusCode
].GetUsersObservableHttpResponseMessage();
case HttpVerb.Post:
return refitClient[Serializer][HttpStatusCode].PostUsersObservableHttpResponseMessage(users[ModelCount]);
return refitClient[Serializer][
HttpStatusCode
].PostUsersObservableHttpResponseMessage(users[ModelCount]);
default:
throw new ArgumentOutOfRangeException(nameof(Verb));
}
Expand All @@ -213,7 +283,9 @@ public async Task<List<User>> TaskT_Async()
case HttpVerb.Get:
return await refitClient[Serializer][HttpStatusCode].GetUsersTaskTAsync();
case HttpVerb.Post:
return await refitClient[Serializer][HttpStatusCode].PostUsersTaskTAsync(users[ModelCount]);
return await refitClient[Serializer][HttpStatusCode].PostUsersTaskTAsync(
users[ModelCount]
);
default:
throw new ArgumentOutOfRangeException(nameof(Verb));
}
Expand All @@ -232,9 +304,13 @@ public async Task<ApiResponse<List<User>>> TaskApiResponseT_Async()
switch (Verb)
{
case HttpVerb.Get:
return await refitClient[Serializer][HttpStatusCode].GetUsersTaskApiResponseTAsync();
return await refitClient[Serializer][
HttpStatusCode
].GetUsersTaskApiResponseTAsync();
case HttpVerb.Post:
return await refitClient[Serializer][HttpStatusCode].PostUsersTaskApiResponseTAsync(users[ModelCount]);
return await refitClient[Serializer][
HttpStatusCode
].PostUsersTaskApiResponseTAsync(users[ModelCount]);
default:
throw new ArgumentOutOfRangeException(nameof(Verb));
}
Expand Down
22 changes: 17 additions & 5 deletions Refit.Benchmarks/IGitHubService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,50 +5,64 @@ public interface IGitHubService
//Task - throws
[Get("/users")]
public Task GetUsersTaskAsync();

[Post("/users")]
public Task PostUsersTaskAsync([Body] IEnumerable<User> users);

//Task<string> - throws
[Get("/users")]
public Task<string> GetUsersTaskStringAsync();

[Post("/users")]
public Task<string> PostUsersTaskStringAsync([Body] IEnumerable<User> users);

//Task<Stream> - throws
[Get("/users")]
public Task<Stream> GetUsersTaskStreamAsync();

[Post("/users")]
public Task<Stream> PostUsersTaskStreamAsync([Body] IEnumerable<User> users);

//Task<HttpContent> - throws
[Get("/users")]
public Task<HttpContent> GetUsersTaskHttpContentAsync();

[Post("/users")]
public Task<HttpContent> PostUsersTaskHttpContentAsync([Body] IEnumerable<User> users);

//Task<HttpResponseMessage>
[Get("/users")]
public Task<HttpResponseMessage> GetUsersTaskHttpResponseMessageAsync();

[Post("/users")]
public Task<HttpResponseMessage> PostUsersTaskHttpResponseMessageAsync([Body] IEnumerable<User> users);
public Task<HttpResponseMessage> PostUsersTaskHttpResponseMessageAsync(
[Body] IEnumerable<User> users
);

//IObservable<HttpResponseMessage>
[Get("/users")]
public IObservable<HttpResponseMessage> GetUsersObservableHttpResponseMessage();

[Post("/users")]
public IObservable<HttpResponseMessage> PostUsersObservableHttpResponseMessage([Body] IEnumerable<User> users);
public IObservable<HttpResponseMessage> PostUsersObservableHttpResponseMessage(
[Body] IEnumerable<User> users
);

//Task<<T>> - throws
[Get("/users")]
public Task<List<User>> GetUsersTaskTAsync();

[Post("/users")]
public Task<List<User>> PostUsersTaskTAsync([Body] IEnumerable<User> users);

//Task<ApiResponse<T>>
[Get("/users")]
public Task<ApiResponse<List<User>>> GetUsersTaskApiResponseTAsync();

[Post("/users")]
public Task<ApiResponse<List<User>>> PostUsersTaskApiResponseTAsync([Body] IEnumerable<User> users);
public Task<ApiResponse<List<User>>> PostUsersTaskApiResponseTAsync(
[Body] IEnumerable<User> users
);
}

public class User
Expand All @@ -61,5 +75,3 @@ public class User
public string Url { get; set; }
}
}


21 changes: 13 additions & 8 deletions Refit.Benchmarks/StaticFileHttpResponseHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,22 @@ public StaticFileHttpResponseHandler(string fileName, HttpStatusCode responseCod
throw new ArgumentNullException(nameof(fileName));

responsePayload = File.ReadAllText(fileName);
; this.responseCode = responseCode;
;
this.responseCode = responseCode;
}

protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
CancellationToken cancellationToken)
protected override Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request,
CancellationToken cancellationToken
)
{
return Task.FromResult(new HttpResponseMessage(responseCode)
{
RequestMessage = request,
Content = new StringContent(responsePayload)
});
return Task.FromResult(
new HttpResponseMessage(responseCode)
{
RequestMessage = request,
Content = new StringContent(responsePayload)
}
);
}
}
}
Loading