2021-10-14 06:55:19 -07:00
|
|
|
using NJsonSchema;
|
|
|
|
|
using NSwag;
|
2024-04-15 10:43:05 -07:00
|
|
|
using Scriban.Runtime;
|
|
|
|
|
using System;
|
2022-09-28 22:34:32 +02:00
|
|
|
using System.Linq;
|
2025-10-12 06:10:53 +08:00
|
|
|
using System.Collections.Generic;
|
2021-10-14 06:55:19 -07:00
|
|
|
|
2021-12-22 17:16:44 -08:00
|
|
|
namespace LibKubernetesGenerator
|
2021-10-14 06:55:19 -07:00
|
|
|
{
|
2024-04-15 10:43:05 -07:00
|
|
|
internal class ParamHelper : IScriptObjectHelper
|
2021-10-14 06:55:19 -07:00
|
|
|
{
|
|
|
|
|
private readonly GeneralNameHelper generalNameHelper;
|
|
|
|
|
private readonly TypeHelper typeHelper;
|
|
|
|
|
|
|
|
|
|
public ParamHelper(GeneralNameHelper generalNameHelper, TypeHelper typeHelper)
|
|
|
|
|
{
|
|
|
|
|
this.generalNameHelper = generalNameHelper;
|
|
|
|
|
this.typeHelper = typeHelper;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-15 10:43:05 -07:00
|
|
|
public void RegisterHelper(ScriptObject scriptObject)
|
2021-10-14 06:55:19 -07:00
|
|
|
{
|
2024-04-15 10:43:05 -07:00
|
|
|
scriptObject.Import(nameof(GetModelCtorParam), new Func<JsonSchema, string>(GetModelCtorParam));
|
|
|
|
|
scriptObject.Import(nameof(IfParamContains), IfParamContains);
|
2025-10-12 06:10:53 +08:00
|
|
|
scriptObject.Import(nameof(FilterParameters), FilterParameters);
|
|
|
|
|
scriptObject.Import(nameof(GetParameterValueForWatch), new Func<OpenApiParameter, bool, string, string>(GetParameterValueForWatch));
|
2021-10-14 06:55:19 -07:00
|
|
|
}
|
|
|
|
|
|
2024-04-15 10:43:05 -07:00
|
|
|
public static bool IfParamContains(OpenApiOperation operation, string name)
|
2021-10-14 06:55:19 -07:00
|
|
|
{
|
2024-04-15 10:43:05 -07:00
|
|
|
var found = false;
|
2021-10-14 06:55:19 -07:00
|
|
|
|
2024-04-15 10:43:05 -07:00
|
|
|
foreach (var param in operation.Parameters)
|
2021-10-20 09:55:58 -04:00
|
|
|
{
|
2024-04-15 10:43:05 -07:00
|
|
|
if (param.Name == name)
|
2021-10-20 09:55:58 -04:00
|
|
|
{
|
2024-04-15 10:43:05 -07:00
|
|
|
found = true;
|
|
|
|
|
break;
|
2021-10-20 09:55:58 -04:00
|
|
|
}
|
|
|
|
|
}
|
2024-04-15 10:43:05 -07:00
|
|
|
|
|
|
|
|
return found;
|
2021-10-20 09:55:58 -04:00
|
|
|
}
|
2021-10-14 06:55:19 -07:00
|
|
|
|
2025-10-12 06:10:53 +08:00
|
|
|
public static IEnumerable<OpenApiParameter> FilterParameters(OpenApiOperation operation, string excludeParam)
|
|
|
|
|
{
|
|
|
|
|
return operation.Parameters.Where(p => p.Name != excludeParam);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string GetParameterValueForWatch(OpenApiParameter parameter, bool watch, string init = "false")
|
|
|
|
|
{
|
|
|
|
|
if (parameter.Name == "watch")
|
|
|
|
|
{
|
|
|
|
|
return watch ? "true" : "false";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return generalNameHelper.GetDotNetNameOpenApiParameter(parameter, init);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-15 10:43:05 -07:00
|
|
|
public string GetModelCtorParam(JsonSchema schema)
|
2021-10-14 06:55:19 -07:00
|
|
|
{
|
2024-04-15 10:43:05 -07:00
|
|
|
return string.Join(", ", schema.Properties.Values
|
2021-10-14 06:55:19 -07:00
|
|
|
.OrderBy(p => !p.IsRequired)
|
|
|
|
|
.Select(p =>
|
|
|
|
|
{
|
|
|
|
|
var sp =
|
|
|
|
|
$"{typeHelper.GetDotNetType(p)} {generalNameHelper.GetDotNetName(p.Name, "fieldctor")}";
|
|
|
|
|
|
|
|
|
|
if (!p.IsRequired)
|
|
|
|
|
{
|
|
|
|
|
sp = $"{sp} = null";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return sp;
|
2024-04-15 10:43:05 -07:00
|
|
|
}));
|
2021-10-14 06:55:19 -07:00
|
|
|
}
|
|
|
|
|
}
|
2025-10-12 06:10:53 +08:00
|
|
|
}
|