From a7cb9bab5d713e07b8baa66ca997b159d96f078e Mon Sep 17 00:00:00 2001 From: Gilderic DERUETTE Date: Thu, 28 Sep 2023 10:43:56 +0200 Subject: [PATCH] =?UTF-8?q?[Core]=20R=C3=A9solution=20des=20param=C3=A8tre?= =?UTF-8?q?s=20avant=20les=20variables=20dans=20les=20templates?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TopModel.Generator.Core/TemplateExtensions.cs | 59 ++++++++++--------- 1 file changed, 32 insertions(+), 27 deletions(-) diff --git a/TopModel.Generator.Core/TemplateExtensions.cs b/TopModel.Generator.Core/TemplateExtensions.cs index 69c1549d..2c828ee2 100644 --- a/TopModel.Generator.Core/TemplateExtensions.cs +++ b/TopModel.Generator.Core/TemplateExtensions.cs @@ -164,6 +164,11 @@ private static string ResolveVariable(this string input, IFieldProperty fp, stri return string.Empty; } + for (var i = 0; i < parameters.Length; i++) + { + input = input.Replace($"${i}", parameters[i]); + } + if (input.StartsWith("parent.")) { return ResolveVariable(input["parent.".Length..], fp.Parent, parameters, config, tag); @@ -202,14 +207,9 @@ private static string ResolveVariable(this string input, IFieldProperty fp, stri "resourceKey" => transform(fp.ResourceKey.ToString()), "commentResourceKey" => transform(fp.CommentResourceKey.ToString()), "defaultValue" => transform(fp.DefaultValue?.ToString() ?? string.Empty), - var i => config.ResolveVariables(config.ResolveGlobalVariables($@"{{{i}}}").Trim('{').Trim('}'), module: fp.Parent.Namespace.Module, tag: tag) + var i => config.ResolveVariables(config.ResolveGlobalVariables($@"{{{i}}}").Trim('{', '}'), module: fp.Parent.Namespace.Module, tag: tag) }; - for (var i = 0; i < parameters.Length; i++) - { - result = result.Replace($"${i}", parameters[i]); - } - return result; } @@ -220,6 +220,11 @@ private static string ResolveVariable(this string input, CompositionProperty cp, return string.Empty; } + for (var i = 0; i < parameters.Length; i++) + { + input = input.Replace($"${i}", parameters[i]); + } + if (input.StartsWith("parent.")) { return ResolveVariable(input["parent.".Length..], cp.Class, parameters, config, tag); @@ -246,14 +251,9 @@ private static string ResolveVariable(this string input, CompositionProperty cp, "name" => transform(cp.Name ?? string.Empty), "label" => transform(cp.Label ?? string.Empty), "comment" => transform(cp.Comment), - var i => config.ResolveVariables(config.ResolveGlobalVariables($@"{{{i}}}").Trim('{').Trim('}'), module: cp.Class.Namespace.Module, tag: tag) + var i => config.ResolveVariables(config.ResolveGlobalVariables($@"{{{i}}}").Trim('{', '}'), module: cp.Class.Namespace.Module, tag: tag) }; - for (var i = 0; i < parameters.Length; i++) - { - result = result.Replace($"${i}", parameters[i]); - } - return result; } @@ -264,6 +264,11 @@ private static string ResolveVariable(this string input, Class c, string[] param return string.Empty; } + for (var i = 0; i < parameters.Length; i++) + { + input = input.Replace($"${i}", parameters[i]); + } + if (input.StartsWith("primaryKey.")) { if (c.PrimaryKey.FirstOrDefault() == null) @@ -304,19 +309,24 @@ private static string ResolveVariable(this string input, Class c, string[] param "label" => transform(c.Label ?? string.Empty), "pluralName" => transform(c.PluralName ?? string.Empty), "module" => transform(c.Namespace.Module ?? string.Empty), - var i => config.ResolveVariables(config.ResolveGlobalVariables($@"{{{i}}}").Trim('{').Trim('}'), module: c.Namespace.Module, tag: tag) + var i => config.ResolveVariables(config.ResolveGlobalVariables($@"{{{i}}}").Trim('{', '}'), module: c.Namespace.Module, tag: tag) }; - for (var i = 0; i < parameters.Length; i++) - { - result = result.Replace($"${i}", parameters[i]); - } - return result; } private static string ResolveVariable(this string input, Endpoint e, string[] parameters, GeneratorConfigBase config, string? tag = null) { + if (input == null || input.Length == 0) + { + return string.Empty; + } + + for (var i = 0; i < parameters.Length; i++) + { + input = input.Replace($"${i}", parameters[i]); + } + if (input.StartsWith("returns.")) { if (e.Returns == null) @@ -355,14 +365,9 @@ private static string ResolveVariable(this string input, Endpoint e, string[] pa "route" => transform(e.Route), "description" => transform(e.Description), "module" => transform(e.Namespace.Module ?? string.Empty), - var i => config.ResolveVariables(config.ResolveGlobalVariables($@"{{{i}}}").Trim('{').Trim('}'), module: e.Namespace.Module, tag: tag) + var i => config.ResolveVariables(config.ResolveGlobalVariables($@"{{{i}}}").Trim('{', '}'), module: e.Namespace.Module, tag: tag) }; - for (var i = 0; i < parameters.Length; i++) - { - result = result.Replace($"${i}", parameters[i]); - } - return result; } @@ -378,7 +383,7 @@ private static string ResolveVariable(this string input, Domain domain, string t "scale" => transform(domain.Scale?.ToString() ?? string.Empty), "name" => transform(domain.Name ?? string.Empty), "type" => transform(domain.Implementations.GetValueOrDefault(targetLanguage)?.Type ?? string.Empty), - var i => config.ResolveVariables(config.ResolveGlobalVariables($@"{{{i}}}").Trim('{').Trim('}'), tag: tag) + var i => config.ResolveVariables(config.ResolveGlobalVariables($@"{{{i}}}").Trim('{', '}'), tag: tag) }; } @@ -386,11 +391,11 @@ private static string ResolveVariable(this string input, Domain domainFrom, Doma { if (input.StartsWith("from.")) { - return ResolveVariable(input.Split("from.")[1], domainFrom, targetLanguage, config, tag); + return ResolveVariable(input["from.".Length..], domainFrom, targetLanguage, config, tag); } else { - return ResolveVariable(input.Split("to.")[1], domainTo, targetLanguage, config, tag); + return ResolveVariable(input["to.".Length..], domainTo, targetLanguage, config, tag); } } } \ No newline at end of file