|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Linq; |
| 3 | +using CaseExtensions; |
| 4 | +using NJsonSchema; |
| 5 | +using NSwag; |
| 6 | +using Nustache.Core; |
| 7 | + |
| 8 | +namespace KubernetesGenerator |
| 9 | +{ |
| 10 | + public class ClassNameHelper : INustacheHelper |
| 11 | + { |
| 12 | + private readonly Dictionary<string, string> classNameMap; |
| 13 | + private readonly HashSet<string> schemaDefinitionsInMultipleGroups; |
| 14 | + private readonly Dictionary<JsonSchema4, string> schemaToNameMapCooked; |
| 15 | + private readonly Dictionary<JsonSchema4, string> schemaToNameMapUnprocessed; |
| 16 | + |
| 17 | + public ClassNameHelper(SwaggerDocument swaggerCooked, SwaggerDocument swaggerUnprocessed) |
| 18 | + { |
| 19 | + classNameMap = InitClassNameMap(swaggerCooked); |
| 20 | + |
| 21 | + schemaToNameMapCooked = GenerateSchemaToNameMapCooked(swaggerCooked); |
| 22 | + schemaToNameMapUnprocessed = GenerateSchemaToNameMapUnprocessed(swaggerUnprocessed); |
| 23 | + schemaDefinitionsInMultipleGroups = InitSchemaDefinitionsInMultipleGroups(schemaToNameMapUnprocessed); |
| 24 | + } |
| 25 | + |
| 26 | + public void RegisterHelper() |
| 27 | + { |
| 28 | + Helpers.Register(nameof(GetClassName), GetClassName); |
| 29 | + } |
| 30 | + |
| 31 | + private static Dictionary<JsonSchema4, string> GenerateSchemaToNameMapUnprocessed( |
| 32 | + SwaggerDocument swaggerUnprocessed) |
| 33 | + { |
| 34 | + return swaggerUnprocessed.Definitions.ToDictionary(x => x.Value, x => x.Key); |
| 35 | + } |
| 36 | + |
| 37 | + private static Dictionary<JsonSchema4, string> GenerateSchemaToNameMapCooked(SwaggerDocument swaggerCooked) |
| 38 | + { |
| 39 | + return swaggerCooked.Definitions.ToDictionary(x => x.Value, x => x.Key.Replace(".", "").ToPascalCase()); |
| 40 | + } |
| 41 | + |
| 42 | + private static HashSet<string> InitSchemaDefinitionsInMultipleGroups( |
| 43 | + Dictionary<JsonSchema4, string> schemaToNameMap) |
| 44 | + { |
| 45 | + return schemaToNameMap.Values.Select(x => |
| 46 | + { |
| 47 | + var parts = x.Split("."); |
| 48 | + return new |
| 49 | + { |
| 50 | + FullName = x, |
| 51 | + Name = parts[parts.Length - 1], |
| 52 | + Version = parts[parts.Length - 2], |
| 53 | + Group = parts[parts.Length - 3], |
| 54 | + }; |
| 55 | + }) |
| 56 | + .GroupBy(x => new { x.Name, x.Version }) |
| 57 | + .Where(x => x.Count() > 1) |
| 58 | + .SelectMany(x => x) |
| 59 | + .Select(x => x.FullName) |
| 60 | + .ToHashSet(); |
| 61 | + } |
| 62 | + |
| 63 | + private Dictionary<string, string> InitClassNameMap(SwaggerDocument doc) |
| 64 | + { |
| 65 | + var map = new Dictionary<string, string>(); |
| 66 | + foreach (var (k, v) in doc.Definitions) |
| 67 | + { |
| 68 | + if (v.ExtensionData?.TryGetValue("x-kubernetes-group-version-kind", out _) == true) |
| 69 | + { |
| 70 | + var groupVersionKindElements = (object[])v.ExtensionData["x-kubernetes-group-version-kind"]; |
| 71 | + var groupVersionKind = (Dictionary<string, object>)groupVersionKindElements[0]; |
| 72 | + |
| 73 | + var group = (string)groupVersionKind["group"]; |
| 74 | + var kind = (string)groupVersionKind["kind"]; |
| 75 | + var version = (string)groupVersionKind["version"]; |
| 76 | + map[$"{group}_{kind}_{version}"] = k.Replace(".", "").ToPascalCase(); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + return map; |
| 81 | + } |
| 82 | + |
| 83 | + public void GetClassName(RenderContext context, IList<object> arguments, IDictionary<string, object> options, |
| 84 | + RenderBlock fn, RenderBlock inverse) |
| 85 | + { |
| 86 | + if (arguments != null && arguments.Count > 0 && arguments[0] != null && arguments[0] is SwaggerOperation) |
| 87 | + { |
| 88 | + context.Write(GetClassName(arguments[0] as SwaggerOperation)); |
| 89 | + } |
| 90 | + else if (arguments != null && arguments.Count > 0 && arguments[0] != null && arguments[0] is JsonSchema4) |
| 91 | + { |
| 92 | + context.Write(GetClassNameForSchemaDefinition(arguments[0] as JsonSchema4)); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + public string GetClassName(SwaggerOperation operation) |
| 97 | + { |
| 98 | + var groupVersionKind = |
| 99 | + (Dictionary<string, object>)operation.ExtensionData["x-kubernetes-group-version-kind"]; |
| 100 | + return GetClassName(groupVersionKind); |
| 101 | + } |
| 102 | + |
| 103 | + public string GetClassName(Dictionary<string, object> groupVersionKind) |
| 104 | + { |
| 105 | + var group = (string)groupVersionKind["group"]; |
| 106 | + var kind = (string)groupVersionKind["kind"]; |
| 107 | + var version = (string)groupVersionKind["version"]; |
| 108 | + |
| 109 | + return classNameMap[$"{group}_{kind}_{version}"]; |
| 110 | + } |
| 111 | + |
| 112 | + public string GetClassName(JsonSchema4 definition) |
| 113 | + { |
| 114 | + var groupVersionKindElements = (object[])definition.ExtensionData["x-kubernetes-group-version-kind"]; |
| 115 | + var groupVersionKind = (Dictionary<string, object>)groupVersionKindElements[0]; |
| 116 | + |
| 117 | + return GetClassName(groupVersionKind); |
| 118 | + } |
| 119 | + |
| 120 | + public string GetClassNameForSchemaDefinition(JsonSchema4 definition) |
| 121 | + { |
| 122 | + if (definition.ExtensionData != null && |
| 123 | + definition.ExtensionData.ContainsKey("x-kubernetes-group-version-kind")) |
| 124 | + { |
| 125 | + return GetClassName(definition); |
| 126 | + } |
| 127 | + |
| 128 | + if (schemaToNameMapCooked.TryGetValue(definition, out var name)) |
| 129 | + { |
| 130 | + return name; |
| 131 | + } |
| 132 | + |
| 133 | + var schemaName = schemaToNameMapUnprocessed[definition]; |
| 134 | + |
| 135 | + var parts = schemaName.Split("."); |
| 136 | + var group = parts[parts.Length - 3]; |
| 137 | + var version = parts[parts.Length - 2]; |
| 138 | + var entityName = parts[parts.Length - 1]; |
| 139 | + if (!schemaDefinitionsInMultipleGroups.Contains(schemaName)) |
| 140 | + { |
| 141 | + group = null; |
| 142 | + } |
| 143 | + |
| 144 | + return $"{group}{version}{entityName}".ToPascalCase(); |
| 145 | + } |
| 146 | + |
| 147 | + private static Dictionary<JsonSchema4, string> InitSchemaToNameCooked(SwaggerDocument swaggercooked) |
| 148 | + { |
| 149 | + return swaggercooked.Definitions.ToDictionary(x => x.Value, x => x.Key.Replace(".", "").ToPascalCase()); |
| 150 | + } |
| 151 | + } |
| 152 | +} |
0 commit comments