Skip to content

Commit

Permalink
[C#] Rend toutes les méthodes des générateurs surchargeables
Browse files Browse the repository at this point in the history
  • Loading branch information
JabX committed Sep 4, 2024
1 parent 886dc79 commit c75a768
Show file tree
Hide file tree
Showing 9 changed files with 335 additions and 331 deletions.
12 changes: 6 additions & 6 deletions TopModel.Generator.Csharp/CSharpApiClientGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ protected override string GetFilePath(ModelFile file, string tag)
return Path.Combine(Config.GetApiPath(file, tag), "generated", $"{file.Options.Endpoints.FileName.ToPascalCase()}Client.cs");
}

protected virtual string GetNamespace(Class classe, string tag)
{
return Config.GetNamespace(classe, classe.Tags.Contains(tag) ? tag : classe.Tags.Intersect(Config.Tags).FirstOrDefault() ?? tag);
}

protected override void HandleFile(string filePath, string fileName, string tag, IList<Endpoint> endpoints)
{
var className = $"{fileName.ToPascalCase()}Client";
Expand Down Expand Up @@ -296,12 +301,7 @@ protected override void HandleFile(string filePath, string fileName, string tag,
fw.WriteLine("}");
}

private string GetNamespace(Class classe, string tag)
{
return Config.GetNamespace(classe, classe.Tags.Contains(tag) ? tag : classe.Tags.Intersect(Config.Tags).FirstOrDefault() ?? tag);
}

private void HandleFilePartial(string filePath, string className, string ns)
protected virtual void HandleFilePartial(string filePath, string className, string ns)
{
if (File.Exists(filePath))
{
Expand Down
116 changes: 58 additions & 58 deletions TopModel.Generator.Csharp/CSharpApiServerGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,64 @@ protected override string GetFilePath(ModelFile file, string tag)
return Path.Combine(Config.GetApiPath(file, tag, withControllers: true), $"{file.Options.Endpoints.FileName.ToPascalCase()}Controller.cs");
}

protected virtual string GetParam(IProperty param)
{
var sb = new StringBuilder();

var type = Config.GetType(param, nonNullable: param.IsJsonBodyParam() || param.IsRouteParam() || param.IsQueryParam() && !param.Endpoint.IsMultipart && Config.GetValue(param, Classes) != "null");

if (param.Endpoint.IsMultipart && !param.IsQueryParam() && !param.IsRouteParam() && type != "IFormFile")
{
sb.Append("[FromForm] ");
}
else if (param.IsJsonBodyParam())
{
sb.Append("[FromBody] ");
}
else if (type.EndsWith("[]"))
{
sb.Append("[FromQuery] ");
}

sb.Append($@"{type} {param.GetParamName().Verbatim()}");

if (param.IsQueryParam() && !param.Endpoint.IsMultipart)
{
sb.Append($" = {Config.GetValue(param, Classes)}");
}

return sb.ToString();
}

protected virtual string GetRoute(Endpoint endpoint)
{
var split = endpoint.FullRoute.Split("/");

for (var i = 0; i < split.Length; i++)
{
if (split[i].StartsWith('{'))
{
var routeParamName = split[i][1..^1];
var param = endpoint.Params.Single(param => param.GetParamName() == routeParamName);

var paramType = Config.GetType(param) switch
{
"int" => "int",
"int?" => "int",
"Guid" => "guid",
"Guid?" => "guid",
_ => null
};
if (paramType != null)
{
split[i] = $"{{{routeParamName}:{paramType}}}";
}
}
}

return string.Join("/", split);
}

protected override void HandleFile(string filePath, string fileName, string tag, IList<Endpoint> endpoints)
{
var className = $"{fileName.ToPascalCase()}Controller";
Expand Down Expand Up @@ -125,62 +183,4 @@ public class {className} : Controller
using var fw = new FileWriter(filePath, _logger, true) { HeaderMessage = "ATTENTION, CE FICHIER EST PARTIELLEMENT GENERE AUTOMATIQUEMENT !" };
fw.Write(syntaxTree.GetRoot().ReplaceNode(existingController, controller).ToString());
}

private string GetParam(IProperty param)
{
var sb = new StringBuilder();

var type = Config.GetType(param, nonNullable: param.IsJsonBodyParam() || param.IsRouteParam() || param.IsQueryParam() && !param.Endpoint.IsMultipart && Config.GetValue(param, Classes) != "null");

if (param.Endpoint.IsMultipart && !param.IsQueryParam() && !param.IsRouteParam() && type != "IFormFile")
{
sb.Append("[FromForm] ");
}
else if (param.IsJsonBodyParam())
{
sb.Append("[FromBody] ");
}
else if (type.EndsWith("[]"))
{
sb.Append("[FromQuery] ");
}

sb.Append($@"{type} {param.GetParamName().Verbatim()}");

if (param.IsQueryParam() && !param.Endpoint.IsMultipart)
{
sb.Append($" = {Config.GetValue(param, Classes)}");
}

return sb.ToString();
}

private string GetRoute(Endpoint endpoint)
{
var split = endpoint.FullRoute.Split("/");

for (var i = 0; i < split.Length; i++)
{
if (split[i].StartsWith('{'))
{
var routeParamName = split[i][1..^1];
var param = endpoint.Params.Single(param => param.GetParamName() == routeParamName);

var paramType = Config.GetType(param) switch
{
"int" => "int",
"int?" => "int",
"Guid" => "guid",
"Guid?" => "guid",
_ => null
};
if (paramType != null)
{
split[i] = $"{{{routeParamName}:{paramType}}}";
}
}
}

return string.Join("/", split);
}
}
Loading

0 comments on commit c75a768

Please sign in to comment.