Skip to content

Commit 4fd70de

Browse files
authored
some static optimization (#832)
* no q builder if not params * make http method static val * header to readonly * add missing change * const to private * fix url in header
1 parent b5e1e9c commit 4fd70de

File tree

8 files changed

+37
-11
lines changed

8 files changed

+37
-11
lines changed

src/KubernetesClient.Basic/AbstractKubernetes.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@ namespace k8s
1010
{
1111
public abstract partial class AbstractKubernetes
1212
{
13+
private static class HttpMethods
14+
{
15+
public static readonly HttpMethod Delete = HttpMethod.Delete;
16+
public static readonly HttpMethod Get = HttpMethod.Get;
17+
public static readonly HttpMethod Head = HttpMethod.Head;
18+
public static readonly HttpMethod Options = HttpMethod.Options;
19+
public static readonly HttpMethod Post = HttpMethod.Post;
20+
public static readonly HttpMethod Put = HttpMethod.Put;
21+
public static readonly HttpMethod Trace = HttpMethod.Trace;
22+
public static readonly HttpMethod Patch = new HttpMethod("Patch");
23+
}
24+
1325
private sealed class QueryBuilder
1426
{
1527
private List<string> parameters = new List<string>();
@@ -101,7 +113,7 @@ private MediaTypeHeaderValue GetHeader(V1Patch body)
101113

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

104-
protected abstract HttpRequestMessage CreateRequest(string relativeUri, string method, IDictionary<string, IList<string>> customHeaders);
116+
protected abstract HttpRequestMessage CreateRequest(string relativeUri, HttpMethod method, IReadOnlyDictionary<string, IReadOnlyList<string>> customHeaders);
105117

106118
protected abstract Task<HttpResponseMessage> SendRequestRaw(string requestContent, HttpRequestMessage httpRequest, CancellationToken cancellationToken);
107119
}

src/KubernetesClient/Kubernetes.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,10 @@ protected override async Task<HttpOperationResponse<T>> CreateResultAsync<T>(Htt
9999
return result;
100100
}
101101

102-
protected override HttpRequestMessage CreateRequest(string relativeUri, string method, IDictionary<string, IList<string>> customHeaders)
102+
protected override HttpRequestMessage CreateRequest(string relativeUri, HttpMethod method, IReadOnlyDictionary<string, IReadOnlyList<string>> customHeaders)
103103
{
104104
var httpRequest = new HttpRequestMessage();
105-
httpRequest.Method = new HttpMethod(method);
105+
httpRequest.Method = method;
106106
httpRequest.RequestUri = new Uri(BaseUri, relativeUri);
107107
#if NETSTANDARD2_1_OR_GREATER
108108
httpRequest.Version = HttpVersion.Version20;

src/LibKubernetesGenerator/UtilHelper.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Generic;
2+
using System.Collections.ObjectModel;
23
using System.Linq;
34
using NSwag;
45
using Nustache.Core;
@@ -10,6 +11,7 @@ internal class UtilHelper : INustacheHelper
1011
public void RegisterHelper()
1112
{
1213
Helpers.Register(nameof(IfKindIs), IfKindIs);
14+
Helpers.Register(nameof(IfListNotEmpty), IfListNotEmpty);
1315
}
1416

1517
public static void IfKindIs(RenderContext context, IList<object> arguments, IDictionary<string, object> options,
@@ -34,5 +36,15 @@ public static void IfKindIs(RenderContext context, IList<object> arguments, IDic
3436
}
3537
}
3638
}
39+
40+
public static void IfListNotEmpty(RenderContext context, IList<object> arguments, IDictionary<string, object> options,
41+
RenderBlock fn, RenderBlock inverse)
42+
{
43+
var parameter = arguments?.FirstOrDefault() as ObservableCollection<OpenApiParameter>;
44+
if (parameter?.Any() == true)
45+
{
46+
fn(null);
47+
}
48+
}
3749
}
3850
}

src/LibKubernetesGenerator/templates/IKubernetes.cs.template

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// <auto-generated>
2-
// Code generated by https://github.com/kubernetes-client/csharp/tree/master/gen/KubernetesGenerator
2+
// Code generated by https://github.com/kubernetes-client/csharp/tree/master/src/LibKubernetesGenerator
33
// Changes may cause incorrect behavior and will be lost if the code is
44
// regenerated.
55
// </auto-generated>
@@ -38,7 +38,7 @@ namespace k8s
3838
{{#operation.parameters}}
3939
{{GetDotNetType .}} {{GetDotNetName . "true"}},
4040
{{/operation.parameters}}
41-
IDictionary<string, IList<string>> customHeaders = null,
41+
IReadOnlyDictionary<string, IReadOnlyList<string>> customHeaders = null,
4242
CancellationToken cancellationToken = default);
4343

4444
{{/.}}

src/LibKubernetesGenerator/templates/Kubernetes.cs.template

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// <auto-generated>
2-
// Code generated by https://github.com/kubernetes-client/csharp/tree/master/gen/KubernetesGenerator
2+
// Code generated by https://github.com/kubernetes-client/csharp/tree/master/src/LibKubernetesGenerator
33
// Changes may cause incorrect behavior and will be lost if the code is
44
// regenerated.
55
// </auto-generated>
@@ -23,7 +23,7 @@ namespace k8s
2323
{{#operation.parameters}}
2424
{{GetDotNetType .}} {{GetDotNetName . "true"}},
2525
{{/operation.parameters}}
26-
IDictionary<string, IList<string>> customHeaders = null,
26+
IReadOnlyDictionary<string, IReadOnlyList<string>> customHeaders = null,
2727
CancellationToken cancellationToken = default(CancellationToken))
2828
{
2929
var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
@@ -50,16 +50,18 @@ namespace k8s
5050
{{#IfGroupPathParamContainsGroup path}}
5151
url = url.Replace("apis//", "api/");
5252
{{/IfGroupPathParamContainsGroup}}
53+
{{#IfListNotEmpty operation.parameters}}
5354
var q = new QueryBuilder();
5455
{{#operation.parameters}}
5556
{{#IfKindIs . "query"}}
5657
q.Append("{{name}}", {{GetDotNetName name}});
5758
{{/IfKindIs . "query"}}
5859
{{/operation.parameters}}
5960
url += q.ToString();
61+
{{/IfListNotEmpty operation.parameters}}
6062

6163
// Create HTTP transport
62-
var httpRequest = CreateRequest(url, "{{Method}}", customHeaders);
64+
var httpRequest = CreateRequest(url, HttpMethods.{{Method}}, customHeaders);
6365
{{#IfParamContains operation "body"}}
6466
var httpResponse = await SendRequest(body, httpRequest, cancellationToken);
6567
{{/IfParamContains operation "body"}}

src/LibKubernetesGenerator/templates/KubernetesExtensions.cs.template

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// <auto-generated>
2-
// Code generated by https://github.com/kubernetes-client/csharp/tree/master/gen/KubernetesGenerator
2+
// Code generated by https://github.com/kubernetes-client/csharp/tree/master/src/LibKubernetesGenerator
33
// Changes may cause incorrect behavior and will be lost if the code is
44
// regenerated.
55
// </auto-generated>

src/LibKubernetesGenerator/templates/Model.cs.template

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// <auto-generated>
2-
// Code generated by https://github.com/kubernetes-client/csharp/tree/master/gen/KubernetesGenerator
2+
// Code generated by https://github.com/kubernetes-client/csharp/tree/master/src/LibKubernetesGenerator
33
// Changes may cause incorrect behavior and will be lost if the code is
44
// regenerated.
55
// </auto-generated>

src/LibKubernetesGenerator/templates/ModelExtensions.cs.template

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// <auto-generated>
2-
// Code generated by https://github.com/kubernetes-client/csharp/tree/master/gen/KubernetesGenerator
2+
// Code generated by https://github.com/kubernetes-client/csharp/tree/master/src/LibKubernetesGenerator
33
// Changes may cause incorrect behavior and will be lost if the code is regenerated.
44
// </auto-generated>
55
namespace k8s.Models

0 commit comments

Comments
 (0)