Skip to content

Commit

Permalink
[Core] Résoudre en cascade les template Fixes #310
Browse files Browse the repository at this point in the history
  • Loading branch information
gideruette committed Sep 27, 2023
1 parent 19f7399 commit 562f891
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 59 deletions.
138 changes: 121 additions & 17 deletions TopModel.Generator.Core/TemplateExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,17 +132,41 @@ public static string ParseTemplate(this string template, Domain domainFrom, Doma

private static IEnumerable<Match> 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<Match>();
}

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),
Expand All @@ -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),
Expand All @@ -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),
Expand All @@ -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
{
Expand All @@ -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);
}
}
}
21 changes: 0 additions & 21 deletions TopModel.Generator.Jpa/SpringClientApiGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ protected override string GetFilePath(ModelFile file, string tag)

protected override void HandleFile(string filePath, string fileName, string tag, IList<Endpoint> 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);
Expand Down Expand Up @@ -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";
Expand Down
21 changes: 0 additions & 21 deletions TopModel.Generator.Jpa/SpringServerApiGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ protected override string GetFilePath(ModelFile file, string tag)

protected override void HandleFile(string filePath, string fileName, string tag, IList<Endpoint> 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);
Expand Down Expand Up @@ -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";
Expand Down

0 comments on commit 562f891

Please sign in to comment.