This commit is contained in:
mdnapo 2024-06-02 09:42:32 +02:00
parent 399cc4d745
commit 860bdfd8d7
3 changed files with 35 additions and 23 deletions

View File

@ -7,8 +7,8 @@ public class RequestClassGenerator
{
public record Import(string Name, List<string> Types)
{
// The Match method accounts for generic types like List[str] or Dict[str, Any]
public bool Match(string type) => Types.Any(t => type == t || type.StartsWith(t));
public string FindType(string type) => Types.First(t => type == t || type.StartsWith(t));
};
@ -90,14 +90,31 @@ public class RequestClassGenerator
foreach (var field in fields)
{
if (imports.FirstOrDefault(i => i.Match(field.Type)) is Import import)
{
if (!importStringBuffer.ContainsKey(import.Name))
{
importStringBuffer.Add(import.Name, []);
}
/*
The following snippet will allow importing nested types if necessary.
importStringBuffer[import.Name].Add(import.FindType(field.Type));
var str = "List[Dict[str, Any]]";
str = str.Replace("[", ",")
.Replace("]", "")
.Replace(" ", "");
Console.WriteLine(str); // = "List,Dict,str,Any"
*/
var dissectedTypes = field.Type.Replace("[", ",")
.Replace("]", "")
.Replace(" ", "")
.Split();
foreach (var dissectedType in dissectedTypes)
{
if (imports.FirstOrDefault(i => i.Match(dissectedType)) is Import import)
{
if (!importStringBuffer.ContainsKey(import.Name))
{
importStringBuffer.Add(import.Name, []);
}
importStringBuffer[import.Name].Add(import.FindType(field.Type));
}
}
}
@ -116,6 +133,10 @@ public class RequestClassGenerator
// Index 0 contains the full Regex match
var fullMatch = match.Groups[0].Value;
// Ignore primary_key fields
if (fullMatch.IndexOf("primary_key", StringComparison.Ordinal) <
fullMatch.IndexOf("=", StringComparison.Ordinal)) continue;
// Ignore relationship fields, these need to be done manually
if (fullMatch.IndexOf("=", StringComparison.Ordinal) <
fullMatch.IndexOf("relationship(", StringComparison.Ordinal)) continue;

View File

@ -6,18 +6,14 @@ namespace MycroForge.CLI.Extensions;
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddServices(this IServiceCollection services)
public static IServiceCollection RegisterServices(this IServiceCollection services)
{
// Register ProjectContext & features
services.AddScoped<ProjectContext>();
services.AddScoped<IFeature, Git>();
services.AddScoped<IFeature, Api>();
services.AddScoped<IFeature, Db>();
return services;
}
public static IServiceCollection AddCommands(this IServiceCollection services)
{
// Register "m4g"
services.AddScoped<Commands.MycroForge>();
services.AddScoped<ISubCommandOf<Commands.MycroForge>, Commands.MycroForge.Init>();

View File

@ -5,12 +5,7 @@ using Microsoft.Extensions.Hosting;
using var host = Host
.CreateDefaultBuilder()
.ConfigureServices((_, services) =>
{
services
.AddServices()
.AddCommands();
})
.ConfigureServices((_, services) => services.RegisterServices())
.Build();
try
@ -18,7 +13,7 @@ try
await host.Services.GetRequiredService<MycroForge.CLI.Commands.MycroForge>()
.InvokeAsync(args.Length == 0 ? ["--help"] : args);
}
catch(Exception e)
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}