2025-09-22 14:20:13 -07:00
|
|
|
using System.Collections.Generic;
|
2021-12-22 17:16:44 -08:00
|
|
|
using Microsoft.CodeAnalysis;
|
2021-10-14 06:55:19 -07:00
|
|
|
using NSwag;
|
|
|
|
|
|
2021-12-22 17:16:44 -08:00
|
|
|
namespace LibKubernetesGenerator
|
2021-10-14 06:55:19 -07:00
|
|
|
{
|
2021-10-24 08:14:22 -07:00
|
|
|
internal class ModelGenerator
|
2021-10-14 06:55:19 -07:00
|
|
|
{
|
|
|
|
|
private readonly ClassNameHelper classNameHelper;
|
2024-04-15 10:43:05 -07:00
|
|
|
private readonly ScriptObjectFactory scriptObjectFactory;
|
2021-10-14 06:55:19 -07:00
|
|
|
|
2024-04-15 10:43:05 -07:00
|
|
|
public ModelGenerator(ClassNameHelper classNameHelper, ScriptObjectFactory scriptObjectFactory)
|
2021-10-14 06:55:19 -07:00
|
|
|
{
|
|
|
|
|
this.classNameHelper = classNameHelper;
|
2024-04-15 10:43:05 -07:00
|
|
|
this.scriptObjectFactory = scriptObjectFactory;
|
2021-10-14 06:55:19 -07:00
|
|
|
}
|
|
|
|
|
|
2023-10-10 09:49:47 -07:00
|
|
|
public void Generate(OpenApiDocument swagger, IncrementalGeneratorPostInitializationContext context)
|
2021-10-14 06:55:19 -07:00
|
|
|
{
|
2024-04-15 10:43:05 -07:00
|
|
|
var sc = scriptObjectFactory.CreateScriptObject();
|
|
|
|
|
|
2025-09-22 14:20:13 -07:00
|
|
|
var genSkippedTypes = new HashSet<string>
|
|
|
|
|
{
|
|
|
|
|
"IntOrString",
|
|
|
|
|
"ResourceQuantity",
|
|
|
|
|
"V1Patch",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var extSkippedTypes = new HashSet<string>
|
|
|
|
|
{
|
|
|
|
|
"V1WatchEvent",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var typeOverrides = new Dictionary<string, string>
|
|
|
|
|
{
|
|
|
|
|
// not used at the moment
|
|
|
|
|
};
|
2024-04-15 10:43:05 -07:00
|
|
|
|
2021-12-22 17:16:44 -08:00
|
|
|
foreach (var kv in swagger.Definitions)
|
2021-10-14 06:55:19 -07:00
|
|
|
{
|
2021-12-22 17:16:44 -08:00
|
|
|
var def = kv.Value;
|
2021-10-14 06:55:19 -07:00
|
|
|
var clz = classNameHelper.GetClassNameForSchemaDefinition(def);
|
2024-04-15 10:43:05 -07:00
|
|
|
|
2025-09-22 14:20:13 -07:00
|
|
|
if (genSkippedTypes.Contains(clz))
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var hasExt = def.ExtensionData != null
|
|
|
|
|
&& def.ExtensionData.ContainsKey("x-kubernetes-group-version-kind")
|
|
|
|
|
&& !extSkippedTypes.Contains(clz);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var typ = "record";
|
|
|
|
|
if (typeOverrides.TryGetValue(clz, out var to))
|
|
|
|
|
{
|
|
|
|
|
typ = to;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-15 10:43:05 -07:00
|
|
|
sc.SetValue("clz", clz, true);
|
|
|
|
|
sc.SetValue("def", def, true);
|
|
|
|
|
sc.SetValue("properties", def.Properties.Values, true);
|
2025-09-22 14:20:13 -07:00
|
|
|
sc.SetValue("typ", typ, true);
|
|
|
|
|
sc.SetValue("hasExt", hasExt, true);
|
2024-04-15 10:43:05 -07:00
|
|
|
|
|
|
|
|
context.RenderToContext("Model.cs.template", sc, $"Models_{clz}.g.cs");
|
2021-10-14 06:55:19 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|