diff --git a/MycroForge.CLI/CodeGen/RequestClassGenerator.cs b/MycroForge.CLI/CodeGen/RequestClassGenerator.cs index 67b8b71..d9a5f2e 100644 --- a/MycroForge.CLI/CodeGen/RequestClassGenerator.cs +++ b/MycroForge.CLI/CodeGen/RequestClassGenerator.cs @@ -7,8 +7,8 @@ public class RequestClassGenerator { public record Import(string Name, List 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; diff --git a/MycroForge.CLI/Extensions/ServiceCollectionExtensions.cs b/MycroForge.CLI/Extensions/ServiceCollectionExtensions.cs index 39b50ed..302f5e1 100644 --- a/MycroForge.CLI/Extensions/ServiceCollectionExtensions.cs +++ b/MycroForge.CLI/Extensions/ServiceCollectionExtensions.cs @@ -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(); services.AddScoped(); services.AddScoped(); services.AddScoped(); - - return services; - } - - public static IServiceCollection AddCommands(this IServiceCollection services) - { + // Register "m4g" services.AddScoped(); services.AddScoped, Commands.MycroForge.Init>(); diff --git a/MycroForge.CLI/Program.cs b/MycroForge.CLI/Program.cs index 690ad3d..4975607 100644 --- a/MycroForge.CLI/Program.cs +++ b/MycroForge.CLI/Program.cs @@ -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() .InvokeAsync(args.Length == 0 ? ["--help"] : args); } -catch(Exception e) +catch (Exception e) { Console.WriteLine(e.Message); -} +} \ No newline at end of file