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) 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 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)); public string FindType(string type) => Types.First(t => type == t || type.StartsWith(t));
}; };
@ -90,7 +90,23 @@ public class RequestClassGenerator
foreach (var field in fields) foreach (var field in fields)
{ {
if (imports.FirstOrDefault(i => i.Match(field.Type)) is Import import) /*
The following snippet will allow importing nested types if necessary.
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)) if (!importStringBuffer.ContainsKey(import.Name))
{ {
@ -100,6 +116,7 @@ public class RequestClassGenerator
importStringBuffer[import.Name].Add(import.FindType(field.Type)); importStringBuffer[import.Name].Add(import.FindType(field.Type));
} }
} }
}
return string.Join("\n", importStringBuffer.Select( return string.Join("\n", importStringBuffer.Select(
pair => $"from {pair.Key} import {string.Join(", ", pair.Value)}\n") pair => $"from {pair.Key} import {string.Join(", ", pair.Value)}\n")
@ -116,6 +133,10 @@ public class RequestClassGenerator
// Index 0 contains the full Regex match // Index 0 contains the full Regex match
var fullMatch = match.Groups[0].Value; 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 // Ignore relationship fields, these need to be done manually
if (fullMatch.IndexOf("=", StringComparison.Ordinal) < if (fullMatch.IndexOf("=", StringComparison.Ordinal) <
fullMatch.IndexOf("relationship(", StringComparison.Ordinal)) continue; fullMatch.IndexOf("relationship(", StringComparison.Ordinal)) continue;

View File

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

View File

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