Skip to content

Commit

Permalink
some static optimization (#832)
Browse files Browse the repository at this point in the history
* no q builder if not params

* make http method static val

* header to readonly

* add missing change

* const to private

* fix url in header
  • Loading branch information
tg123 committed Apr 18, 2022
1 parent b5e1e9c commit 4fd70de
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 11 deletions.
14 changes: 13 additions & 1 deletion src/KubernetesClient.Basic/AbstractKubernetes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ namespace k8s
{
public abstract partial class AbstractKubernetes
{
private static class HttpMethods
{
public static readonly HttpMethod Delete = HttpMethod.Delete;
public static readonly HttpMethod Get = HttpMethod.Get;
public static readonly HttpMethod Head = HttpMethod.Head;
public static readonly HttpMethod Options = HttpMethod.Options;
public static readonly HttpMethod Post = HttpMethod.Post;
public static readonly HttpMethod Put = HttpMethod.Put;
public static readonly HttpMethod Trace = HttpMethod.Trace;
public static readonly HttpMethod Patch = new HttpMethod("Patch");
}

private sealed class QueryBuilder
{
private List<string> parameters = new List<string>();
Expand Down Expand Up @@ -101,7 +113,7 @@ private MediaTypeHeaderValue GetHeader(V1Patch body)

protected abstract Task<HttpOperationResponse<T>> CreateResultAsync<T>(HttpRequestMessage httpRequest, HttpResponseMessage httpResponse, bool? watch, CancellationToken cancellationToken);

protected abstract HttpRequestMessage CreateRequest(string relativeUri, string method, IDictionary<string, IList<string>> customHeaders);
protected abstract HttpRequestMessage CreateRequest(string relativeUri, HttpMethod method, IReadOnlyDictionary<string, IReadOnlyList<string>> customHeaders);

protected abstract Task<HttpResponseMessage> SendRequestRaw(string requestContent, HttpRequestMessage httpRequest, CancellationToken cancellationToken);
}
Expand Down
4 changes: 2 additions & 2 deletions src/KubernetesClient/Kubernetes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,10 @@ protected override async Task<HttpOperationResponse<T>> CreateResultAsync<T>(Htt
return result;
}

protected override HttpRequestMessage CreateRequest(string relativeUri, string method, IDictionary<string, IList<string>> customHeaders)
protected override HttpRequestMessage CreateRequest(string relativeUri, HttpMethod method, IReadOnlyDictionary<string, IReadOnlyList<string>> customHeaders)
{
var httpRequest = new HttpRequestMessage();
httpRequest.Method = new HttpMethod(method);
httpRequest.Method = method;
httpRequest.RequestUri = new Uri(BaseUri, relativeUri);
#if NETSTANDARD2_1_OR_GREATER
httpRequest.Version = HttpVersion.Version20;
Expand Down
12 changes: 12 additions & 0 deletions src/LibKubernetesGenerator/UtilHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using NSwag;
using Nustache.Core;
Expand All @@ -10,6 +11,7 @@ internal class UtilHelper : INustacheHelper
public void RegisterHelper()
{
Helpers.Register(nameof(IfKindIs), IfKindIs);
Helpers.Register(nameof(IfListNotEmpty), IfListNotEmpty);
}

public static void IfKindIs(RenderContext context, IList<object> arguments, IDictionary<string, object> options,
Expand All @@ -34,5 +36,15 @@ public static void IfKindIs(RenderContext context, IList<object> arguments, IDic
}
}
}

public static void IfListNotEmpty(RenderContext context, IList<object> arguments, IDictionary<string, object> options,
RenderBlock fn, RenderBlock inverse)
{
var parameter = arguments?.FirstOrDefault() as ObservableCollection<OpenApiParameter>;
if (parameter?.Any() == true)
{
fn(null);
}
}
}
}
4 changes: 2 additions & 2 deletions src/LibKubernetesGenerator/templates/IKubernetes.cs.template
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// <auto-generated>
// Code generated by https://github.com/kubernetes-client/csharp/tree/master/gen/KubernetesGenerator
// Code generated by https://github.com/kubernetes-client/csharp/tree/master/src/LibKubernetesGenerator
// Changes may cause incorrect behavior and will be lost if the code is
// regenerated.
// </auto-generated>
Expand Down Expand Up @@ -38,7 +38,7 @@ namespace k8s
{{#operation.parameters}}
{{GetDotNetType .}} {{GetDotNetName . "true"}},
{{/operation.parameters}}
IDictionary<string, IList<string>> customHeaders = null,
IReadOnlyDictionary<string, IReadOnlyList<string>> customHeaders = null,
CancellationToken cancellationToken = default);

{{/.}}
Expand Down
8 changes: 5 additions & 3 deletions src/LibKubernetesGenerator/templates/Kubernetes.cs.template
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// <auto-generated>
// Code generated by https://github.com/kubernetes-client/csharp/tree/master/gen/KubernetesGenerator
// Code generated by https://github.com/kubernetes-client/csharp/tree/master/src/LibKubernetesGenerator
// Changes may cause incorrect behavior and will be lost if the code is
// regenerated.
// </auto-generated>
Expand All @@ -23,7 +23,7 @@ namespace k8s
{{#operation.parameters}}
{{GetDotNetType .}} {{GetDotNetName . "true"}},
{{/operation.parameters}}
IDictionary<string, IList<string>> customHeaders = null,
IReadOnlyDictionary<string, IReadOnlyList<string>> customHeaders = null,
CancellationToken cancellationToken = default(CancellationToken))
{
var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
Expand All @@ -50,16 +50,18 @@ namespace k8s
{{#IfGroupPathParamContainsGroup path}}
url = url.Replace("apis//", "api/");
{{/IfGroupPathParamContainsGroup}}
{{#IfListNotEmpty operation.parameters}}
var q = new QueryBuilder();
{{#operation.parameters}}
{{#IfKindIs . "query"}}
q.Append("{{name}}", {{GetDotNetName name}});
{{/IfKindIs . "query"}}
{{/operation.parameters}}
url += q.ToString();
{{/IfListNotEmpty operation.parameters}}

// Create HTTP transport
var httpRequest = CreateRequest(url, "{{Method}}", customHeaders);
var httpRequest = CreateRequest(url, HttpMethods.{{Method}}, customHeaders);
{{#IfParamContains operation "body"}}
var httpResponse = await SendRequest(body, httpRequest, cancellationToken);
{{/IfParamContains operation "body"}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// <auto-generated>
// Code generated by https://github.com/kubernetes-client/csharp/tree/master/gen/KubernetesGenerator
// Code generated by https://github.com/kubernetes-client/csharp/tree/master/src/LibKubernetesGenerator
// Changes may cause incorrect behavior and will be lost if the code is
// regenerated.
// </auto-generated>
Expand Down
2 changes: 1 addition & 1 deletion src/LibKubernetesGenerator/templates/Model.cs.template
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// <auto-generated>
// Code generated by https://github.com/kubernetes-client/csharp/tree/master/gen/KubernetesGenerator
// Code generated by https://github.com/kubernetes-client/csharp/tree/master/src/LibKubernetesGenerator
// Changes may cause incorrect behavior and will be lost if the code is
// regenerated.
// </auto-generated>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// <auto-generated>
// Code generated by https://github.com/kubernetes-client/csharp/tree/master/gen/KubernetesGenerator
// Code generated by https://github.com/kubernetes-client/csharp/tree/master/src/LibKubernetesGenerator
// Changes may cause incorrect behavior and will be lost if the code is regenerated.
// </auto-generated>
namespace k8s.Models
Expand Down

0 comments on commit 4fd70de

Please sign in to comment.