diff --git a/src/PatternKit.Generators/Messaging/ContentEnricherGenerator.cs b/src/PatternKit.Generators/Messaging/ContentEnricherGenerator.cs index 95560eeb..824480e9 100644 --- a/src/PatternKit.Generators/Messaging/ContentEnricherGenerator.cs +++ b/src/PatternKit.Generators/Messaging/ContentEnricherGenerator.cs @@ -85,6 +85,12 @@ private static void Generate(SourceProductionContext context, INamedTypeSymbol t return; } + if (!IsKnownPolicy(defaultPolicy)) + { + context.ReportDiagnostic(Diagnostic.Create(InvalidConfiguration, node.Identifier.GetLocation(), type.Name, $"default policy '{defaultPolicy}' is not supported")); + return; + } + var steps = type.GetMembers().OfType() .Select(method => (Method: method, Attribute: method.GetAttributes().FirstOrDefault(static attr => attr.AttributeClass?.ToDisplayString() == StepAttributeName))) .Where(static item => item.Attribute is not null) @@ -107,6 +113,12 @@ private static void Generate(SourceProductionContext context, INamedTypeSymbol t return; } + if (!IsKnownPolicy(step.Policy)) + { + context.ReportDiagnostic(Diagnostic.Create(InvalidConfiguration, step.Method.Locations.FirstOrDefault(), type.Name, $"step '{step.Name}' policy '{step.Policy}' is not supported")); + return; + } + if (step.Policy == "UseDefault") { if (string.IsNullOrWhiteSpace(step.DefaultFactoryName)) @@ -225,6 +237,9 @@ private static bool IsValueTaskOfPayload(ITypeSymbol type, ITypeSymbol payloadTy private static int? GetNamedInt(AttributeData attribute, string name) => attribute.NamedArguments.FirstOrDefault(kv => kv.Key == name).Value.Value as int?; + private static bool IsKnownPolicy(string policy) + => policy is "Throw" or "Skip" or "UseDefault"; + private static string Escape(string value) => value.Replace("\\", "\\\\").Replace("\"", "\\\""); private static string GetAccessibility(Accessibility accessibility) diff --git a/test/PatternKit.Generators.Tests/ContentEnricherGeneratorTests.cs b/test/PatternKit.Generators.Tests/ContentEnricherGeneratorTests.cs index 5e260d82..a7495460 100644 --- a/test/PatternKit.Generators.Tests/ContentEnricherGeneratorTests.cs +++ b/test/PatternKit.Generators.Tests/ContentEnricherGeneratorTests.cs @@ -259,6 +259,52 @@ public static partial class Host; ScenarioExpect.Equal("PKMCE004", diagnostic.Id); } + [Scenario("Reports diagnostic for invalid default content enricher policy")] + [Fact] + public void ReportsDiagnosticForInvalidDefaultContentEnricherPolicy() + { + var source = """ + using PatternKit.Generators.Messaging; + + namespace Demo; + + [GenerateContentEnricher(typeof(string), DefaultPolicy = (ContentEnrichmentErrorPolicy)99)] + public static partial class Host; + """; + + var diagnostic = RunAndGetSingleDiagnostic(source, nameof(ReportsDiagnosticForInvalidDefaultContentEnricherPolicy)); + + ScenarioExpect.Equal("PKMCE004", diagnostic.Id); + ScenarioExpect.Contains("default policy '99' is not supported", diagnostic.GetMessage()); + } + + [Scenario("Reports diagnostic for invalid content enricher step policy")] + [Fact] + public void ReportsDiagnosticForInvalidContentEnricherStepPolicy() + { + var source = """ + using System.Threading; + using System.Threading.Tasks; + using PatternKit.Generators.Messaging; + using PatternKit.Messaging; + + namespace Demo; + + [GenerateContentEnricher(typeof(string))] + public static partial class Host + { + [ContentEnrichmentStep("invalid", Policy = (ContentEnrichmentErrorPolicy)99)] + private static ValueTask Add(string payload, MessageContext context, CancellationToken cancellationToken) + => ValueTask.FromResult(payload); + } + """; + + var diagnostic = RunAndGetSingleDiagnostic(source, nameof(ReportsDiagnosticForInvalidContentEnricherStepPolicy)); + + ScenarioExpect.Equal("PKMCE004", diagnostic.Id); + ScenarioExpect.Contains("step 'invalid' policy '99' is not supported", diagnostic.GetMessage()); + } + [Scenario("Reports diagnostic for invalid default content enricher factory")] [Fact] public void ReportsDiagnosticForInvalidDefaultContentEnricherFactory()