diff --git a/sample/TestWebApiGenerator/ServiceRegistrationExtensions.cs b/sample/TestWebApiGenerator/ServiceRegistrationExtensions.cs index e5f0151..3262390 100644 --- a/sample/TestWebApiGenerator/ServiceRegistrationExtensions.cs +++ b/sample/TestWebApiGenerator/ServiceRegistrationExtensions.cs @@ -5,7 +5,7 @@ namespace TestWebApiGenerator; internal static partial class ServiceRegistrationExtensions { - [GenerateServiceRegistration("Services")] + [GenerateServiceRegistration("Services", ExcludedSections = new[] { "Hsts" })] public static partial void AddServicesFromConfiguration(this WebApplicationBuilder builder); // [GenerateServiceRegistration("Services")] diff --git a/src/ConfigurationProcessor.DependencyInjection.Generator/GenerateServiceRegistrationAttribute.cs b/src/ConfigurationProcessor.DependencyInjection.Generator/GenerateServiceRegistrationAttribute.cs index 01a03ac..006f5d3 100644 --- a/src/ConfigurationProcessor.DependencyInjection.Generator/GenerateServiceRegistrationAttribute.cs +++ b/src/ConfigurationProcessor.DependencyInjection.Generator/GenerateServiceRegistrationAttribute.cs @@ -28,6 +28,11 @@ public GenerateServiceRegistrationAttribute(string configurationSection) /// public string ConfigurationFile { get; set; } = DefaultConfigurationFile; + /// + /// Sections to exclude. + /// + public string[] ExcludedSections { get; set; } = Array.Empty(); + /// /// Gets the configuration section. /// diff --git a/src/ConfigurationProcessor.DependencyInjection.SourceGeneration/Parser.cs b/src/ConfigurationProcessor.DependencyInjection.SourceGeneration/Parser.cs index 70a9228..129d121 100644 --- a/src/ConfigurationProcessor.DependencyInjection.SourceGeneration/Parser.cs +++ b/src/ConfigurationProcessor.DependencyInjection.SourceGeneration/Parser.cs @@ -1,4 +1,5 @@ using System.Collections.Immutable; +using System.Data; using System.Diagnostics; using ConfigurationProcessor.DependencyInjection.SourceGeneration.Parsing; using ConfigurationProcessor.DependencyInjection.SourceGeneration.Utility; @@ -83,7 +84,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[] excluded = Array.Empty(); foreach (AttributeListSyntax mal in method.AttributeLists) { foreach (AttributeSyntax ma in mal.Attributes) @@ -158,6 +159,10 @@ internal IReadOnlyList GetServiceRegistrationClasses(I case "ConfigurationFile": configurationFile = (string?)GetItem(value); break; + case "ExcludedSections": + var values = (ImmutableArray)GetItem(value)!; + excluded = values.Select(x => $"{configurationSection}:{x.Value}").ToArray(); + break; } } } @@ -189,6 +194,13 @@ internal IReadOnlyList GetServiceRegistrationClasses(I else { configurationValues = JsonConfigurationFileParser.Parse(File.OpenRead(jsonFilePath)); + + if (excluded.Length > 0) + { + configurationValues = configurationValues + .Where(x => !excluded.Any(z => x.Key.StartsWith(z))) + .ToDictionary(x => x.Key, x => x.Value); + } } var methodSignature = string.Join(", ", configurationMethodSymbol.Parameters.Select(ToDisplay)); diff --git a/src/Directory.Build.props b/src/Directory.Build.props index da9c9dd..6daf169 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -10,8 +10,8 @@ - 0.2.1 - $(Version)-beta.2 + 0.2.2 + $(Version)-beta.1