Skip to content

Commit

Permalink
[Core] Résolution des paramètres avant les variables dans les templates
Browse files Browse the repository at this point in the history
  • Loading branch information
gideruette committed Sep 28, 2023
1 parent 0dd978e commit a7cb9ba
Showing 1 changed file with 32 additions and 27 deletions.
59 changes: 32 additions & 27 deletions TopModel.Generator.Core/TemplateExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}

Expand All @@ -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);
Expand All @@ -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;
}

Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}

Expand All @@ -378,19 +383,19 @@ 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)
};
}

private static string ResolveVariable(this string input, Domain domainFrom, Domain domainTo, string targetLanguage, GeneratorConfigBase config, string? tag = null)
{
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);
}
}
}

0 comments on commit a7cb9ba

Please sign in to comment.