Refactor LibKubernetesGenerator code structure (#1546)

* Refactor LibKubernetesGenerator code structure

* clean up comment out code

* No more locks

* clean up dep

* Update dependency paths in LibKubernetesGenerator.target
This commit is contained in:
Boshi Lian
2024-04-15 10:43:05 -07:00
committed by GitHub
parent de7ecf1da9
commit 3dae1cf299
24 changed files with 489 additions and 696 deletions

View File

@@ -8,6 +8,13 @@ namespace LibKubernetesGenerator
{
internal class ApiGenerator
{
private readonly ScriptObjectFactory scriptObjectFactory;
public ApiGenerator(ScriptObjectFactory scriptObjectFactory)
{
this.scriptObjectFactory = scriptObjectFactory;
}
public void Generate(OpenApiDocument swagger, IncrementalGeneratorPostInitializationContext context)
{
var data = swagger.Operations
@@ -42,6 +49,8 @@ namespace LibKubernetesGenerator
})
.ToArray();
var sc = scriptObjectFactory.CreateScriptObject();
var groups = new List<string>();
foreach (var grouped in data.GroupBy(d => d.Operation.Tags.First()))
@@ -50,14 +59,20 @@ namespace LibKubernetesGenerator
groups.Add(name);
var apis = grouped.ToArray();
var gctx = new { name, apis };
context.RenderToContext($"IOperations.cs.template", gctx, $"I{name}Operations.g.cs");
context.RenderToContext("Operations.cs.template", gctx, $"{name}Operations.g.cs");
context.RenderToContext("OperationsExtensions.cs.template", gctx, $"{name}OperationsExtensions.g.cs");
sc.SetValue("name", name, true);
sc.SetValue("apis", apis, true);
context.RenderToContext($"IOperations.cs.template", sc, $"I{name}Operations.g.cs");
context.RenderToContext("Operations.cs.template", sc, $"{name}Operations.g.cs");
context.RenderToContext("OperationsExtensions.cs.template", sc, $"{name}OperationsExtensions.g.cs");
}
context.RenderToContext($"IBasicKubernetes.cs.template", groups, $"IBasicKubernetes.g.cs");
context.RenderToContext($"AbstractKubernetes.cs.template", groups, $"AbstractKubernetes.g.cs");
sc = scriptObjectFactory.CreateScriptObject();
sc.SetValue("groups", groups, true);
context.RenderToContext($"IBasicKubernetes.cs.template", sc, $"IBasicKubernetes.g.cs");
context.RenderToContext($"AbstractKubernetes.cs.template", sc, $"AbstractKubernetes.g.cs");
}
}
}

View File

@@ -1,13 +1,14 @@
using CaseExtensions;
using NJsonSchema;
using NSwag;
using Nustache.Core;
using Scriban.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
namespace LibKubernetesGenerator
{
internal class ClassNameHelper : INustacheHelper
internal class ClassNameHelper : IScriptObjectHelper
{
private readonly Dictionary<string, string> classNameMap;
private readonly Dictionary<JsonSchema, string> schemaToNameMapCooked;
@@ -18,9 +19,10 @@ namespace LibKubernetesGenerator
schemaToNameMapCooked = GenerateSchemaToNameMapCooked(swagger);
}
public void RegisterHelper()
public void RegisterHelper(ScriptObject scriptObject)
{
Helpers.Register(nameof(GetClassName), GetClassName);
scriptObject.Import(nameof(GetClassName), new Func<JsonSchema, string>(GetClassNameForSchemaDefinition));
}
private static Dictionary<JsonSchema, string> GenerateSchemaToNameMapCooked(OpenApiDocument swagger)
@@ -50,27 +52,7 @@ namespace LibKubernetesGenerator
return map;
}
public void GetClassName(RenderContext context, IList<object> arguments, IDictionary<string, object> options,
RenderBlock fn, RenderBlock inverse)
{
if (arguments != null && arguments.Count > 0 && arguments[0] != null && arguments[0] is OpenApiOperation)
{
context.Write(GetClassName(arguments[0] as OpenApiOperation));
}
else if (arguments != null && arguments.Count > 0 && arguments[0] != null && arguments[0] is JsonSchema)
{
context.Write(GetClassNameForSchemaDefinition(arguments[0] as JsonSchema));
}
}
public string GetClassName(OpenApiOperation operation)
{
var groupVersionKind =
(Dictionary<string, object>)operation.ExtensionData["x-kubernetes-group-version-kind"];
return GetClassName(groupVersionKind);
}
public string GetClassName(Dictionary<string, object> groupVersionKind)
private string GetClassName(Dictionary<string, object> groupVersionKind)
{
var group = (string)groupVersionKind["group"];
var kind = (string)groupVersionKind["kind"];
@@ -98,10 +80,5 @@ namespace LibKubernetesGenerator
return schemaToNameMapCooked[definition];
}
private static Dictionary<JsonSchema, string> InitSchemaToNameCooked(OpenApiDocument swagger)
{
return swagger.Definitions.ToDictionary(x => x.Value, x => x.Key.Replace(".", "").ToPascalCase());
}
}
}

View File

@@ -1,14 +1,15 @@
using CaseExtensions;
using NJsonSchema;
using NSwag;
using Nustache.Core;
using Scriban.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace LibKubernetesGenerator
{
internal class GeneralNameHelper : INustacheHelper
internal class GeneralNameHelper : IScriptObjectHelper
{
private readonly ClassNameHelper classNameHelper;
@@ -17,20 +18,12 @@ namespace LibKubernetesGenerator
this.classNameHelper = classNameHelper;
}
public void RegisterHelper()
public void RegisterHelper(ScriptObject scriptObject)
{
Helpers.Register(nameof(GetInterfaceName), GetInterfaceName);
Helpers.Register(nameof(GetMethodName), GetMethodName);
Helpers.Register(nameof(GetDotNetName), GetDotNetName);
}
public void GetInterfaceName(RenderContext context, IList<object> arguments,
IDictionary<string, object> options, RenderBlock fn, RenderBlock inverse)
{
if (arguments != null && arguments.Count > 0 && arguments[0] != null && arguments[0] is JsonSchema)
{
context.Write(GetInterfaceName(arguments[0] as JsonSchema));
}
scriptObject.Import(nameof(GetInterfaceName), new Func<JsonSchema, string>(GetInterfaceName));
scriptObject.Import(nameof(GetMethodName), new Func<OpenApiOperation, string, string>(GetMethodName));
scriptObject.Import(nameof(GetDotNetName), new Func<string, string, string>(GetDotNetName));
scriptObject.Import(nameof(GetDotNetNameOpenApiParameter), new Func<OpenApiParameter, string, string>(GetDotNetNameOpenApiParameter));
}
private string GetInterfaceName(JsonSchema definition)
@@ -68,44 +61,16 @@ namespace LibKubernetesGenerator
return string.Join(", ", interfaces);
}
public void GetMethodName(RenderContext context, IList<object> arguments, IDictionary<string, object> options,
RenderBlock fn, RenderBlock inverse)
public string GetDotNetNameOpenApiParameter(OpenApiParameter parameter, string init)
{
if (arguments != null && arguments.Count > 0 && arguments[0] != null && arguments[0] is OpenApiOperation)
var name = GetDotNetName(parameter.Name);
if (init == "true" && !parameter.IsRequired)
{
string suffix = null;
if (arguments.Count > 1)
{
suffix = arguments[1] as string;
name += " = null";
}
context.Write(GetMethodName(arguments[0] as OpenApiOperation, suffix));
}
}
public void GetDotNetName(RenderContext context, IList<object> arguments, IDictionary<string, object> options,
RenderBlock fn, RenderBlock inverse)
{
if (arguments != null && arguments.Count > 0 && arguments[0] != null && arguments[0] is OpenApiParameter)
{
var parameter = arguments[0] as OpenApiParameter;
context.Write(GetDotNetName(parameter.Name));
if (arguments.Count > 1 && (arguments[1] as string) == "true" && !parameter.IsRequired)
{
context.Write(" = null");
}
}
else if (arguments != null && arguments.Count > 0 && arguments[0] != null && arguments[0] is string)
{
var style = "parameter";
if (arguments.Count > 1)
{
style = arguments[1] as string;
}
context.Write(GetDotNetName((string)arguments[0], style));
}
return name;
}
public string GetDotNetName(string jsonName, string style = "parameter")

View File

@@ -1,16 +1,24 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;
using Nustache.Core;
using Scriban;
using Scriban.Runtime;
using System.Text;
namespace LibKubernetesGenerator
{
internal static class GeneratorExecutionContextExt
{
public static void RenderToContext(this IncrementalGeneratorPostInitializationContext context, string templatefile, object data, string generatedfile)
public static void RenderToContext(this IncrementalGeneratorPostInitializationContext context, string templatefile, ScriptObject sc, string generatedfile)
{
var template = EmbedResource.GetResource(templatefile);
var generated = Render.StringToString(template, data);
var tc = new TemplateContext();
tc.PushGlobal(sc);
context.RenderToContext(templatefile, tc, generatedfile);
}
public static void RenderToContext(this IncrementalGeneratorPostInitializationContext context, string templatefile, TemplateContext tc, string generatedfile)
{
var template = Template.Parse(EmbedResource.GetResource(templatefile));
var generated = template.Render(tc);
context.AddSource(generatedfile, SourceText.From(generated, Encoding.UTF8));
}
}

View File

@@ -1,7 +0,0 @@
namespace LibKubernetesGenerator
{
public interface INustacheHelper
{
void RegisterHelper();
}
}

View File

@@ -0,0 +1,8 @@
using Scriban.Runtime;
namespace LibKubernetesGenerator;
internal interface IScriptObjectHelper
{
void RegisterHelper(ScriptObject scriptObject);
}

View File

@@ -1,38 +1,22 @@
using Autofac;
using Microsoft.CodeAnalysis;
using NSwag;
using Nustache.Core;
#if GENERATE_AUTOMAPPER
using System.Collections.Generic;
using System;
using System.IO;
using System.Linq;
#endif
using System.Collections.Generic;
using System.Reflection;
namespace LibKubernetesGenerator
{
[Generator]
public class KubernetesClientSourceGenerator : IIncrementalGenerator
{
private static readonly object Execlock = new object();
private static (OpenApiDocument, IContainer) BuildContainer()
{
var swagger = OpenApiDocument.FromJsonAsync(EmbedResource.GetResource("swagger.json")).GetAwaiter().GetResult();
var container = BuildContainer(swagger);
// TODO move to Handlebars.Net
// here is to clean up the custom helpers in static Nustache.Core.Helpers
{
var ch = typeof(Helpers).GetField("CustomHelpers", BindingFlags.Static | BindingFlags.NonPublic);
((Dictionary<string, Helper>)ch.GetValue(null)).Clear();
}
foreach (var helper in container.Resolve<IEnumerable<INustacheHelper>>())
{
helper.RegisterHelper();
}
return (swagger, container);
}
@@ -77,6 +61,9 @@ namespace LibKubernetesGenerator
.AsImplementedInterfaces()
;
builder.RegisterType<ScriptObjectFactory>()
;
builder.RegisterType<ModelExtGenerator>();
builder.RegisterType<ModelGenerator>();
builder.RegisterType<ApiGenerator>();
@@ -91,8 +78,6 @@ namespace LibKubernetesGenerator
{
#if GENERATE_BASIC
generatorContext.RegisterPostInitializationOutput(ctx =>
{
lock (Execlock)
{
var (swagger, container) = BuildContainer();
@@ -101,22 +86,16 @@ namespace LibKubernetesGenerator
container.Resolve<ModelGenerator>().Generate(swagger, ctx);
container.Resolve<ModelExtGenerator>().Generate(swagger, ctx);
container.Resolve<VersionConverterStubGenerator>().Generate(swagger, ctx);
container.Resolve<ApiGenerator>().Generate(swagger, ctx);
}
});
#endif
#if GENERATE_AUTOMAPPER
var automappersrc = generatorContext.CompilationProvider.Select((c, _) => c.SyntaxTrees.First(s => PathSuffixMath(s.FilePath, "AutoMapper/VersionConverter.cs")));
generatorContext.RegisterSourceOutput(automappersrc, (ctx, srctree) =>
{
lock (Execlock)
{
var (swagger, container) = BuildContainer();
container.Resolve<VersionConverterAutoMapperGenerator>().Generate(swagger, ctx, srctree);
}
});
#endif
}

View File

@@ -1,8 +1,8 @@
<Project>
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<NoWarn>CA1812</NoWarn>
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
<PackageScribanIncludeSource>true</PackageScribanIncludeSource>
</PropertyGroup>
<ItemGroup>
@@ -16,15 +16,22 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Autofac" Version="8.0.0" GeneratePathProperty="true" PrivateAssets="all" />
<!-- Scriban No Dependency -->
<PackageReference Include="Scriban" Version="5.9.1" IncludeAssets="Build"/>
<!-- CaseExtensions No Dependency -->
<PackageReference Include="CaseExtensions" Version="1.1.0" GeneratePathProperty="true" PrivateAssets="all" />
<PackageReference Include="NSwag.Core" Version="13.20.0" GeneratePathProperty="true" PrivateAssets="all" />
<PackageReference Include="Nustache" Version="1.16.0.10" GeneratePathProperty="true" PrivateAssets="all" NoWarn="NU1701" />
<PackageReference Include="NJsonSchema" Version="10.9.0" GeneratePathProperty="true" PrivateAssets="all" />
<!-- Autofac -->
<PackageReference Include="Autofac" Version="8.0.0" GeneratePathProperty="true" PrivateAssets="all" />
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="8.0.0" GeneratePathProperty="true" PrivateAssets="all" />
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="8.0.0" GeneratePathProperty="true" PrivateAssets="all" />
<!-- NSwag -->
<PackageReference Include="NSwag.Core" Version="13.20.0" GeneratePathProperty="true" PrivateAssets="all" />
<PackageReference Include="NJsonSchema" Version="10.9.0" GeneratePathProperty="true" PrivateAssets="all" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" GeneratePathProperty="true" PrivateAssets="all" />
<PackageReference Include="Namotion.Reflection" Version="3.0.1" GeneratePathProperty="true" PrivateAssets="all" />
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="8.0.0" GeneratePathProperty="true" PrivateAssets="all" />
</ItemGroup>
<PropertyGroup>
@@ -33,15 +40,16 @@
<Target Name="GetDependencyTargetPaths">
<ItemGroup>
<TargetPathWithTargetPlatformMoniker Include="$(PKGAutofac)\lib\netstandard2.0\Autofac.dll" IncludeRuntimeDependency="false" />
<TargetPathWithTargetPlatformMoniker Include="$(PKGCaseExtensions)\lib\netstandard2.0\CaseExtensions.dll" IncludeRuntimeDependency="false" />
<TargetPathWithTargetPlatformMoniker Include="$(PKGNSwag_Core)\lib\netstandard2.0\NSwag.Core.dll" IncludeRuntimeDependency="false" />
<TargetPathWithTargetPlatformMoniker Include="$(PKGNustache)\lib\net20\Nustache.Core.dll" IncludeRuntimeDependency="false" />
<TargetPathWithTargetPlatformMoniker Include="$(PKGNJsonSchema)\lib\netstandard2.0\NJsonSchema.dll" IncludeRuntimeDependency="false" />
<TargetPathWithTargetPlatformMoniker Include="$(PKGAutofac)\lib\netstandard2.0\Autofac.dll" IncludeRuntimeDependency="false" />
<TargetPathWithTargetPlatformMoniker Include="$(PKGMicrosoft_Bcl_AsyncInterfaces)\lib\netstandard2.0\Microsoft.Bcl.AsyncInterfaces.dll" IncludeRuntimeDependency="false" />
<TargetPathWithTargetPlatformMoniker Include="$(PKGSystem_Diagnostics_DiagnosticSource)\lib\netstandard2.0\System.Diagnostics.DiagnosticSource.dll" IncludeRuntimeDependency="false" />
<TargetPathWithTargetPlatformMoniker Include="$(PKGNSwag_Core)\lib\netstandard2.0\NSwag.Core.dll" IncludeRuntimeDependency="false" />
<TargetPathWithTargetPlatformMoniker Include="$(PKGNJsonSchema)\lib\netstandard2.0\NJsonSchema.dll" IncludeRuntimeDependency="false" />
<TargetPathWithTargetPlatformMoniker Include="$(PKGNewtonsoft_Json)\lib\netstandard1.0\Newtonsoft.Json.dll" IncludeRuntimeDependency="false" />
<TargetPathWithTargetPlatformMoniker Include="$(PKGNamotion_Reflection)\lib\netstandard2.0\Namotion.Reflection.dll" IncludeRuntimeDependency="false" />
<TargetPathWithTargetPlatformMoniker Include="$(PKGSystem_Diagnostics_DiagnosticSource)\lib\netstandard2.0\System.Diagnostics.DiagnosticSource.dll" IncludeRuntimeDependency="false" />
</ItemGroup>
</Target>

View File

@@ -1,28 +1,19 @@
using NJsonSchema;
using NSwag;
using Nustache.Core;
using Scriban.Runtime;
using System;
using System.Collections.Generic;
namespace LibKubernetesGenerator
{
internal class MetaHelper : INustacheHelper
internal class MetaHelper : IScriptObjectHelper
{
public void RegisterHelper()
public void RegisterHelper(ScriptObject scriptObject)
{
Helpers.Register(nameof(GetGroup), GetGroup);
Helpers.Register(nameof(GetApiVersion), GetApiVersion);
Helpers.Register(nameof(GetKind), GetKind);
Helpers.Register(nameof(GetPathExpression), GetPathExpression);
}
public static void GetKind(RenderContext context, IList<object> arguments, IDictionary<string, object> options,
RenderBlock fn, RenderBlock inverse)
{
if (arguments != null && arguments.Count > 0 && arguments[0] != null && arguments[0] is JsonSchema)
{
context.Write(GetKind(arguments[0] as JsonSchema));
}
scriptObject.Import(nameof(GetGroup), GetGroup);
scriptObject.Import(nameof(GetApiVersion), GetApiVersion);
scriptObject.Import(nameof(GetKind), GetKind);
scriptObject.Import(nameof(GetPathExpression), GetPathExpression);
}
private static string GetKind(JsonSchema definition)
@@ -33,15 +24,6 @@ namespace LibKubernetesGenerator
return groupVersionKind["kind"] as string;
}
public static void GetGroup(RenderContext context, IList<object> arguments, IDictionary<string, object> options,
RenderBlock fn, RenderBlock inverse)
{
if (arguments != null && arguments.Count > 0 && arguments[0] != null && arguments[0] is JsonSchema)
{
context.Write(GetGroup(arguments[0] as JsonSchema));
}
}
private static string GetGroup(JsonSchema definition)
{
var groupVersionKindElements = (object[])definition.ExtensionData["x-kubernetes-group-version-kind"];
@@ -50,16 +32,6 @@ namespace LibKubernetesGenerator
return groupVersionKind["group"] as string;
}
public static void GetApiVersion(RenderContext context, IList<object> arguments,
IDictionary<string, object> options,
RenderBlock fn, RenderBlock inverse)
{
if (arguments != null && arguments.Count > 0 && arguments[0] != null && arguments[0] is JsonSchema)
{
context.Write(GetApiVersion(arguments[0] as JsonSchema));
}
}
private static string GetApiVersion(JsonSchema definition)
{
var groupVersionKindElements = (object[])definition.ExtensionData["x-kubernetes-group-version-kind"];
@@ -68,17 +40,6 @@ namespace LibKubernetesGenerator
return groupVersionKind["version"] as string;
}
public static void GetPathExpression(RenderContext context, IList<object> arguments,
IDictionary<string, object> options, RenderBlock fn, RenderBlock inverse)
{
if (arguments != null && arguments.Count > 0 && arguments[0] != null &&
arguments[0] is OpenApiOperationDescription)
{
var operation = arguments[0] as OpenApiOperationDescription;
context.Write(GetPathExpression(operation));
}
}
private static string GetPathExpression(OpenApiOperationDescription operation)
{
var pathExpression = operation.Path;

View File

@@ -8,10 +8,12 @@ namespace LibKubernetesGenerator
internal class ModelExtGenerator
{
private readonly ClassNameHelper classNameHelper;
private readonly ScriptObjectFactory scriptObjectFactory;
public ModelExtGenerator(ClassNameHelper classNameHelper)
public ModelExtGenerator(ClassNameHelper classNameHelper, ScriptObjectFactory scriptObjectFactory)
{
this.classNameHelper = classNameHelper;
this.scriptObjectFactory = scriptObjectFactory;
}
public void Generate(OpenApiDocument swagger, IncrementalGeneratorPostInitializationContext context)
@@ -25,7 +27,10 @@ namespace LibKubernetesGenerator
&& d.ExtensionData.ContainsKey("x-kubernetes-group-version-kind")
&& !skippedTypes.Contains(classNameHelper.GetClassName(d)));
context.RenderToContext("ModelExtensions.cs.template", definitions, "ModelExtensions.g.cs");
var sc = scriptObjectFactory.CreateScriptObject();
sc.SetValue("definitions", definitions, true);
context.RenderToContext("ModelExtensions.cs.template", sc, "ModelExtensions.g.cs");
}
}
}

View File

@@ -6,22 +6,29 @@ namespace LibKubernetesGenerator
internal class ModelGenerator
{
private readonly ClassNameHelper classNameHelper;
private readonly ScriptObjectFactory scriptObjectFactory;
public ModelGenerator(ClassNameHelper classNameHelper)
public ModelGenerator(ClassNameHelper classNameHelper, ScriptObjectFactory scriptObjectFactory)
{
this.classNameHelper = classNameHelper;
this.scriptObjectFactory = scriptObjectFactory;
}
public void Generate(OpenApiDocument swagger, IncrementalGeneratorPostInitializationContext context)
{
var sc = scriptObjectFactory.CreateScriptObject();
foreach (var kv in swagger.Definitions)
{
var def = kv.Value;
var clz = classNameHelper.GetClassNameForSchemaDefinition(def);
context.RenderToContext(
"Model.cs.template",
new { clz, def, properties = def.Properties.Values },
$"Models_{clz}.g.cs");
sc.SetValue("clz", clz, true);
sc.SetValue("def", def, true);
sc.SetValue("properties", def.Properties.Values, true);
context.RenderToContext("Model.cs.template", sc, $"Models_{clz}.g.cs");
}
}
}

View File

@@ -1,12 +1,12 @@
using NJsonSchema;
using NSwag;
using Nustache.Core;
using System.Collections.Generic;
using Scriban.Runtime;
using System;
using System.Linq;
namespace LibKubernetesGenerator
{
internal class ParamHelper : INustacheHelper
internal class ParamHelper : IScriptObjectHelper
{
private readonly GeneralNameHelper generalNameHelper;
private readonly TypeHelper typeHelper;
@@ -17,26 +17,14 @@ namespace LibKubernetesGenerator
this.typeHelper = typeHelper;
}
public void RegisterHelper()
public void RegisterHelper(ScriptObject scriptObject)
{
Helpers.Register(nameof(IfParamContains), IfParamContains);
Helpers.Register(nameof(IfParamDoesNotContain), IfParamDoesNotContain);
Helpers.Register(nameof(GetModelCtorParam), GetModelCtorParam);
scriptObject.Import(nameof(GetModelCtorParam), new Func<JsonSchema, string>(GetModelCtorParam));
scriptObject.Import(nameof(IfParamContains), IfParamContains);
}
public static void IfParamContains(RenderContext context, IList<object> arguments,
IDictionary<string, object> options,
RenderBlock fn, RenderBlock inverse)
public static bool IfParamContains(OpenApiOperation operation, string name)
{
var operation = arguments?.FirstOrDefault() as OpenApiOperation;
if (operation != null)
{
string name = null;
if (arguments.Count > 1)
{
name = arguments[1] as string;
}
var found = false;
foreach (var param in operation.Parameters)
@@ -48,53 +36,12 @@ namespace LibKubernetesGenerator
}
}
if (found)
{
fn(null);
}
}
return found;
}
public static void IfParamDoesNotContain(RenderContext context, IList<object> arguments,
IDictionary<string, object> options,
RenderBlock fn, RenderBlock inverse)
public string GetModelCtorParam(JsonSchema schema)
{
var operation = arguments?.FirstOrDefault() as OpenApiOperation;
if (operation != null)
{
string name = null;
if (arguments.Count > 1)
{
name = arguments[1] as string;
}
var found = false;
foreach (var param in operation.Parameters)
{
if (param.Name == name)
{
found = true;
break;
}
}
if (!found)
{
fn(null);
}
}
}
public void GetModelCtorParam(RenderContext context, IList<object> arguments,
IDictionary<string, object> options,
RenderBlock fn, RenderBlock inverse)
{
var schema = arguments[0] as JsonSchema;
if (schema != null)
{
context.Write(string.Join(", ", schema.Properties.Values
return string.Join(", ", schema.Properties.Values
.OrderBy(p => !p.IsRequired)
.Select(p =>
{
@@ -107,8 +54,7 @@ namespace LibKubernetesGenerator
}
return sp;
})));
}
}));
}
}
}

View File

@@ -1,13 +1,13 @@
using NJsonSchema;
using NSwag;
using Nustache.Core;
using Scriban.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
namespace LibKubernetesGenerator
{
internal class PluralHelper : INustacheHelper
internal class PluralHelper : IScriptObjectHelper
{
private readonly Dictionary<string, string> _classNameToPluralMap;
private readonly ClassNameHelper classNameHelper;
@@ -23,26 +23,9 @@ namespace LibKubernetesGenerator
_classNameToPluralMap = InitClassNameToPluralMap(swagger);
}
public void RegisterHelper()
public void RegisterHelper(ScriptObject scriptObject)
{
Helpers.Register(nameof(GetPlural), GetPlural);
}
public void GetPlural(RenderContext context, IList<object> arguments, IDictionary<string, object> options,
RenderBlock fn, RenderBlock inverse)
{
if (arguments != null && arguments.Count > 0 && arguments[0] != null && arguments[0] is JsonSchema)
{
var plural = GetPlural(arguments[0] as JsonSchema);
if (plural != null)
{
context.Write($"\"{plural}\"");
}
else
{
context.Write("null");
}
}
scriptObject.Import(nameof(GetPlural), new Func<JsonSchema, string>(GetPlural));
}
public string GetPlural(JsonSchema definition)

View File

@@ -0,0 +1,26 @@
using Scriban.Runtime;
using System.Collections.Generic;
using System.Linq;
namespace LibKubernetesGenerator;
internal class ScriptObjectFactory
{
private readonly List<IScriptObjectHelper> scriptObjectHelpers;
public ScriptObjectFactory(IEnumerable<IScriptObjectHelper> scriptObjectHelpers)
{
this.scriptObjectHelpers = scriptObjectHelpers.ToList();
}
public ScriptObject CreateScriptObject()
{
var scriptObject = new ScriptObject();
foreach (var helper in scriptObjectHelpers)
{
helper.RegisterHelper(scriptObject);
}
return scriptObject;
}
}

View File

@@ -1,15 +1,16 @@
using NJsonSchema;
using Nustache.Core;
using Scriban.Runtime;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security;
using System.Text;
using System.Text.RegularExpressions;
namespace LibKubernetesGenerator
{
internal class StringHelpers : INustacheHelper
internal class StringHelpers : IScriptObjectHelper
{
private readonly GeneralNameHelper generalNameHelper;
@@ -18,21 +19,24 @@ namespace LibKubernetesGenerator
this.generalNameHelper = generalNameHelper;
}
public void RegisterHelper()
public void RegisterHelper(ScriptObject scriptObject)
{
Helpers.Register(nameof(ToXmlDoc), ToXmlDoc);
Helpers.Register(nameof(ToInterpolationPathString), ToInterpolationPathString);
Helpers.Register(nameof(IfGroupPathParamContainsGroup), IfGroupPathParamContainsGroup);
scriptObject.Import(nameof(ToXmlDoc), new Func<string, string>(ToXmlDoc));
scriptObject.Import(nameof(ToInterpolationPathString), ToInterpolationPathString);
scriptObject.Import(nameof(IfGroupPathParamContainsGroup), IfGroupPathParamContainsGroup);
}
private void ToXmlDoc(RenderContext context, IList<object> arguments, IDictionary<string, object> options,
RenderBlock fn, RenderBlock inverse)
public static string ToXmlDoc(string arg)
{
if (arguments != null && arguments.Count > 0 && arguments[0] != null && arguments[0] is string)
if (arg == null)
{
var first = true;
return "";
}
using (var reader = new StringReader(arguments[0] as string))
var first = true;
var sb = new StringBuilder();
using (var reader = new StringReader(arg))
{
string line = null;
while ((line = reader.ReadLine()) != null)
@@ -41,19 +45,20 @@ namespace LibKubernetesGenerator
{
if (!first)
{
context.Write("\n");
context.Write(" /// ");
sb.Append("\n");
sb.Append(" /// ");
}
else
{
first = false;
}
context.Write(SecurityElement.Escape(wline));
}
sb.Append(SecurityElement.Escape(wline));
}
}
}
return sb.ToString();
}
private static IEnumerable<string> WordWrap(string text, int width)
@@ -91,24 +96,14 @@ namespace LibKubernetesGenerator
}
}
public void ToInterpolationPathString(RenderContext context, IList<object> arguments, IDictionary<string, object> options,
RenderBlock fn, RenderBlock inverse)
public string ToInterpolationPathString(string arg)
{
var p = arguments?.FirstOrDefault() as string;
if (p != null)
{
context.Write(Regex.Replace(p, "{(.+?)}", (m) => "{" + generalNameHelper.GetDotNetName(m.Groups[1].Value) + "}"));
}
return Regex.Replace(arg, "{(.+?)}", (m) => "{" + generalNameHelper.GetDotNetName(m.Groups[1].Value) + "}");
}
public void IfGroupPathParamContainsGroup(RenderContext context, IList<object> arguments, IDictionary<string, object> options,
RenderBlock fn, RenderBlock inverse)
public static bool IfGroupPathParamContainsGroup(string arg)
{
var p = arguments?.FirstOrDefault() as string;
if (p?.StartsWith("apis/{group}") == true)
{
fn(null);
}
return arg.StartsWith("apis/{group}");
}
}
}

View File

@@ -1,13 +1,11 @@
using NJsonSchema;
using NSwag;
using Nustache.Core;
using Scriban.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
namespace LibKubernetesGenerator
{
internal class TypeHelper : INustacheHelper
internal class TypeHelper : IScriptObjectHelper
{
private readonly ClassNameHelper classNameHelper;
@@ -16,57 +14,13 @@ namespace LibKubernetesGenerator
this.classNameHelper = classNameHelper;
}
public void RegisterHelper()
public void RegisterHelper(ScriptObject scriptObject)
{
Helpers.Register(nameof(GetDotNetType), GetDotNetType);
Helpers.Register(nameof(GetReturnType), GetReturnType);
Helpers.Register(nameof(IfReturnType), IfReturnType);
Helpers.Register(nameof(IfType), IfType);
}
public void GetDotNetType(RenderContext context, IList<object> arguments,
IDictionary<string, object> options,
RenderBlock fn, RenderBlock inverse)
{
if (arguments != null && arguments.Count > 0 && arguments[0] != null && arguments[0] is OpenApiParameter)
{
var parameter = arguments[0] as OpenApiParameter;
if (parameter.Schema?.Reference != null)
{
context.Write(classNameHelper.GetClassNameForSchemaDefinition(parameter.Schema.Reference));
}
else if (parameter.Schema != null)
{
context.Write(GetDotNetType(parameter.Schema.Type, parameter.Name, parameter.IsRequired,
parameter.Schema.Format));
}
else
{
context.Write(GetDotNetType(parameter.Type, parameter.Name, parameter.IsRequired,
parameter.Format));
}
}
else if (arguments != null && arguments.Count > 0 && arguments[0] != null && arguments[0] is JsonSchemaProperty)
{
var property = arguments[0] as JsonSchemaProperty;
context.Write(GetDotNetType(property));
}
else if (arguments != null && arguments.Count > 2 && arguments[0] != null && arguments[1] != null &&
arguments[2] != null && arguments[0] is JsonObjectType && arguments[1] is string &&
arguments[2] is bool)
{
context.Write(GetDotNetType((JsonObjectType)arguments[0], (string)arguments[1], (bool)arguments[2],
(string)arguments[3]));
}
else if (arguments != null && arguments.Count > 0 && arguments[0] != null)
{
context.Write($"ERROR: Expected OpenApiParameter but got {arguments[0].GetType().FullName}");
}
else
{
context.Write("ERROR: Expected a OpenApiParameter argument but got none.");
}
scriptObject.Import(nameof(GetDotNetType), new Func<JsonSchemaProperty, string>(GetDotNetType));
scriptObject.Import(nameof(GetDotNetTypeOpenApiParameter), new Func<OpenApiParameter, string>(GetDotNetTypeOpenApiParameter));
scriptObject.Import(nameof(GetReturnType), new Func<OpenApiOperation, string, string>(GetReturnType));
scriptObject.Import(nameof(IfReturnType), new Func<OpenApiOperation, string, bool>(IfReturnType));
scriptObject.Import(nameof(IfType), new Func<JsonSchemaProperty, string, bool>(IfType));
}
private string GetDotNetType(JsonObjectType jsonType, string name, bool required, string format)
@@ -204,20 +158,21 @@ namespace LibKubernetesGenerator
return GetDotNetType(p.Type, p.Name, p.IsRequired, p.Format);
}
public void GetReturnType(RenderContext context, IList<object> arguments,
IDictionary<string, object> options,
RenderBlock fn, RenderBlock inverse)
public string GetDotNetTypeOpenApiParameter(OpenApiParameter parameter)
{
var operation = arguments?.FirstOrDefault() as OpenApiOperation;
if (operation != null)
if (parameter.Schema?.Reference != null)
{
string style = null;
if (arguments.Count > 1)
{
style = arguments[1] as string;
return classNameHelper.GetClassNameForSchemaDefinition(parameter.Schema.Reference);
}
context.Write(GetReturnType(operation, style));
else if (parameter.Schema != null)
{
return (GetDotNetType(parameter.Schema.Type, parameter.Name, parameter.IsRequired,
parameter.Schema.Format));
}
else
{
return (GetDotNetType(parameter.Type, parameter.Name, parameter.IsRequired,
parameter.Format));
}
}
@@ -295,57 +250,38 @@ namespace LibKubernetesGenerator
return t;
}
public void IfReturnType(RenderContext context, IList<object> arguments,
IDictionary<string, object> options,
RenderBlock fn, RenderBlock inverse)
public bool IfReturnType(OpenApiOperation operation, string type)
{
var operation = arguments?.FirstOrDefault() as OpenApiOperation;
if (operation != null)
{
string type = null;
if (arguments.Count > 1)
{
type = arguments[1] as string;
}
var rt = GetReturnType(operation, "void");
if (type == "any" && rt != "void")
{
fn(null);
return true;
}
else if (string.Equals(type, rt.ToLower(), StringComparison.OrdinalIgnoreCase))
{
fn(null);
return true;
}
else if (type == "obj" && rt != "void" && rt != "Stream")
{
fn(null);
}
}
return true;
}
public static void IfType(RenderContext context, IList<object> arguments, IDictionary<string, object> options,
RenderBlock fn, RenderBlock inverse)
{
var property = arguments?.FirstOrDefault() as JsonSchemaProperty;
if (property != null)
{
string type = null;
if (arguments.Count > 1)
{
type = arguments[1] as string;
return false;
}
public static bool IfType(JsonSchemaProperty property, string type)
{
if (type == "object" && property.Reference != null && !property.IsArray &&
property.AdditionalPropertiesSchema == null)
{
fn(null);
return true;
}
else if (type == "objectarray" && property.IsArray && property.Item?.Reference != null)
{
fn(null);
}
return true;
}
return false;
}
}
}

View File

@@ -1,50 +1,30 @@
using NSwag;
using Nustache.Core;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Scriban.Runtime;
namespace LibKubernetesGenerator
{
internal class UtilHelper : INustacheHelper
internal class UtilHelper : IScriptObjectHelper
{
public void RegisterHelper()
public void RegisterHelper(ScriptObject scriptObject)
{
Helpers.Register(nameof(IfKindIs), IfKindIs);
Helpers.Register(nameof(IfListNotEmpty), IfListNotEmpty);
scriptObject.Import(nameof(IfKindIs), IfKindIs);
}
public static void IfKindIs(RenderContext context, IList<object> arguments, IDictionary<string, object> options,
RenderBlock fn, RenderBlock inverse)
public static bool IfKindIs(OpenApiParameter parameter, string kind)
{
var parameter = arguments?.FirstOrDefault() as OpenApiParameter;
if (parameter != null)
{
string kind = null;
if (arguments.Count > 1)
{
kind = arguments[1] as string;
}
if (kind == "query" && parameter.Kind == OpenApiParameterKind.Query)
{
fn(null);
return true;
}
else if (kind == "path" && parameter.Kind == OpenApiParameterKind.Path)
{
fn(null);
}
return true;
}
}
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);
}
return false;
}
}
}

View File

@@ -10,7 +10,7 @@ namespace k8s;
/// </summary>
public abstract partial class AbstractKubernetes
{
{{#.}}
public I{{.}}Operations {{.}} => this;
{{/.}}
{{for group in groups}}
public I{{group}}Operations {{group}} => this;
{{end}}
}

View File

@@ -10,7 +10,7 @@ namespace k8s;
/// </summary>
public partial interface IBasicKubernetes
{
{{#.}}
I{{.}}Operations {{.}} { get; }
{{/.}}
{{for group in groups}}
I{{group}}Operations {{group}} { get; }
{{end}}
}

View File

@@ -10,50 +10,50 @@ namespace k8s;
/// </summary>
public partial interface I{{name}}Operations
{
{{#apis}}
{{for api in apis }}
/// <summary>
/// {{ToXmlDoc operation.description}}
/// {{ToXmlDoc api.operation.description}}
/// </summary>
{{#operation.parameters}}
/// <param name="{{GetDotNetName .}}">
/// {{ToXmlDoc description}}
{{ for parameter in api.operation.parameters}}
/// <param name="{{GetDotNetNameOpenApiParameter parameter "false"}}">
/// {{ToXmlDoc parameter.description}}
/// </param>
{{/operation.parameters}}
{{ end }}
/// <param name="customHeaders">
/// The headers that will be added to request.
/// </param>
/// <param name="cancellationToken">
/// A <see cref="CancellationToken"/> which can be used to cancel the asynchronous operation.
/// </param>
Task<HttpOperationResponse{{GetReturnType operation "<>"}}> {{GetMethodName operation "WithHttpMessagesAsync"}}(
{{#operation.parameters}}
{{GetDotNetType .}} {{GetDotNetName . "true"}},
{{/operation.parameters}}
Task<HttpOperationResponse{{GetReturnType api.operation "<>"}}> {{GetMethodName api.operation "WithHttpMessagesAsync"}}(
{{ for parameter in api.operation.parameters}}
{{GetDotNetTypeOpenApiParameter parameter}} {{GetDotNetNameOpenApiParameter parameter "true"}},
{{ end }}
IReadOnlyDictionary<string, IReadOnlyList<string>> customHeaders = null,
CancellationToken cancellationToken = default);
{{#IfReturnType operation "object"}}
{{if IfReturnType api.operation "object"}}
/// <summary>
/// {{ToXmlDoc operation.description}}
/// {{ToXmlDoc api.operation.description}}
/// </summary>
{{#operation.parameters}}
/// <param name="{{GetDotNetName .}}">
/// {{ToXmlDoc description}}
{{ for parameter in api.operation.parameters}}
/// <param name="{{GetDotNetNameOpenApiParameter parameter "false"}}">
/// {{ToXmlDoc parameter.description}}
/// </param>
{{/operation.parameters}}
{{ end }}
/// <param name="customHeaders">
/// The headers that will be added to request.
/// </param>
/// <param name="cancellationToken">
/// A <see cref="CancellationToken"/> which can be used to cancel the asynchronous operation.
/// </param>
Task<HttpOperationResponse<T>> {{GetMethodName operation "WithHttpMessagesAsync"}}<T>(
{{#operation.parameters}}
{{GetDotNetType .}} {{GetDotNetName . "true"}},
{{/operation.parameters}}
Task<HttpOperationResponse<T>> {{GetMethodName api.operation "WithHttpMessagesAsync"}}<T>(
{{ for parameter in api.operation.parameters}}
{{GetDotNetTypeOpenApiParameter parameter}} {{GetDotNetNameOpenApiParameter parameter "true"}},
{{ end }}
IReadOnlyDictionary<string, IReadOnlyList<string>> customHeaders = null,
CancellationToken cancellationToken = default);
{{/IfReturnType operation "object"}}
{{ end }}
{{/apis}}
{{ end }}
}

View File

@@ -22,25 +22,26 @@ namespace k8s.Models
/// <summary>
/// Initializes a new instance of the {{GetClassName def}} class.
/// </summary>
{{#properties}}
{{#isRequired}}
/// <param name="{{GetDotNetName name "fieldctor"}}">
/// {{ToXmlDoc description}}
{{ for property in properties }}
{{ if property.IsRequired }}
/// <param name="{{GetDotNetName property.name "fieldctor"}}">
/// {{ToXmlDoc property.description}}
/// </param>
{{/isRequired}}
{{/properties}}
{{#properties}}
{{^isRequired}}
/// <param name="{{GetDotNetName name "fieldctor"}}">
/// {{ToXmlDoc description}}
{{ end }}
{{ end }}
{{ for property in properties }}
{{ if !property.IsRequired }}
/// <param name="{{GetDotNetName property.name "fieldctor"}}">
/// {{ToXmlDoc property.description}}
/// </param>
{{/isRequired}}
{{/properties}}
{{ end }}
{{ end }}
public {{clz}}({{GetModelCtorParam def}})
{
{{#properties}}
{{GetDotNetName name "field"}} = {{GetDotNetName name "fieldctor"}};
{{/properties}}
{{ for property in properties }}
{{GetDotNetName property.name "field"}} = {{GetDotNetName property.name "fieldctor"}};
{{ end }}
CustomInit();
}
@@ -48,14 +49,14 @@ namespace k8s.Models
/// An initialization method that performs custom operations like setting defaults
/// </summary>
partial void CustomInit();
{{#properties}}
{{ for property in properties }}
/// <summary>
/// {{ToXmlDoc description}}
/// {{ToXmlDoc property.description}}
/// </summary>
[JsonPropertyName("{{name}}")]
public {{GetDotNetType .}} {{GetDotNetName name "field"}} { get; set; }
{{/properties}}
[JsonPropertyName("{{property.name}}")]
public {{GetDotNetType property}} {{GetDotNetName property.name "field"}} { get; set; }
{{ end }}
/// <summary>
/// Validate the object.
@@ -65,29 +66,32 @@ namespace k8s.Models
/// </exception>
public virtual void Validate()
{
{{#properties}}
{{#IfType . "object"}}
{{#isRequired}}
if ({{GetDotNetName name "field"}} == null)
{{ for property in properties }}
{{if IfType property "object" }}
{{ if property.IsRequired }}
if ({{GetDotNetName property.name "field"}} == null)
{
throw new ArgumentNullException("{{GetDotNetName name "field"}}");
throw new ArgumentNullException("{{GetDotNetName property.name "field"}}");
}
{{/isRequired}}
{{/IfType . "object"}}
{{/properties}}
{{#properties}}
{{#IfType . "object"}}
{{GetDotNetName name "field"}}?.Validate();
{{/IfType . "object"}}
{{#IfType . "objectarray"}}
if ({{GetDotNetName name "field"}} != null){
foreach(var obj in {{GetDotNetName name "field"}})
{{ end }}
{{ end }}
{{ end }}
{{ for property in properties }}
{{if IfType property "object" }}
{{GetDotNetName property.name "field"}}?.Validate();
{{ end }}
{{if IfType property "objectarray" }}
if ({{GetDotNetName property.name "field"}} != null){
foreach(var obj in {{GetDotNetName property.name "field"}})
{
obj.Validate();
}
}
{{/IfType . "objectarray"}}
{{/properties}}
{{ end }}
{{ end }}
}
}
}

View File

@@ -4,25 +4,24 @@
// </auto-generated>
namespace k8s.Models
{
{{#.}}
{{ for definition in definitions }}
[KubernetesEntity(Group=KubeGroup, Kind=KubeKind, ApiVersion=KubeApiVersion, PluralName=KubePluralName)]
public partial class {{GetClassName . }} : {{GetInterfaceName . }}
public partial class {{ GetClassName definition }} : {{ GetInterfaceName definition }}
{
public const string KubeApiVersion = "{{GetApiVersion . }}";
public const string KubeKind = "{{GetKind . }}";
public const string KubeGroup = "{{GetGroup . }}";
public const string KubePluralName = {{GetPlural . }};
public const string KubeApiVersion = "{{ GetApiVersion definition }}";
public const string KubeKind = "{{ GetKind definition }}";
public const string KubeGroup = "{{ GetGroup definition }}";
public const string KubePluralName = "{{ GetPlural definition }}";
}
{{/.}}
{{ end }}
}
#if NET8_0_OR_GREATER
namespace k8s
{
{{#.}}
[JsonSerializable(typeof({{GetClassName . }}))]
{{/.}}
{{ for definition in definitions }}
[JsonSerializable(typeof({{ GetClassName definition }}))]
{{ end }}
internal partial class SourceGenerationContext : JsonSerializerContext
{
}

View File

@@ -8,139 +8,137 @@ namespace k8s;
public partial class AbstractKubernetes : I{{name}}Operations
{
{{#apis}}
{{#IfReturnType operation "void"}}
private async Task<HttpOperationResponse> I{{name}}Operations_{{GetMethodName operation "WithHttpMessagesAsync"}}(
{{/IfReturnType operation "void"}}
{{#IfReturnType operation "obj"}}
private async Task<HttpOperationResponse<T>> I{{name}}Operations_{{GetMethodName operation "WithHttpMessagesAsync"}}<T>(
{{/IfReturnType operation "obj"}}
{{#IfReturnType operation "stream"}}
private async Task<HttpOperationResponse<Stream>> I{{name}}Operations_{{GetMethodName operation "WithHttpMessagesAsync"}}(
{{/IfReturnType operation "stream"}}
{{#operation.parameters}}
{{GetDotNetType .}} {{GetDotNetName .}},
{{/operation.parameters}}
{{for api in apis }}
{{if IfReturnType api.operation "void"}}
private async Task<HttpOperationResponse> I{{name}}Operations_{{GetMethodName api.operation "WithHttpMessagesAsync"}}(
{{end}}
{{if IfReturnType api.operation "obj"}}
private async Task<HttpOperationResponse<T>> I{{name}}Operations_{{GetMethodName api.operation "WithHttpMessagesAsync"}}<T>(
{{end}}
{{if IfReturnType api.operation "stream"}}
private async Task<HttpOperationResponse<Stream>> I{{name}}Operations_{{GetMethodName api.operation "WithHttpMessagesAsync"}}(
{{end}}
{{ for parameter in api.operation.parameters}}
{{GetDotNetTypeOpenApiParameter parameter}} {{GetDotNetNameOpenApiParameter parameter "false"}},
{{end}}
IReadOnlyDictionary<string, IReadOnlyList<string>> customHeaders,
CancellationToken cancellationToken)
{
using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
cts.CancelAfter(HttpClientTimeout);
{{#IfParamContains operation "watch"}}
{{if IfParamContains api.operation "watch"}}
if (watch == true)
{
cts.CancelAfter(Timeout.InfiniteTimeSpan);
}
{{/IfParamContains operation "watch"}}
{{end}}
cancellationToken = cts.Token;
{{#operation.parameters}}
{{#isRequired}}
if ({{GetDotNetName name}} == null)
{{ for parameter in api.operation.parameters}}
{{ if parameter.IsRequired}}
if ({{GetDotNetName parameter.name}} == null)
{
throw new ArgumentNullException("{{GetDotNetName name}}");
throw new ArgumentNullException("{{GetDotNetName parameter.name}}");
}
{{/isRequired}}
{{/operation.parameters}}
{{end}}
{{end}}
// Construct URL
var url = $"{{ToInterpolationPathString path}}";
{{#IfGroupPathParamContainsGroup path}}
var url = $"{{ToInterpolationPathString api.path}}";
{{if IfGroupPathParamContainsGroup api.path}}
url = url.Replace("apis//", "api/");
{{/IfGroupPathParamContainsGroup}}
{{#IfListNotEmpty operation.parameters}}
{{end}}
{{if (array.size api.operation.parameters) > 0}}
var q = new QueryBuilder();
{{#operation.parameters}}
{{#IfKindIs . "query"}}
q.Append("{{name}}", {{GetDotNetName name}});
{{/IfKindIs . "query"}}
{{/operation.parameters}}
{{ for parameter in api.operation.parameters}}
{{if IfKindIs parameter "query"}}
q.Append("{{parameter.name}}", {{GetDotNetName parameter.name}});
{{end}}
{{end}}
url += q.ToString();
{{/IfListNotEmpty operation.parameters}}
{{end}}
// Create HTTP transport
{{#IfParamContains operation "body"}}
var httpResponse = await SendRequest(url, HttpMethods.{{Method}}, customHeaders, body, cancellationToken);
{{/IfParamContains operation "body"}}
{{#IfParamDoesNotContain operation "body"}}
var httpResponse = await SendRequest<object>(url, HttpMethods.{{Method}}, customHeaders, null, cancellationToken);
{{/IfParamDoesNotContain operation "body"}}
{{if IfParamContains api.operation "body"}}
var httpResponse = await SendRequest(url, HttpMethods.{{api.method}}, customHeaders, body, cancellationToken);
{{ else }}
var httpResponse = await SendRequest<object>(url, HttpMethods.{{api.method}}, customHeaders, null, cancellationToken);
{{end}}
// Create Result
var httpRequest = httpResponse.RequestMessage;
{{#IfReturnType operation "void"}}
{{if IfReturnType api.operation "void"}}
HttpOperationResponse result = new HttpOperationResponse() { Request = httpRequest, Response = httpResponse };
{{/IfReturnType operation "void"}}
{{#IfReturnType operation "obj"}}
{{end}}
{{if IfReturnType api.operation "obj"}}
var result = await CreateResultAsync<T>(
httpRequest,
httpResponse,
{{#IfParamContains operation "watch"}}
{{if IfParamContains api.operation "watch"}}
watch,
{{/IfParamContains operation "watch"}}
{{#IfParamDoesNotContain operation "watch"}}
{{else}}
false,
{{/IfParamDoesNotContain operation "watch"}}
{{end}}
cancellationToken);
{{/IfReturnType operation "obj"}}
{{#IfReturnType operation "stream"}}
{{end}}
{{if IfReturnType api.operation "stream"}}
var result = new HttpOperationResponse<Stream>() {
Request = httpRequest,
Response = httpResponse,
Body = await httpResponse.Content.ReadAsStreamAsync().ConfigureAwait(false) };
{{/IfReturnType operation "stream"}}
{{end}}
return result;
}
/// <inheritdoc/>
async Task<HttpOperationResponse{{GetReturnType operation "<>"}}> I{{name}}Operations.{{GetMethodName operation "WithHttpMessagesAsync"}}(
{{#operation.parameters}}
{{GetDotNetType .}} {{GetDotNetName .}},
{{/operation.parameters}}
async Task<HttpOperationResponse{{GetReturnType api.operation "<>"}}> I{{name}}Operations.{{GetMethodName api.operation "WithHttpMessagesAsync"}}(
{{ for parameter in api.operation.parameters}}
{{GetDotNetTypeOpenApiParameter parameter}} {{GetDotNetNameOpenApiParameter parameter "false"}},
{{end}}
IReadOnlyDictionary<string, IReadOnlyList<string>> customHeaders,
CancellationToken cancellationToken)
{
{{#IfReturnType operation "void"}}
return await I{{name}}Operations_{{GetMethodName operation "WithHttpMessagesAsync"}}(
{{#operation.parameters}}
{{GetDotNetName .}},
{{/operation.parameters}}
{{if IfReturnType api.operation "void"}}
return await I{{name}}Operations_{{GetMethodName api.operation "WithHttpMessagesAsync"}}(
{{ for parameter in api.operation.parameters}}
{{GetDotNetNameOpenApiParameter parameter "false"}},
{{end}}
customHeaders,
cancellationToken).ConfigureAwait(false);
{{/IfReturnType operation "void"}}
{{#IfReturnType operation "obj"}}
return await I{{name}}Operations_{{GetMethodName operation "WithHttpMessagesAsync"}}{{GetReturnType operation "<>"}}(
{{#operation.parameters}}
{{GetDotNetName .}},
{{/operation.parameters}}
{{end}}
{{if IfReturnType api.operation "obj"}}
return await I{{name}}Operations_{{GetMethodName api.operation "WithHttpMessagesAsync"}}{{GetReturnType api.operation "<>"}}(
{{ for parameter in api.operation.parameters}}
{{GetDotNetNameOpenApiParameter parameter "false"}},
{{end}}
customHeaders,
cancellationToken).ConfigureAwait(false);
{{/IfReturnType operation "obj"}}
{{#IfReturnType operation "stream"}}
return await I{{name}}Operations_{{GetMethodName operation "WithHttpMessagesAsync"}}(
{{#operation.parameters}}
{{GetDotNetName .}},
{{/operation.parameters}}
{{end}}
{{if IfReturnType api.operation "stream"}}
return await I{{name}}Operations_{{GetMethodName api.operation "WithHttpMessagesAsync"}}(
{{ for parameter in api.operation.parameters}}
{{GetDotNetNameOpenApiParameter parameter "false"}},
{{end}}
customHeaders,
cancellationToken).ConfigureAwait(false);
{{/IfReturnType operation "stream"}}
{{end}}
}
{{#IfReturnType operation "object"}}
{{if IfReturnType api.operation "object"}}
/// <inheritdoc/>
async Task<HttpOperationResponse<T>> I{{name}}Operations.{{GetMethodName operation "WithHttpMessagesAsync"}}<T>(
{{#operation.parameters}}
{{GetDotNetType .}} {{GetDotNetName .}},
{{/operation.parameters}}
async Task<HttpOperationResponse<T>> I{{name}}Operations.{{GetMethodName api.operation "WithHttpMessagesAsync"}}<T>(
{{ for parameter in api.operation.parameters}}
{{GetDotNetTypeOpenApiParameter parameter}} {{GetDotNetNameOpenApiParameter parameter "false"}},
{{end}}
IReadOnlyDictionary<string, IReadOnlyList<string>> customHeaders,
CancellationToken cancellationToken)
{
return await I{{name}}Operations_{{GetMethodName operation "WithHttpMessagesAsync"}}<T>(
{{#operation.parameters}}
{{GetDotNetName .}},
{{/operation.parameters}}
return await I{{name}}Operations_{{GetMethodName api.operation "WithHttpMessagesAsync"}}<T>(
{{ for parameter in api.operation.parameters}}
{{GetDotNetNameOpenApiParameter parameter "false"}},
{{end}}
customHeaders,
cancellationToken).ConfigureAwait(false);
}
{{/IfReturnType operation "object"}}
{{/apis}}
{{end}}
{{end}}
}

View File

@@ -11,148 +11,148 @@ namespace k8s;
/// </summary>
public static partial class {{name}}OperationsExtensions
{
{{#apis}}
{{for api in apis }}
/// <summary>
/// {{ToXmlDoc operation.description}}
/// {{ToXmlDoc api.operation.description}}
/// </summary>
/// <param name='operations'>
/// The operations group for this extension method.
/// </param>
{{#operation.parameters}}
/// <param name="{{GetDotNetName .}}">
/// {{ToXmlDoc description}}
{{ for parameter in api.operation.parameters}}
/// <param name="{{GetDotNetNameOpenApiParameter parameter "false"}}">
/// {{ToXmlDoc api.description}}
/// </param>
{{/operation.parameters}}
public static {{GetReturnType operation "void"}} {{GetMethodName operation ""}}(
{{ end }}
public static {{GetReturnType api.operation "void"}} {{GetMethodName api.operation ""}}(
this I{{name}}Operations operations
{{#operation.parameters}}
,{{GetDotNetType .}} {{GetDotNetName . "true"}}
{{/operation.parameters}}
{{ for parameter in api.operation.parameters}}
,{{GetDotNetTypeOpenApiParameter parameter}} {{GetDotNetNameOpenApiParameter parameter "true"}}
{{end}}
)
{
{{GetReturnType operation "return"}} operations.{{GetMethodName operation "Async"}}(
{{#operation.parameters}}
{{GetDotNetName .}},
{{/operation.parameters}}
{{GetReturnType api.operation "return"}} operations.{{GetMethodName api.operation "Async"}}(
{{ for parameter in api.operation.parameters}}
{{GetDotNetNameOpenApiParameter parameter "false"}},
{{end}}
CancellationToken.None
).GetAwaiter().GetResult();
}
{{#IfReturnType operation "object"}}
{{if IfReturnType api.operation "object"}}
/// <summary>
/// {{ToXmlDoc operation.description}}
/// {{ToXmlDoc api.operation.description}}
/// </summary>
/// <param name='operations'>
/// The operations group for this extension method.
/// </param>
{{#operation.parameters}}
/// <param name="{{GetDotNetName .}}">
/// {{ToXmlDoc description}}
{{ for parameter in api.operation.parameters}}
/// <param name="{{GetDotNetNameOpenApiParameter parameter "false"}}">
/// {{ToXmlDoc parameter.description}}
/// </param>
{{/operation.parameters}}
public static T {{GetMethodName operation ""}}<T>(
{{end}}
public static T {{GetMethodName api.operation ""}}<T>(
this I{{name}}Operations operations
{{#operation.parameters}}
,{{GetDotNetType .}} {{GetDotNetName . "true"}}
{{/operation.parameters}}
{{ for parameter in api.operation.parameters}}
,{{GetDotNetTypeOpenApiParameter parameter}} {{GetDotNetNameOpenApiParameter parameter "true"}}
{{end}}
)
{
return operations.{{GetMethodName operation "Async"}}<T>(
{{#operation.parameters}}
{{GetDotNetName .}},
{{/operation.parameters}}
return operations.{{GetMethodName api.operation "Async"}}<T>(
{{ for parameter in api.operation.parameters}}
{{GetDotNetNameOpenApiParameter parameter "false"}},
{{end}}
CancellationToken.None
).GetAwaiter().GetResult();
}
{{/IfReturnType operation "object"}}
{{end}}
/// <summary>
/// {{ToXmlDoc operation.description}}
/// {{ToXmlDoc api.operation.description}}
/// </summary>
/// <param name='operations'>
/// The operations group for this extension method.
/// </param>
{{#operation.parameters}}
/// <param name="{{GetDotNetName .}}">
/// {{ToXmlDoc description}}
{{ for parameter in api.operation.parameters}}
/// <param name="{{GetDotNetNameOpenApiParameter parameter "false"}}">
/// {{ToXmlDoc parameter.description}}
/// </param>
{{/operation.parameters}}
{{end}}
/// <param name="cancellationToken">
/// A <see cref="CancellationToken"/> which can be used to cancel the asynchronous operation.
/// </param>
public static async Task{{GetReturnType operation "<>"}} {{GetMethodName operation "Async"}}(
public static async Task{{GetReturnType api.operation "<>"}} {{GetMethodName api.operation "Async"}}(
this I{{name}}Operations operations,
{{#operation.parameters}}
{{GetDotNetType .}} {{GetDotNetName . "true"}},
{{/operation.parameters}}
{{ for parameter in api.operation.parameters}}
{{GetDotNetTypeOpenApiParameter parameter}} {{GetDotNetNameOpenApiParameter parameter "true"}},
{{ end }}
CancellationToken cancellationToken = default(CancellationToken))
{
{{#IfReturnType operation "stream"}}
var _result = await operations.{{GetMethodName operation "WithHttpMessagesAsync"}}(
{{#operation.parameters}}
{{GetDotNetName .}},
{{/operation.parameters}}
{{if IfReturnType api.operation "stream"}}
var _result = await operations.{{GetMethodName api.operation "WithHttpMessagesAsync"}}(
{{ for parameter in api.operation.parameters}}
{{GetDotNetNameOpenApiParameter parameter "false"}},
{{end}}
null,
cancellationToken);
_result.Request.Dispose();
{{GetReturnType operation "_result.Body"}};
{{/IfReturnType operation "stream"}}
{{#IfReturnType operation "obj"}}
using (var _result = await operations.{{GetMethodName operation "WithHttpMessagesAsync"}}(
{{#operation.parameters}}
{{GetDotNetName .}},
{{/operation.parameters}}
{{GetReturnType api.operation "_result.Body"}};
{{end}}
{{if IfReturnType api.operation "obj"}}
using (var _result = await operations.{{GetMethodName api.operation "WithHttpMessagesAsync"}}(
{{ for parameter in api.operation.parameters}}
{{GetDotNetNameOpenApiParameter parameter "false"}},
{{end}}
null,
cancellationToken).ConfigureAwait(false))
{
{{GetReturnType operation "_result.Body"}};
{{GetReturnType api.operation "_result.Body"}};
}
{{/IfReturnType operation "obj"}}
{{#IfReturnType operation "void"}}
using (var _result = await operations.{{GetMethodName operation "WithHttpMessagesAsync"}}(
{{#operation.parameters}}
{{GetDotNetName .}},
{{/operation.parameters}}
{{end}}
{{if IfReturnType api.operation "void"}}
using (var _result = await operations.{{GetMethodName api.operation "WithHttpMessagesAsync"}}(
{{ for parameter in api.operation.parameters}}
{{GetDotNetNameOpenApiParameter parameter "false"}},
{{end}}
null,
cancellationToken).ConfigureAwait(false))
{
}
{{/IfReturnType operation "void"}}
{{end}}
}
{{#IfReturnType operation "object"}}
{{if IfReturnType api.operation "object"}}
/// <summary>
/// {{ToXmlDoc operation.description}}
/// {{ToXmlDoc api.operation.description}}
/// </summary>
/// <param name='operations'>
/// The operations group for this extension method.
/// </param>
{{#operation.parameters}}
/// <param name="{{GetDotNetName .}}">
/// {{ToXmlDoc description}}
{{ for parameter in api.operation.parameters}}
/// <param name="{{GetDotNetNameOpenApiParameter parameter "false"}}">
/// {{ToXmlDoc parameter.description}}
/// </param>
{{/operation.parameters}}
{{end}}
/// <param name="cancellationToken">
/// A <see cref="CancellationToken"/> which can be used to cancel the asynchronous operation.
/// </param>
public static async Task<T> {{GetMethodName operation "Async"}}<T>(
public static async Task<T> {{GetMethodName api.operation "Async"}}<T>(
this I{{name}}Operations operations,
{{#operation.parameters}}
{{GetDotNetType .}} {{GetDotNetName . "true"}},
{{/operation.parameters}}
{{ for parameter in api.operation.parameters}}
{{GetDotNetTypeOpenApiParameter parameter}} {{GetDotNetNameOpenApiParameter parameter "true"}},
{{ end }}
CancellationToken cancellationToken = default(CancellationToken))
{
using (var _result = await operations.{{GetMethodName operation "WithHttpMessagesAsync"}}<T>(
{{#operation.parameters}}
{{GetDotNetName .}},
{{/operation.parameters}}
using (var _result = await operations.{{GetMethodName api.operation "WithHttpMessagesAsync"}}<T>(
{{ for parameter in api.operation.parameters}}
{{GetDotNetNameOpenApiParameter parameter "false"}},
{{end}}
null,
cancellationToken).ConfigureAwait(false))
{
return _result.Body;
}
}
{{/IfReturnType}}
{{end}}
{{/apis}}
{{end}}
}