Skip to content

Commit 53e14ec

Browse files
committed
Add [SNSEvent] annotation attribute and source generator support
- SNSEventAttribute with Topic, ResourceName, FilterPolicy, Enabled - SNSEventAttributeBuilder for Roslyn AttributeData parsing - Source generator wiring (TypeFullNames, SyntaxReceiver, EventTypeBuilder, AttributeModelBuilder) - CloudFormationWriter ProcessSNSAttribute (SAM SNS event subscription) - LambdaFunctionValidator ValidateSNSEvents - DiagnosticDescriptors InvalidSNSEventAttribute - SNSEventAttributeTests (attribute unit tests) - SNSEventsTests (CloudFormation writer tests) - E2E source generator snapshot tests - Integration test (SNSEventSubscription) - Sample function (SnsMessageProcessing) - .autover change file - README documentation
1 parent 78afc7a commit 53e14ec

26 files changed

+1099
-3
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"Projects": [
3+
{
4+
"Name": "Amazon.Lambda.Annotations",
5+
"Type": "Minor",
6+
"ChangelogMessages": [
7+
"Added [SNSEvent] annotation attribute for declaratively configuring SNS topic-triggered Lambda functions with support for topic reference, filter policy, and enabled state."
8+
]
9+
}
10+
]
11+
}

Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Diagnostics/AnalyzerReleases.Unshipped.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ AWSLambda0133 | AWSLambdaCSharpGenerator | Error | ALB Listener Reference Not Fo
2121
AWSLambda0134 | AWSLambdaCSharpGenerator | Error | FromRoute not supported on ALB functions
2222
AWSLambda0135 | AWSLambdaCSharpGenerator | Error | Unmapped parameter on ALB function
2323
AWSLambda0136 | AWSLambdaCSharpGenerator | Error | Invalid S3EventAttribute
24+
AWSLambda0138 | AWSLambdaCSharpGenerator | Error | Invalid SNSEventAttribute

Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Diagnostics/DiagnosticDescriptors.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,5 +281,12 @@ public static class DiagnosticDescriptors
281281
category: "AWSLambdaCSharpGenerator",
282282
DiagnosticSeverity.Error,
283283
isEnabledByDefault: true);
284+
285+
public static readonly DiagnosticDescriptor InvalidSnsEventAttribute = new DiagnosticDescriptor(id: "AWSLambda0138",
286+
title: "Invalid SNSEventAttribute",
287+
messageFormat: "Invalid SNSEventAttribute encountered: {0}",
288+
category: "AWSLambdaCSharpGenerator",
289+
DiagnosticSeverity.Error,
290+
isEnabledByDefault: true);
284291
}
285292
}

Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Models/Attributes/AttributeModelBuilder.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using Amazon.Lambda.Annotations.ALB;
3+
using Amazon.Lambda.Annotations.SNS;
34
using Amazon.Lambda.Annotations.APIGateway;
45
using Amazon.Lambda.Annotations.S3;
56
using Amazon.Lambda.Annotations.SQS;
@@ -101,6 +102,15 @@ public static AttributeModel Build(AttributeData att, GeneratorExecutionContext
101102
Type = TypeModelBuilder.Build(att.AttributeClass, context)
102103
};
103104
}
105+
else if (att.AttributeClass.Equals(context.Compilation.GetTypeByMetadataName(TypeFullNames.SNSEventAttribute), SymbolEqualityComparer.Default))
106+
{
107+
var data = SNSEventAttributeBuilder.Build(att);
108+
model = new AttributeModel<Amazon.Lambda.Annotations.SNS.SNSEventAttribute>
109+
{
110+
Data = data,
111+
Type = TypeModelBuilder.Build(att.AttributeClass, context)
112+
};
113+
}
104114
else if (att.AttributeClass.Equals(context.Compilation.GetTypeByMetadataName(TypeFullNames.HttpApiAuthorizerAttribute), SymbolEqualityComparer.Default))
105115
{
106116
var data = HttpApiAuthorizerAttributeBuilder.Build(att);
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using Amazon.Lambda.Annotations.SNS;
2+
using Microsoft.CodeAnalysis;
3+
using System;
4+
5+
namespace Amazon.Lambda.Annotations.SourceGenerator.Models.Attributes
6+
{
7+
/// <summary>
8+
/// Builder for <see cref="SNSEventAttribute"/>.
9+
/// </summary>
10+
public class SNSEventAttributeBuilder
11+
{
12+
public static SNSEventAttribute Build(AttributeData att)
13+
{
14+
if (att.ConstructorArguments.Length != 1)
15+
{
16+
throw new NotSupportedException($"{TypeFullNames.SNSEventAttribute} must have constructor with 1 argument.");
17+
}
18+
var topic = att.ConstructorArguments[0].Value as string;
19+
var data = new SNSEventAttribute(topic);
20+
21+
foreach (var pair in att.NamedArguments)
22+
{
23+
if (pair.Key == nameof(data.ResourceName) && pair.Value.Value is string resourceName)
24+
{
25+
data.ResourceName = resourceName;
26+
}
27+
else if (pair.Key == nameof(data.FilterPolicy) && pair.Value.Value is string filterPolicy)
28+
{
29+
data.FilterPolicy = filterPolicy;
30+
}
31+
else if (pair.Key == nameof(data.Enabled) && pair.Value.Value is bool enabled)
32+
{
33+
data.Enabled = enabled;
34+
}
35+
}
36+
37+
return data;
38+
}
39+
}
40+
}

Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Models/EventType.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public enum EventType
99
API,
1010
S3,
1111
SQS,
12+
SNS,
1213
DynamoDB,
1314
Schedule,
1415
Authorizer,

Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Models/EventTypeBuilder.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ public static HashSet<EventType> Build(IMethodSymbol lambdaMethodSymbol,
3030
{
3131
events.Add(EventType.S3);
3232
}
33+
else if (attribute.AttributeClass.ToDisplayString() == TypeFullNames.SNSEventAttribute)
34+
{
35+
events.Add(EventType.SNS);
36+
}
3337
else if (attribute.AttributeClass.ToDisplayString() == TypeFullNames.HttpApiAuthorizerAttribute
3438
|| attribute.AttributeClass.ToDisplayString() == TypeFullNames.RestApiAuthorizerAttribute)
3539
{

Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/SyntaxReceiver.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ internal class SyntaxReceiver : ISyntaxContextReceiver
2323
{ "RestApiAttribute", "RestApi" },
2424
{ "SQSEventAttribute", "SQSEvent" },
2525
{ "ALBApiAttribute", "ALBApi" },
26-
{ "S3EventAttribute", "S3Event" }
26+
{ "S3EventAttribute", "S3Event" },
27+
{ "SNSEventAttribute", "SNSEvent" }
2728
};
2829

2930
public List<MethodDeclarationSyntax> LambdaMethods { get; } = new List<MethodDeclarationSyntax>();

Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/TypeFullNames.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ public static class TypeFullNames
5656
public const string S3Event = "Amazon.Lambda.S3Events.S3Event";
5757
public const string S3EventAttribute = "Amazon.Lambda.Annotations.S3.S3EventAttribute";
5858

59+
public const string SNSEvent = "Amazon.Lambda.SNSEvents.SNSEvent";
60+
public const string SNSEventAttribute = "Amazon.Lambda.Annotations.SNS.SNSEventAttribute";
61+
5962
public const string LambdaSerializerAttribute = "Amazon.Lambda.Core.LambdaSerializerAttribute";
6063
public const string DefaultLambdaSerializer = "Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer";
6164

@@ -84,7 +87,8 @@ public static class TypeFullNames
8487
HttpApiAttribute,
8588
SQSEventAttribute,
8689
ALBApiAttribute,
87-
S3EventAttribute
90+
S3EventAttribute,
91+
SNSEventAttribute
8892
};
8993
}
9094
}

Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Validation/LambdaFunctionValidator.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Amazon.Lambda.Annotations.SourceGenerator.Extensions;
55
using Amazon.Lambda.Annotations.SourceGenerator.Models;
66
using Amazon.Lambda.Annotations.SourceGenerator.Models.Attributes;
7+
using Amazon.Lambda.Annotations.SNS;
78
using Amazon.Lambda.Annotations.SQS;
89
using Microsoft.CodeAnalysis;
910
using System.Collections.Generic;
@@ -61,6 +62,7 @@ internal static bool ValidateFunction(GeneratorExecutionContext context, IMethod
6162
// Validate Events
6263
ValidateApiGatewayEvents(lambdaFunctionModel, methodLocation, diagnostics);
6364
ValidateSqsEvents(lambdaFunctionModel, methodLocation, diagnostics);
65+
ValidateSnsEvents(lambdaFunctionModel, methodLocation, diagnostics);
6466
ValidateAlbEvents(lambdaFunctionModel, methodLocation, diagnostics);
6567
ValidateS3Events(lambdaFunctionModel, methodLocation, diagnostics);
6668

@@ -110,6 +112,16 @@ internal static bool ValidateDependencies(GeneratorExecutionContext context, IMe
110112
}
111113
}
112114

115+
// Check for references to "Amazon.Lambda.SNSEvents" if the Lambda method is annotated with SNSEvent attribute.
116+
if (lambdaMethodSymbol.HasAttribute(context, TypeFullNames.SNSEventAttribute))
117+
{
118+
if (context.Compilation.ReferencedAssemblyNames.FirstOrDefault(x => x.Name == "Amazon.Lambda.SNSEvents") == null)
119+
{
120+
diagnosticReporter.Report(Diagnostic.Create(DiagnosticDescriptors.MissingDependencies, methodLocation, "Amazon.Lambda.SNSEvents"));
121+
return false;
122+
}
123+
}
124+
113125
return true;
114126
}
115127

@@ -420,6 +432,45 @@ private static void ValidateS3Events(LambdaFunctionModel lambdaFunctionModel, Lo
420432
}
421433
}
422434

435+
private static void ValidateSnsEvents(LambdaFunctionModel lambdaFunctionModel, Location methodLocation, List<Diagnostic> diagnostics)
436+
{
437+
if (!lambdaFunctionModel.LambdaMethod.Events.Contains(EventType.SNS))
438+
{
439+
return;
440+
}
441+
442+
// Validate SNSEventAttributes
443+
foreach (var att in lambdaFunctionModel.Attributes)
444+
{
445+
if (att.Type.FullName != TypeFullNames.SNSEventAttribute)
446+
continue;
447+
448+
var snsEventAttribute = ((AttributeModel<SNSEventAttribute>)att).Data;
449+
var validationErrors = snsEventAttribute.Validate();
450+
validationErrors.ForEach(errorMessage => diagnostics.Add(Diagnostic.Create(DiagnosticDescriptors.InvalidSnsEventAttribute, methodLocation, errorMessage)));
451+
}
452+
453+
// Validate method parameters - When using SNSEventAttribute, the method signature must be (SNSEvent snsEvent) or (SNSEvent snsEvent, ILambdaContext context)
454+
var parameters = lambdaFunctionModel.LambdaMethod.Parameters;
455+
if (parameters.Count == 0 ||
456+
parameters.Count > 2 ||
457+
(parameters.Count == 1 && parameters[0].Type.FullName != TypeFullNames.SNSEvent) ||
458+
(parameters.Count == 2 && (parameters[0].Type.FullName != TypeFullNames.SNSEvent || parameters[1].Type.FullName != TypeFullNames.ILambdaContext)))
459+
{
460+
var errorMessage = $"When using the {nameof(SNSEventAttribute)}, the Lambda method can accept at most 2 parameters. " +
461+
$"The first parameter is required and must be of type {TypeFullNames.SNSEvent}. " +
462+
$"The second parameter is optional and must be of type {TypeFullNames.ILambdaContext}.";
463+
diagnostics.Add(Diagnostic.Create(DiagnosticDescriptors.InvalidLambdaMethodSignature, methodLocation, errorMessage));
464+
}
465+
466+
// Validate method return type - When using SNSEventAttribute, the return type must be either void or Task
467+
if (!lambdaFunctionModel.LambdaMethod.ReturnsVoid && !lambdaFunctionModel.LambdaMethod.ReturnsVoidTask)
468+
{
469+
var errorMessage = $"When using the {nameof(SNSEventAttribute)}, the Lambda method can return either void or {TypeFullNames.Task}";
470+
diagnostics.Add(Diagnostic.Create(DiagnosticDescriptors.InvalidLambdaMethodSignature, methodLocation, errorMessage));
471+
}
472+
}
473+
423474
private static bool ReportDiagnostics(DiagnosticReporter diagnosticReporter, List<Diagnostic> diagnostics)
424475
{
425476
var isValid = true;

0 commit comments

Comments
 (0)