From ed1dc7c2cec908e039c80c2db16db512a36f048a Mon Sep 17 00:00:00 2001 From: almostchristian Date: Tue, 26 Sep 2023 16:55:32 +0800 Subject: [PATCH] Add ConfigurationPath property --- .../ServiceRegistrationExtensions.cs | 2 +- .../GenerateConfigurationAttribute.cs | 5 +++++ src/ConfigurationProcessor.SourceGeneration/Parser.cs | 10 +++++++++- src/Directory.Build.props | 2 +- 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/sample/TestWebApiGenerator/ServiceRegistrationExtensions.cs b/sample/TestWebApiGenerator/ServiceRegistrationExtensions.cs index 426d352..fb3fd9a 100644 --- a/sample/TestWebApiGenerator/ServiceRegistrationExtensions.cs +++ b/sample/TestWebApiGenerator/ServiceRegistrationExtensions.cs @@ -5,7 +5,7 @@ namespace TestWebApiGenerator; internal static partial class ServiceRegistrationExtensions { - [GenerateConfiguration("Services", ExcludedSections = new[] { "Hsts" }, ImplicitSuffixes = new[] { "Instrumentation", "Exporter" })] + [GenerateConfiguration("Services", ExcludedSections = new[] { "Hsts" }, ImplicitSuffixes = new[] { "Instrumentation", "Exporter" }, ConfigurationPath = nameof(WebApplicationBuilder.Configuration))] public static partial void AddServicesFromConfiguration(this WebApplicationBuilder builder); // [GenerateServiceRegistration("Services")] diff --git a/src/ConfigurationProcessor.Generator/GenerateConfigurationAttribute.cs b/src/ConfigurationProcessor.Generator/GenerateConfigurationAttribute.cs index 52752fd..a5a8704 100644 --- a/src/ConfigurationProcessor.Generator/GenerateConfigurationAttribute.cs +++ b/src/ConfigurationProcessor.Generator/GenerateConfigurationAttribute.cs @@ -24,6 +24,11 @@ public GenerateConfigurationAttribute(string configurationSection) /// public string ConfigurationFile { get; set; } = DefaultConfigurationFile; + /// + /// The configuration path. + /// + public string? ConfigurationPath { get; set; } + /// /// Sections to exclude. /// diff --git a/src/ConfigurationProcessor.SourceGeneration/Parser.cs b/src/ConfigurationProcessor.SourceGeneration/Parser.cs index dc12fb4..5cbb849 100644 --- a/src/ConfigurationProcessor.SourceGeneration/Parser.cs +++ b/src/ConfigurationProcessor.SourceGeneration/Parser.cs @@ -80,7 +80,7 @@ internal IReadOnlyList GetServiceRegistrationClasses(I IMethodSymbol? configurationMethodSymbol = sm.GetDeclaredSymbol(method, cancellationToken)!; Debug.Assert(configurationMethodSymbol != null, "configuration method is present."); - (string configurationSection, string? configurationFile) = (string.Empty, null); + (string configurationSection, string? configurationFile, string? configurationPath) = (string.Empty, null, null); string[] excluded = Array.Empty(); string[] suffixes = Array.Empty(); foreach (AttributeListSyntax mal in method.AttributeLists) @@ -157,6 +157,9 @@ internal IReadOnlyList GetServiceRegistrationClasses(I case "ConfigurationFile": configurationFile = (string?)GetItem(value); break; + case "ConfigurationPath": + configurationPath = (string?)GetItem(value); + break; case "ExcludedSections": var values = (ImmutableArray)GetItem(value)!; excluded = values.Select(x => $"{configurationSection}:{x.Value}").ToArray(); @@ -357,6 +360,11 @@ static string ToDisplay(IParameterSymbol parameter) foundTarget = false; } + if (!string.IsNullOrEmpty(configurationPath)) + { + lm.ConfigurationField = $"{paramName}.{configurationPath}"; + } + var properties = paramTypeSymbol.GetMembers().OfType().ToArray(); foreach (var property in properties) { diff --git a/src/Directory.Build.props b/src/Directory.Build.props index d4c7778..683e5d8 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -10,7 +10,7 @@ - 0.4.0 + 0.4.1 $(Version)-beta.1