From 3dae1cf299df50fb1c62f00b0f5fe04cc4856c41 Mon Sep 17 00:00:00 2001 From: Boshi Lian Date: Mon, 15 Apr 2024 10:43:05 -0700 Subject: [PATCH] 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 --- src/LibKubernetesGenerator/ApiGenerator.cs | 27 ++- src/LibKubernetesGenerator/ClassNameHelper.cs | 37 +--- .../GeneralNameHelper.cs | 61 ++----- .../GeneratorExecutionContextExt.cs | 16 +- src/LibKubernetesGenerator/INustacheHelper.cs | 7 - .../IScriptObjectHelper.cs | 8 + .../KubernetesClientSourceGenerator.cs | 45 ++--- .../LibKubernetesGenerator.target | 26 ++- src/LibKubernetesGenerator/MetaHelper.cs | 59 ++----- .../ModelExtGenerator.cs | 9 +- src/LibKubernetesGenerator/ModelGenerator.cs | 17 +- src/LibKubernetesGenerator/ParamHelper.cs | 88 ++-------- src/LibKubernetesGenerator/PluralHelper.cs | 25 +-- .../ScriptObjectFactory.cs | 26 +++ src/LibKubernetesGenerator/StringHelpers.cs | 73 ++++---- src/LibKubernetesGenerator/TypeHelper.cs | 154 +++++------------ src/LibKubernetesGenerator/UtilHelper.cs | 36 +--- .../templates/AbstractKubernetes.cs.template | 8 +- .../templates/IBasicKubernetes.cs.template | 6 +- .../templates/IOperations.cs.template | 44 ++--- .../templates/Model.cs.template | 78 +++++---- .../templates/ModelExtensions.cs.template | 21 ++- .../templates/Operations.cs.template | 158 +++++++++--------- .../OperationsExtensions.cs.template | 148 ++++++++-------- 24 files changed, 485 insertions(+), 692 deletions(-) delete mode 100644 src/LibKubernetesGenerator/INustacheHelper.cs create mode 100644 src/LibKubernetesGenerator/IScriptObjectHelper.cs create mode 100644 src/LibKubernetesGenerator/ScriptObjectFactory.cs diff --git a/src/LibKubernetesGenerator/ApiGenerator.cs b/src/LibKubernetesGenerator/ApiGenerator.cs index 53acfb047..63fd9667e 100644 --- a/src/LibKubernetesGenerator/ApiGenerator.cs +++ b/src/LibKubernetesGenerator/ApiGenerator.cs @@ -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 @@ public void Generate(OpenApiDocument swagger, IncrementalGeneratorPostInitializa }) .ToArray(); + var sc = scriptObjectFactory.CreateScriptObject(); + var groups = new List(); foreach (var grouped in data.GroupBy(d => d.Operation.Tags.First())) @@ -50,14 +59,20 @@ public void Generate(OpenApiDocument swagger, IncrementalGeneratorPostInitializa 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"); } } } diff --git a/src/LibKubernetesGenerator/ClassNameHelper.cs b/src/LibKubernetesGenerator/ClassNameHelper.cs index b7e224039..a307a160f 100644 --- a/src/LibKubernetesGenerator/ClassNameHelper.cs +++ b/src/LibKubernetesGenerator/ClassNameHelper.cs @@ -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 classNameMap; private readonly Dictionary schemaToNameMapCooked; @@ -18,9 +19,10 @@ public ClassNameHelper(OpenApiDocument swagger) schemaToNameMapCooked = GenerateSchemaToNameMapCooked(swagger); } - public void RegisterHelper() + + public void RegisterHelper(ScriptObject scriptObject) { - Helpers.Register(nameof(GetClassName), GetClassName); + scriptObject.Import(nameof(GetClassName), new Func(GetClassNameForSchemaDefinition)); } private static Dictionary GenerateSchemaToNameMapCooked(OpenApiDocument swagger) @@ -50,27 +52,7 @@ public void RegisterHelper() return map; } - public void GetClassName(RenderContext context, IList arguments, IDictionary 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)operation.ExtensionData["x-kubernetes-group-version-kind"]; - return GetClassName(groupVersionKind); - } - - public string GetClassName(Dictionary groupVersionKind) + private string GetClassName(Dictionary groupVersionKind) { var group = (string)groupVersionKind["group"]; var kind = (string)groupVersionKind["kind"]; @@ -98,10 +80,5 @@ public string GetClassNameForSchemaDefinition(JsonSchema definition) return schemaToNameMapCooked[definition]; } - - private static Dictionary InitSchemaToNameCooked(OpenApiDocument swagger) - { - return swagger.Definitions.ToDictionary(x => x.Value, x => x.Key.Replace(".", "").ToPascalCase()); - } } } diff --git a/src/LibKubernetesGenerator/GeneralNameHelper.cs b/src/LibKubernetesGenerator/GeneralNameHelper.cs index c86464491..16616bafa 100644 --- a/src/LibKubernetesGenerator/GeneralNameHelper.cs +++ b/src/LibKubernetesGenerator/GeneralNameHelper.cs @@ -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 @@ public GeneralNameHelper(ClassNameHelper classNameHelper) 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 arguments, - IDictionary 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(GetInterfaceName)); + scriptObject.Import(nameof(GetMethodName), new Func(GetMethodName)); + scriptObject.Import(nameof(GetDotNetName), new Func(GetDotNetName)); + scriptObject.Import(nameof(GetDotNetNameOpenApiParameter), new Func(GetDotNetNameOpenApiParameter)); } private string GetInterfaceName(JsonSchema definition) @@ -68,44 +61,16 @@ private string GetInterfaceName(JsonSchema definition) return string.Join(", ", interfaces); } - public void GetMethodName(RenderContext context, IList arguments, IDictionary 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) - { - string suffix = null; - if (arguments.Count > 1) - { - suffix = arguments[1] as string; - } + var name = GetDotNetName(parameter.Name); - context.Write(GetMethodName(arguments[0] as OpenApiOperation, suffix)); - } - } - - public void GetDotNetName(RenderContext context, IList arguments, IDictionary options, - RenderBlock fn, RenderBlock inverse) - { - if (arguments != null && arguments.Count > 0 && arguments[0] != null && arguments[0] is OpenApiParameter) + if (init == "true" && !parameter.IsRequired) { - 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"); - } + name += " = 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") diff --git a/src/LibKubernetesGenerator/GeneratorExecutionContextExt.cs b/src/LibKubernetesGenerator/GeneratorExecutionContextExt.cs index 85a097c86..de5975cc9 100644 --- a/src/LibKubernetesGenerator/GeneratorExecutionContextExt.cs +++ b/src/LibKubernetesGenerator/GeneratorExecutionContextExt.cs @@ -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)); } } diff --git a/src/LibKubernetesGenerator/INustacheHelper.cs b/src/LibKubernetesGenerator/INustacheHelper.cs deleted file mode 100644 index c5593a809..000000000 --- a/src/LibKubernetesGenerator/INustacheHelper.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace LibKubernetesGenerator -{ - public interface INustacheHelper - { - void RegisterHelper(); - } -} diff --git a/src/LibKubernetesGenerator/IScriptObjectHelper.cs b/src/LibKubernetesGenerator/IScriptObjectHelper.cs new file mode 100644 index 000000000..99dc9fe77 --- /dev/null +++ b/src/LibKubernetesGenerator/IScriptObjectHelper.cs @@ -0,0 +1,8 @@ +using Scriban.Runtime; + +namespace LibKubernetesGenerator; + +internal interface IScriptObjectHelper +{ + void RegisterHelper(ScriptObject scriptObject); +} diff --git a/src/LibKubernetesGenerator/KubernetesClientSourceGenerator.cs b/src/LibKubernetesGenerator/KubernetesClientSourceGenerator.cs index 0e74e7f87..8dd368e03 100644 --- a/src/LibKubernetesGenerator/KubernetesClientSourceGenerator.cs +++ b/src/LibKubernetesGenerator/KubernetesClientSourceGenerator.cs @@ -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)ch.GetValue(null)).Clear(); - } - - foreach (var helper in container.Resolve>()) - { - helper.RegisterHelper(); - } - return (swagger, container); } @@ -77,6 +61,9 @@ private static IContainer BuildContainer(OpenApiDocument swagger) .AsImplementedInterfaces() ; + builder.RegisterType() + ; + builder.RegisterType(); builder.RegisterType(); builder.RegisterType(); @@ -92,18 +79,14 @@ public void Initialize(IncrementalGeneratorInitializationContext generatorContex #if GENERATE_BASIC generatorContext.RegisterPostInitializationOutput(ctx => { - lock (Execlock) - { - var (swagger, container) = BuildContainer(); - - container.Resolve().Generate(swagger, ctx); + var (swagger, container) = BuildContainer(); - container.Resolve().Generate(swagger, ctx); - container.Resolve().Generate(swagger, ctx); - container.Resolve().Generate(swagger, ctx); + container.Resolve().Generate(swagger, ctx); - container.Resolve().Generate(swagger, ctx); - } + container.Resolve().Generate(swagger, ctx); + container.Resolve().Generate(swagger, ctx); + container.Resolve().Generate(swagger, ctx); + container.Resolve().Generate(swagger, ctx); }); #endif @@ -111,12 +94,8 @@ public void Initialize(IncrementalGeneratorInitializationContext generatorContex 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().Generate(swagger, ctx, srctree); - } + var (swagger, container) = BuildContainer(); + container.Resolve().Generate(swagger, ctx, srctree); }); #endif } diff --git a/src/LibKubernetesGenerator/LibKubernetesGenerator.target b/src/LibKubernetesGenerator/LibKubernetesGenerator.target index 6b789a976..e5ae80459 100644 --- a/src/LibKubernetesGenerator/LibKubernetesGenerator.target +++ b/src/LibKubernetesGenerator/LibKubernetesGenerator.target @@ -1,8 +1,8 @@ netstandard2.0 - CA1812 true + true @@ -16,15 +16,22 @@ - + + + + + + + + + + + - - - @@ -33,15 +40,16 @@ - + + + + + - - - diff --git a/src/LibKubernetesGenerator/MetaHelper.cs b/src/LibKubernetesGenerator/MetaHelper.cs index 285b62f40..72fdb462e 100644 --- a/src/LibKubernetesGenerator/MetaHelper.cs +++ b/src/LibKubernetesGenerator/MetaHelper.cs @@ -1,45 +1,27 @@ 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 arguments, IDictionary 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) { - var groupVersionKindElements = (object[])definition.ExtensionData["x-kubernetes-group-version-kind"]; - var groupVersionKind = (Dictionary)groupVersionKindElements[0]; - - return groupVersionKind["kind"] as string; - } + var groupVersionKindElements = (object[])definition.ExtensionData["x-kubernetes-group-version-kind"]; + var groupVersionKind = (Dictionary)groupVersionKindElements[0]; - public static void GetGroup(RenderContext context, IList arguments, IDictionary 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)); - } + return groupVersionKind["kind"] as string; } private static string GetGroup(JsonSchema definition) @@ -50,16 +32,6 @@ private static string GetGroup(JsonSchema definition) return groupVersionKind["group"] as string; } - public static void GetApiVersion(RenderContext context, IList arguments, - IDictionary 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 @@ private static string GetApiVersion(JsonSchema definition) return groupVersionKind["version"] as string; } - public static void GetPathExpression(RenderContext context, IList arguments, - IDictionary 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; diff --git a/src/LibKubernetesGenerator/ModelExtGenerator.cs b/src/LibKubernetesGenerator/ModelExtGenerator.cs index 39d3583ec..bbadcb5c0 100644 --- a/src/LibKubernetesGenerator/ModelExtGenerator.cs +++ b/src/LibKubernetesGenerator/ModelExtGenerator.cs @@ -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 @@ public void Generate(OpenApiDocument swagger, IncrementalGeneratorPostInitializa && 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"); } } } diff --git a/src/LibKubernetesGenerator/ModelGenerator.cs b/src/LibKubernetesGenerator/ModelGenerator.cs index 33be4d319..98b9576cd 100644 --- a/src/LibKubernetesGenerator/ModelGenerator.cs +++ b/src/LibKubernetesGenerator/ModelGenerator.cs @@ -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"); } } } diff --git a/src/LibKubernetesGenerator/ParamHelper.cs b/src/LibKubernetesGenerator/ParamHelper.cs index ffe6fed62..216c2f357 100644 --- a/src/LibKubernetesGenerator/ParamHelper.cs +++ b/src/LibKubernetesGenerator/ParamHelper.cs @@ -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,84 +17,31 @@ public ParamHelper(GeneralNameHelper generalNameHelper, TypeHelper typeHelper) 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(GetModelCtorParam)); + scriptObject.Import(nameof(IfParamContains), IfParamContains); } - public static void IfParamContains(RenderContext context, IList arguments, - IDictionary 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) - { - if (param.Name == name) - { - found = true; - break; - } - } - - if (found) - { - fn(null); - } - } - } + var found = false; - public static void IfParamDoesNotContain(RenderContext context, IList arguments, - IDictionary options, - RenderBlock fn, RenderBlock inverse) - { - var operation = arguments?.FirstOrDefault() as OpenApiOperation; - if (operation != null) + foreach (var param in operation.Parameters) { - string name = null; - if (arguments.Count > 1) + if (param.Name == name) { - 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); + found = true; + break; } } + + return found; } - public void GetModelCtorParam(RenderContext context, IList arguments, - IDictionary options, - RenderBlock fn, RenderBlock inverse) + public string GetModelCtorParam(JsonSchema schema) { - 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 @@ public void RegisterHelper() } return sp; - }))); - } + })); } } } diff --git a/src/LibKubernetesGenerator/PluralHelper.cs b/src/LibKubernetesGenerator/PluralHelper.cs index 26d939028..b2f697375 100644 --- a/src/LibKubernetesGenerator/PluralHelper.cs +++ b/src/LibKubernetesGenerator/PluralHelper.cs @@ -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 _classNameToPluralMap; private readonly ClassNameHelper classNameHelper; @@ -23,26 +23,9 @@ public PluralHelper(ClassNameHelper classNameHelper, OpenApiDocument swagger) _classNameToPluralMap = InitClassNameToPluralMap(swagger); } - public void RegisterHelper() + public void RegisterHelper(ScriptObject scriptObject) { - Helpers.Register(nameof(GetPlural), GetPlural); - } - - public void GetPlural(RenderContext context, IList arguments, IDictionary 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(GetPlural)); } public string GetPlural(JsonSchema definition) diff --git a/src/LibKubernetesGenerator/ScriptObjectFactory.cs b/src/LibKubernetesGenerator/ScriptObjectFactory.cs new file mode 100644 index 000000000..9e2fcd608 --- /dev/null +++ b/src/LibKubernetesGenerator/ScriptObjectFactory.cs @@ -0,0 +1,26 @@ +using Scriban.Runtime; +using System.Collections.Generic; +using System.Linq; + +namespace LibKubernetesGenerator; + +internal class ScriptObjectFactory +{ + private readonly List scriptObjectHelpers; + + public ScriptObjectFactory(IEnumerable scriptObjectHelpers) + { + this.scriptObjectHelpers = scriptObjectHelpers.ToList(); + } + + public ScriptObject CreateScriptObject() + { + var scriptObject = new ScriptObject(); + foreach (var helper in scriptObjectHelpers) + { + helper.RegisterHelper(scriptObject); + } + + return scriptObject; + } +} diff --git a/src/LibKubernetesGenerator/StringHelpers.cs b/src/LibKubernetesGenerator/StringHelpers.cs index 22e0466bb..7fd50eac5 100644 --- a/src/LibKubernetesGenerator/StringHelpers.cs +++ b/src/LibKubernetesGenerator/StringHelpers.cs @@ -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,42 +19,46 @@ public StringHelpers(GeneralNameHelper generalNameHelper) 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(ToXmlDoc)); + scriptObject.Import(nameof(ToInterpolationPathString), ToInterpolationPathString); + scriptObject.Import(nameof(IfGroupPathParamContainsGroup), IfGroupPathParamContainsGroup); } - private void ToXmlDoc(RenderContext context, IList arguments, IDictionary 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 ""; + } + + var first = true; + var sb = new StringBuilder(); - using (var reader = new StringReader(arguments[0] as string)) + using (var reader = new StringReader(arg)) + { + string line = null; + while ((line = reader.ReadLine()) != null) { - string line = null; - while ((line = reader.ReadLine()) != null) + foreach (var wline in WordWrap(line, 80)) { - foreach (var wline in WordWrap(line, 80)) + if (!first) { - if (!first) - { - context.Write("\n"); - context.Write(" /// "); - } - else - { - first = false; - } - - context.Write(SecurityElement.Escape(wline)); + sb.Append("\n"); + sb.Append(" /// "); + } + else + { + first = false; } + + sb.Append(SecurityElement.Escape(wline)); } } } + + return sb.ToString(); } private static IEnumerable WordWrap(string text, int width) @@ -91,24 +96,14 @@ private static IEnumerable WordWrap(string text, int width) } } - public void ToInterpolationPathString(RenderContext context, IList arguments, IDictionary 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 arguments, IDictionary 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}"); } } } diff --git a/src/LibKubernetesGenerator/TypeHelper.cs b/src/LibKubernetesGenerator/TypeHelper.cs index 0f38b5bf5..057a363a2 100644 --- a/src/LibKubernetesGenerator/TypeHelper.cs +++ b/src/LibKubernetesGenerator/TypeHelper.cs @@ -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 @@ public TypeHelper(ClassNameHelper classNameHelper) 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 arguments, - IDictionary 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(GetDotNetType)); + scriptObject.Import(nameof(GetDotNetTypeOpenApiParameter), new Func(GetDotNetTypeOpenApiParameter)); + scriptObject.Import(nameof(GetReturnType), new Func(GetReturnType)); + scriptObject.Import(nameof(IfReturnType), new Func(IfReturnType)); + scriptObject.Import(nameof(IfType), new Func(IfType)); } private string GetDotNetType(JsonObjectType jsonType, string name, bool required, string format) @@ -204,20 +158,21 @@ public string GetDotNetType(JsonSchemaProperty p) return GetDotNetType(p.Type, p.Name, p.IsRequired, p.Format); } - public void GetReturnType(RenderContext context, IList arguments, - IDictionary 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; - } - - context.Write(GetReturnType(operation, style)); + return classNameHelper.GetClassNameForSchemaDefinition(parameter.Schema.Reference); + } + 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 @@ string toType() return t; } - public void IfReturnType(RenderContext context, IList arguments, - IDictionary options, - RenderBlock fn, RenderBlock inverse) + public bool IfReturnType(OpenApiOperation operation, string type) { - var operation = arguments?.FirstOrDefault() as OpenApiOperation; - if (operation != null) + var rt = GetReturnType(operation, "void"); + if (type == "any" && rt != "void") { - string type = null; - if (arguments.Count > 1) - { - type = arguments[1] as string; - } - - var rt = GetReturnType(operation, "void"); - if (type == "any" && rt != "void") - { - fn(null); - } - else if (string.Equals(type, rt.ToLower(), StringComparison.OrdinalIgnoreCase)) - { - fn(null); - } - else if (type == "obj" && rt != "void" && rt != "Stream") - { - fn(null); - } + return true; + } + else if (string.Equals(type, rt.ToLower(), StringComparison.OrdinalIgnoreCase)) + { + return true; } + else if (type == "obj" && rt != "void" && rt != "Stream") + { + return true; + } + + return false; } - public static void IfType(RenderContext context, IList arguments, IDictionary options, - RenderBlock fn, RenderBlock inverse) + public static bool IfType(JsonSchemaProperty property, string type) { - var property = arguments?.FirstOrDefault() as JsonSchemaProperty; - if (property != null) + if (type == "object" && property.Reference != null && !property.IsArray && + property.AdditionalPropertiesSchema == null) { - string type = null; - if (arguments.Count > 1) - { - type = arguments[1] as string; - } - - if (type == "object" && property.Reference != null && !property.IsArray && - property.AdditionalPropertiesSchema == null) - { - fn(null); - } - else if (type == "objectarray" && property.IsArray && property.Item?.Reference != null) - { - fn(null); - } + return true; } + else if (type == "objectarray" && property.IsArray && property.Item?.Reference != null) + { + return true; + } + + return false; } } } diff --git a/src/LibKubernetesGenerator/UtilHelper.cs b/src/LibKubernetesGenerator/UtilHelper.cs index 25a2d18a3..e11e1acc0 100644 --- a/src/LibKubernetesGenerator/UtilHelper.cs +++ b/src/LibKubernetesGenerator/UtilHelper.cs @@ -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 arguments, IDictionary 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 arguments, IDictionary options, - RenderBlock fn, RenderBlock inverse) - { - var parameter = arguments?.FirstOrDefault() as ObservableCollection; - if (parameter?.Any() == true) - { - fn(null); - } + return false; } } } diff --git a/src/LibKubernetesGenerator/templates/AbstractKubernetes.cs.template b/src/LibKubernetesGenerator/templates/AbstractKubernetes.cs.template index c2a0ad86b..94a607608 100644 --- a/src/LibKubernetesGenerator/templates/AbstractKubernetes.cs.template +++ b/src/LibKubernetesGenerator/templates/AbstractKubernetes.cs.template @@ -10,7 +10,7 @@ namespace k8s; /// public abstract partial class AbstractKubernetes { - {{#.}} - public I{{.}}Operations {{.}} => this; - {{/.}} -} + {{for group in groups}} + public I{{group}}Operations {{group}} => this; + {{end}} +} diff --git a/src/LibKubernetesGenerator/templates/IBasicKubernetes.cs.template b/src/LibKubernetesGenerator/templates/IBasicKubernetes.cs.template index 9aa2c173c..99a284fcf 100644 --- a/src/LibKubernetesGenerator/templates/IBasicKubernetes.cs.template +++ b/src/LibKubernetesGenerator/templates/IBasicKubernetes.cs.template @@ -10,7 +10,7 @@ namespace k8s; /// public partial interface IBasicKubernetes { - {{#.}} - I{{.}}Operations {{.}} { get; } - {{/.}} + {{for group in groups}} + I{{group}}Operations {{group}} { get; } + {{end}} } \ No newline at end of file diff --git a/src/LibKubernetesGenerator/templates/IOperations.cs.template b/src/LibKubernetesGenerator/templates/IOperations.cs.template index ee3926a2e..c310acf9f 100644 --- a/src/LibKubernetesGenerator/templates/IOperations.cs.template +++ b/src/LibKubernetesGenerator/templates/IOperations.cs.template @@ -10,50 +10,50 @@ namespace k8s; /// public partial interface I{{name}}Operations { - {{#apis}} + {{for api in apis }} /// - /// {{ToXmlDoc operation.description}} + /// {{ToXmlDoc api.operation.description}} /// - {{#operation.parameters}} - /// - /// {{ToXmlDoc description}} + {{ for parameter in api.operation.parameters}} + /// + /// {{ToXmlDoc parameter.description}} /// - {{/operation.parameters}} + {{ end }} /// /// The headers that will be added to request. /// /// /// A which can be used to cancel the asynchronous operation. /// - Task"}}> {{GetMethodName operation "WithHttpMessagesAsync"}}( -{{#operation.parameters}} - {{GetDotNetType .}} {{GetDotNetName . "true"}}, -{{/operation.parameters}} + Task"}}> {{GetMethodName api.operation "WithHttpMessagesAsync"}}( +{{ for parameter in api.operation.parameters}} + {{GetDotNetTypeOpenApiParameter parameter}} {{GetDotNetNameOpenApiParameter parameter "true"}}, +{{ end }} IReadOnlyDictionary> customHeaders = null, CancellationToken cancellationToken = default); - {{#IfReturnType operation "object"}} + {{if IfReturnType api.operation "object"}} /// - /// {{ToXmlDoc operation.description}} + /// {{ToXmlDoc api.operation.description}} /// - {{#operation.parameters}} - /// - /// {{ToXmlDoc description}} + {{ for parameter in api.operation.parameters}} + /// + /// {{ToXmlDoc parameter.description}} /// - {{/operation.parameters}} + {{ end }} /// /// The headers that will be added to request. /// /// /// A which can be used to cancel the asynchronous operation. /// - Task> {{GetMethodName operation "WithHttpMessagesAsync"}}( -{{#operation.parameters}} - {{GetDotNetType .}} {{GetDotNetName . "true"}}, -{{/operation.parameters}} + Task> {{GetMethodName api.operation "WithHttpMessagesAsync"}}( +{{ for parameter in api.operation.parameters}} + {{GetDotNetTypeOpenApiParameter parameter}} {{GetDotNetNameOpenApiParameter parameter "true"}}, +{{ end }} IReadOnlyDictionary> customHeaders = null, CancellationToken cancellationToken = default); - {{/IfReturnType operation "object"}} + {{ end }} - {{/apis}} + {{ end }} } diff --git a/src/LibKubernetesGenerator/templates/Model.cs.template b/src/LibKubernetesGenerator/templates/Model.cs.template index 836940db2..f074ae72c 100644 --- a/src/LibKubernetesGenerator/templates/Model.cs.template +++ b/src/LibKubernetesGenerator/templates/Model.cs.template @@ -22,25 +22,26 @@ namespace k8s.Models /// /// Initializes a new instance of the {{GetClassName def}} class. /// - {{#properties}} - {{#isRequired}} - /// - /// {{ToXmlDoc description}} + {{ for property in properties }} + {{ if property.IsRequired }} + /// + /// {{ToXmlDoc property.description}} /// - {{/isRequired}} - {{/properties}} - {{#properties}} - {{^isRequired}} - /// - /// {{ToXmlDoc description}} + {{ end }} + {{ end }} + + {{ for property in properties }} + {{ if !property.IsRequired }} + /// + /// {{ToXmlDoc property.description}} /// - {{/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 /// partial void CustomInit(); - {{#properties}} + {{ for property in properties }} /// - /// {{ToXmlDoc description}} + /// {{ToXmlDoc property.description}} /// - [JsonPropertyName("{{name}}")] - public {{GetDotNetType .}} {{GetDotNetName name "field"}} { get; set; } - {{/properties}} + [JsonPropertyName("{{property.name}}")] + public {{GetDotNetType property}} {{GetDotNetName property.name "field"}} { get; set; } + {{ end }} /// /// Validate the object. @@ -65,29 +66,32 @@ namespace k8s.Models /// 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 }} } } } diff --git a/src/LibKubernetesGenerator/templates/ModelExtensions.cs.template b/src/LibKubernetesGenerator/templates/ModelExtensions.cs.template index 2878d09b3..bad5507bc 100644 --- a/src/LibKubernetesGenerator/templates/ModelExtensions.cs.template +++ b/src/LibKubernetesGenerator/templates/ModelExtensions.cs.template @@ -4,25 +4,24 @@ // 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 { } diff --git a/src/LibKubernetesGenerator/templates/Operations.cs.template b/src/LibKubernetesGenerator/templates/Operations.cs.template index 86d7d22ac..876ed8a16 100644 --- a/src/LibKubernetesGenerator/templates/Operations.cs.template +++ b/src/LibKubernetesGenerator/templates/Operations.cs.template @@ -8,139 +8,137 @@ namespace k8s; public partial class AbstractKubernetes : I{{name}}Operations { - {{#apis}} - {{#IfReturnType operation "void"}} - private async Task I{{name}}Operations_{{GetMethodName operation "WithHttpMessagesAsync"}}( - {{/IfReturnType operation "void"}} - {{#IfReturnType operation "obj"}} - private async Task> I{{name}}Operations_{{GetMethodName operation "WithHttpMessagesAsync"}}( - {{/IfReturnType operation "obj"}} - {{#IfReturnType operation "stream"}} - private async Task> 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 I{{name}}Operations_{{GetMethodName api.operation "WithHttpMessagesAsync"}}( + {{end}} + {{if IfReturnType api.operation "obj"}} + private async Task> I{{name}}Operations_{{GetMethodName api.operation "WithHttpMessagesAsync"}}( + {{end}} + {{if IfReturnType api.operation "stream"}} + private async Task> I{{name}}Operations_{{GetMethodName api.operation "WithHttpMessagesAsync"}}( + {{end}} +{{ for parameter in api.operation.parameters}} + {{GetDotNetTypeOpenApiParameter parameter}} {{GetDotNetNameOpenApiParameter parameter "false"}}, +{{end}} IReadOnlyDictionary> 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(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(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( 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() { Request = httpRequest, Response = httpResponse, Body = await httpResponse.Content.ReadAsStreamAsync().ConfigureAwait(false) }; - {{/IfReturnType operation "stream"}} + {{end}} return result; } /// - async Task"}}> I{{name}}Operations.{{GetMethodName operation "WithHttpMessagesAsync"}}( -{{#operation.parameters}} - {{GetDotNetType .}} {{GetDotNetName .}}, -{{/operation.parameters}} + async Task"}}> I{{name}}Operations.{{GetMethodName api.operation "WithHttpMessagesAsync"}}( +{{ for parameter in api.operation.parameters}} + {{GetDotNetTypeOpenApiParameter parameter}} {{GetDotNetNameOpenApiParameter parameter "false"}}, +{{end}} IReadOnlyDictionary> 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"}} /// - async Task> I{{name}}Operations.{{GetMethodName operation "WithHttpMessagesAsync"}}( -{{#operation.parameters}} - {{GetDotNetType .}} {{GetDotNetName .}}, -{{/operation.parameters}} + async Task> I{{name}}Operations.{{GetMethodName api.operation "WithHttpMessagesAsync"}}( +{{ for parameter in api.operation.parameters}} + {{GetDotNetTypeOpenApiParameter parameter}} {{GetDotNetNameOpenApiParameter parameter "false"}}, +{{end}} IReadOnlyDictionary> customHeaders, CancellationToken cancellationToken) { - return await I{{name}}Operations_{{GetMethodName operation "WithHttpMessagesAsync"}}( - {{#operation.parameters}} - {{GetDotNetName .}}, - {{/operation.parameters}} + 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 "object"}} - {{/apis}} + {{end}} + {{end}} } diff --git a/src/LibKubernetesGenerator/templates/OperationsExtensions.cs.template b/src/LibKubernetesGenerator/templates/OperationsExtensions.cs.template index f19500723..98c304ede 100644 --- a/src/LibKubernetesGenerator/templates/OperationsExtensions.cs.template +++ b/src/LibKubernetesGenerator/templates/OperationsExtensions.cs.template @@ -11,148 +11,148 @@ namespace k8s; /// public static partial class {{name}}OperationsExtensions { - {{#apis}} + {{for api in apis }} /// - /// {{ToXmlDoc operation.description}} + /// {{ToXmlDoc api.operation.description}} /// /// /// The operations group for this extension method. /// - {{#operation.parameters}} - /// - /// {{ToXmlDoc description}} + {{ for parameter in api.operation.parameters}} + /// + /// {{ToXmlDoc api.description}} /// - {{/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"}} /// - /// {{ToXmlDoc operation.description}} + /// {{ToXmlDoc api.operation.description}} /// /// /// The operations group for this extension method. /// - {{#operation.parameters}} - /// - /// {{ToXmlDoc description}} + {{ for parameter in api.operation.parameters}} + /// + /// {{ToXmlDoc parameter.description}} /// - {{/operation.parameters}} - public static T {{GetMethodName operation ""}}( + {{end}} + public static T {{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}} ) { - return operations.{{GetMethodName operation "Async"}}( -{{#operation.parameters}} - {{GetDotNetName .}}, -{{/operation.parameters}} + return operations.{{GetMethodName api.operation "Async"}}( +{{ for parameter in api.operation.parameters}} + {{GetDotNetNameOpenApiParameter parameter "false"}}, +{{end}} CancellationToken.None ).GetAwaiter().GetResult(); } - {{/IfReturnType operation "object"}} + {{end}} /// - /// {{ToXmlDoc operation.description}} + /// {{ToXmlDoc api.operation.description}} /// /// /// The operations group for this extension method. /// - {{#operation.parameters}} - /// - /// {{ToXmlDoc description}} + {{ for parameter in api.operation.parameters}} + /// + /// {{ToXmlDoc parameter.description}} /// - {{/operation.parameters}} + {{end}} /// /// A which can be used to cancel the asynchronous operation. /// - 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"}} /// - /// {{ToXmlDoc operation.description}} + /// {{ToXmlDoc api.operation.description}} /// /// /// The operations group for this extension method. /// - {{#operation.parameters}} - /// - /// {{ToXmlDoc description}} + {{ for parameter in api.operation.parameters}} + /// + /// {{ToXmlDoc parameter.description}} /// - {{/operation.parameters}} + {{end}} /// /// A which can be used to cancel the asynchronous operation. /// - public static async Task {{GetMethodName operation "Async"}}( + public static async Task {{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)) { - using (var _result = await operations.{{GetMethodName operation "WithHttpMessagesAsync"}}( - {{#operation.parameters}} - {{GetDotNetName .}}, - {{/operation.parameters}} + using (var _result = await operations.{{GetMethodName api.operation "WithHttpMessagesAsync"}}( +{{ for parameter in api.operation.parameters}} + {{GetDotNetNameOpenApiParameter parameter "false"}}, +{{end}} null, cancellationToken).ConfigureAwait(false)) { return _result.Body; } } - {{/IfReturnType}} + {{end}} - {{/apis}} + {{end}} }