From 562f8918b47415feec7d6398c304a8d4665708f5 Mon Sep 17 00:00:00 2001 From: Gilderic DERUETTE Date: Wed, 27 Sep 2023 19:43:29 +0200 Subject: [PATCH] =?UTF-8?q?[Core]=20R=C3=A9soudre=20en=20cascade=20les=20t?= =?UTF-8?q?emplate=20Fixes=20#310?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TopModel.Generator.Core/TemplateExtensions.cs | 138 +++++++++++++++--- .../SpringClientApiGenerator.cs | 21 --- .../SpringServerApiGenerator.cs | 21 --- 3 files changed, 121 insertions(+), 59 deletions(-) diff --git a/TopModel.Generator.Core/TemplateExtensions.cs b/TopModel.Generator.Core/TemplateExtensions.cs index c136e796..0f9391f0 100644 --- a/TopModel.Generator.Core/TemplateExtensions.cs +++ b/TopModel.Generator.Core/TemplateExtensions.cs @@ -132,17 +132,41 @@ public static string ParseTemplate(this string template, Domain domainFrom, Doma private static IEnumerable ExtractVariables(this string input) { - var regex = new Regex(@"(\{[$a-zA-Z0-9:.]+\})"); + var regex = new Regex(@"(\{[$a-zA-Z0-9:.\[\]]+\})"); return regex.Matches(input).Cast(); } + private static string ResolveVariable(this string input, IPropertyContainer container, string[] parameters) + { + switch (container) + { + case Endpoint e: + return ResolveVariable(input, e, parameters); + case Class c: return ResolveVariable(input, c, parameters); + default: return string.Empty; + } + } + private static string ResolveVariable(this string input, IFieldProperty rp, string[] parameters) { + if (input == null || input.Length == 0) + { + return string.Empty; + } + + if (input.StartsWith("parent.")) + { + return ResolveVariable(input["parent.".Length..], rp.Parent, parameters); + } + + if (input.StartsWith("association.") && rp is AssociationProperty ap) + { + return ResolveVariable(input["association.".Length..], ap.Association, parameters); + } + var transform = input.GetTransformation(); var result = input.Split(':').First() switch { - "class.name" => transform(rp.Class?.Name.ToString() ?? string.Empty), - "class.sqlName" => transform(rp.Class?.SqlName ?? string.Empty), "name" => transform(rp.Name ?? string.Empty), "sqlName" => transform(rp.SqlName ?? string.Empty), "trigram" => transform(rp.Trigram ?? rp.Class?.Trigram ?? string.Empty), @@ -165,11 +189,24 @@ private static string ResolveVariable(this string input, IFieldProperty rp, stri private static string ResolveVariable(this string input, CompositionProperty cp, string[] parameters) { + if (input == null || input.Length == 0) + { + return string.Empty; + } + + if (input.StartsWith("parent.")) + { + return ResolveVariable(input["parent.".Length..], cp.Class, parameters); + } + + if (input.StartsWith("composition.")) + { + return ResolveVariable(input["composition.".Length..], cp.Composition, parameters); + } + var transform = input.GetTransformation(); var result = input.Split(':').First() switch { - "class.name" => transform(cp.Class?.Name.ToString() ?? string.Empty), - "composition.name" => transform(cp.Composition?.Name.ToString() ?? string.Empty), "name" => transform(cp.Name ?? string.Empty), "label" => transform(cp.Label ?? string.Empty), "comment" => transform(cp.Comment), @@ -184,12 +221,49 @@ private static string ResolveVariable(this string input, CompositionProperty cp, return result; } + private static string ResolveVariable(this string input, IProperty c, string[] parameters) + { + switch (c) + { + case IFieldProperty fp: return ResolveVariable(input, fp, parameters); + case CompositionProperty cp: return ResolveVariable(input, cp, parameters); + default: return string.Empty; + } + } + private static string ResolveVariable(this string input, Class c, string[] parameters) { + if (input == null || input.Length == 0) + { + return string.Empty; + } + + if (input.StartsWith("primaryKey.")) + { + if (c.PrimaryKey.FirstOrDefault() == null) + { + return string.Empty; + } + + return ResolveVariable(input["primaryKey.".Length..], c.PrimaryKey.FirstOrDefault()!, parameters); + } + + if (input.StartsWith("properties[")) + { + var indexSize = input["properties[".Length..].IndexOf("]"); + var index = int.Parse(input.Split("properties[")[1].Split("]")[0]); + var nextInput = input[("properties[].".Length + indexSize)..]; + if (c.Properties.Count < index) + { + return string.Empty; + } + + return ResolveVariable(nextInput, c.Properties[index], parameters); + } + var transform = input.GetTransformation(); var result = input.Split(':').First() switch { - "primaryKey.name" => transform(c.PrimaryKey.FirstOrDefault()?.Name ?? string.Empty), "trigram" => transform(c.Trigram), "name" => transform(c.Name), "sqlName" => transform(c.SqlName), @@ -210,6 +284,29 @@ private static string ResolveVariable(this string input, Class c, string[] param private static string ResolveVariable(this string input, Endpoint e, string[] parameters) { + if (input.StartsWith("returns.")) + { + if (e.Returns == null) + { + return string.Empty; + } + + return ResolveVariable(input["returns.".Length..], e.Returns, parameters); + } + + if (input.StartsWith("params[")) + { + var indexSize = input["params[".Length..].IndexOf("]"); + var index = int.Parse(input.Split("params[")[1].Split("]")[0]); + var nextInput = input[("params[].".Length + indexSize)..]; + if (e.Params.Count < index) + { + return string.Empty; + } + + return ResolveVariable(nextInput, e.Params[index], parameters); + } + var transform = input.GetTransformation(); var result = input.Split(':').First() switch { @@ -229,24 +326,31 @@ private static string ResolveVariable(this string input, Endpoint e, string[] pa return result; } - private static string ResolveVariable(this string input, Domain domainFrom, Domain domainTo, string targetLanguage) + private static string ResolveVariable(this string input, Domain domain, string targetLanguage) { var transform = input.GetTransformation(); var variable = input.Split(':').First(); return input.Split(':').First() switch { - "from.mediaType" => transform(domainFrom.MediaType ?? string.Empty), - "from.length" => transform(domainFrom.Length?.ToString() ?? string.Empty), - "from.scale" => transform(domainFrom.Scale?.ToString() ?? string.Empty), - "from.name" => transform(domainFrom.Name ?? string.Empty), - "from.type" => transform(domainFrom.Implementations.GetValueOrDefault(targetLanguage)?.Type ?? string.Empty), - "to.mediaType" => transform(domainTo.MediaType ?? string.Empty), - "to.length" => transform(domainTo.Length?.ToString() ?? string.Empty), - "to.scale" => transform(domainTo.Scale?.ToString() ?? string.Empty), - "to.name" => transform(domainTo.Name ?? string.Empty), - "to.type" => transform(domainTo.Implementations.GetValueOrDefault(targetLanguage)?.Type ?? string.Empty), + "mediaType" => transform(domain.MediaType ?? string.Empty), + "length" => transform(domain.Length?.ToString() ?? string.Empty), + "scale" => transform(domain.Scale?.ToString() ?? string.Empty), + "name" => transform(domain.Name ?? string.Empty), + "type" => transform(domain.Implementations.GetValueOrDefault(targetLanguage)?.Type ?? string.Empty), var i => i }; } + + private static string ResolveVariable(this string input, Domain domainFrom, Domain domainTo, string targetLanguage) + { + if (input.StartsWith("from.")) + { + return ResolveVariable(input.Split("from.")[1], domainFrom, targetLanguage); + } + else + { + return ResolveVariable(input.Split("to.")[1], domainTo, targetLanguage); + } + } } \ No newline at end of file diff --git a/TopModel.Generator.Jpa/SpringClientApiGenerator.cs b/TopModel.Generator.Jpa/SpringClientApiGenerator.cs index 7cd68df3..7b6b4899 100644 --- a/TopModel.Generator.Jpa/SpringClientApiGenerator.cs +++ b/TopModel.Generator.Jpa/SpringClientApiGenerator.cs @@ -33,11 +33,6 @@ protected override string GetFilePath(ModelFile file, string tag) protected override void HandleFile(string filePath, string fileName, string tag, IList endpoints) { - foreach (var endpoint in endpoints) - { - CheckEndpoint(endpoint); - } - var className = GetClassName(fileName); var packageName = Config.GetPackageName(endpoints.First(), tag); using var fw = new JavaWriter(filePath, _logger, packageName, null); @@ -79,22 +74,6 @@ protected override void HandleFile(string filePath, string fileName, string tag, fw.WriteLine("}"); } - private static void CheckEndpoint(Endpoint endpoint) - { - foreach (var q in endpoint.GetQueryParams().Concat(endpoint.GetRouteParams())) - { - if (q is AssociationProperty ap) - { - throw new ModelException(endpoint, $"Le endpoint {endpoint.Route} ne peut pas contenir d'association"); - } - } - - if (endpoint.Returns != null && endpoint.Returns is AssociationProperty) - { - throw new ModelException(endpoint, $"Le retour du endpoint {endpoint.Route} ne peut pas ĂȘtre une association"); - } - } - private static string GetClassName(string fileName) { return $"Abstract{fileName.ToPascalCase()}Client"; diff --git a/TopModel.Generator.Jpa/SpringServerApiGenerator.cs b/TopModel.Generator.Jpa/SpringServerApiGenerator.cs index 7fe1d6ce..848d48c7 100644 --- a/TopModel.Generator.Jpa/SpringServerApiGenerator.cs +++ b/TopModel.Generator.Jpa/SpringServerApiGenerator.cs @@ -33,11 +33,6 @@ protected override string GetFilePath(ModelFile file, string tag) protected override void HandleFile(string filePath, string fileName, string tag, IList endpoints) { - foreach (var endpoint in endpoints) - { - CheckEndpoint(endpoint); - } - var className = GetClassName(fileName); var packageName = Config.GetPackageName(endpoints.First(), tag); using var fw = new JavaWriter(filePath, _logger, packageName, null); @@ -65,22 +60,6 @@ protected override void HandleFile(string filePath, string fileName, string tag, fw.WriteLine("}"); } - private static void CheckEndpoint(Endpoint endpoint) - { - foreach (var q in endpoint.GetQueryParams().Concat(endpoint.GetRouteParams())) - { - if (q is AssociationProperty ap) - { - throw new ModelException(endpoint, $"Le endpoint {endpoint.Route} ne peut pas contenir d'association"); - } - } - - if (endpoint.Returns != null && endpoint.Returns is AssociationProperty) - { - throw new ModelException(endpoint, $"Le retour du endpoint {endpoint.Route} ne peut pas ĂȘtre une association"); - } - } - private static string GetClassName(string fileName) { return $"{fileName.ToPascalCase()}Controller";