Files
csharp/src/LibKubernetesGenerator/ApiGenerator.cs
Manuel Menegazzo 3702fd6e90 Standardization of using order and object initialization (#1028)
* Code cleanup KubernetesClient

* KubernetesClient.Basic code cleanup

* KubernetesClient.Models cleanup

* LibKubernetesGenerator code cleanup

* Improved readability of object initialization

* FIx namespace order

* Fixed some compilation warning
2022-09-28 13:34:32 -07:00

64 lines
2.2 KiB
C#

using CaseExtensions;
using Microsoft.CodeAnalysis;
using NSwag;
using System.Collections.Generic;
using System.Linq;
namespace LibKubernetesGenerator
{
internal class ApiGenerator
{
public void Generate(OpenApiDocument swagger, GeneratorExecutionContext context)
{
var data = swagger.Operations
.Where(o => o.Method != OpenApiOperationMethod.Options)
.Select(o =>
{
var ps = o.Operation.ActualParameters.OrderBy(p => !p.IsRequired).ToArray();
o.Operation.Parameters.Clear();
var name = new HashSet<string>();
var i = 1;
foreach (var p in ps)
{
if (name.Contains(p.Name))
{
p.Name = p.Name + i++;
}
o.Operation.Parameters.Add(p);
name.Add(p.Name);
}
return o;
})
.Select(o =>
{
o.Path = o.Path.TrimStart('/');
o.Method = char.ToUpper(o.Method[0]) + o.Method.Substring(1);
return o;
})
.ToArray();
var groups = new List<string>();
foreach (var grouped in data.GroupBy(d => d.Operation.Tags.First()))
{
var name = grouped.Key.ToPascalCase();
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");
}
context.RenderToContext($"IBasicKubernetes.cs.template", groups, $"IBasicKubernetes.g.cs");
context.RenderToContext($"AbstractKubernetes.cs.template", groups, $"AbstractKubernetes.g.cs");
}
}
}