Skip to content

Commit

Permalink
Add trim support for Amazon.Lambda.Annotations (#1610)
Browse files Browse the repository at this point in the history
  • Loading branch information
normj authored Nov 15, 2023
1 parent 2993196 commit 146ce67
Show file tree
Hide file tree
Showing 30 changed files with 941 additions and 516 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,9 @@ public void Execute(GeneratorExecutionContext context)
}
}

var serializerString = GetSerializerAttribute(context, lambdaMethodModel);
var serializerInfo = GetSerializerInfoAttribute(context, lambdaMethodModel);

var model = LambdaFunctionModelBuilder.Build(lambdaMethodModel, configureMethodModel, context, isExecutable, serializerString, defaultRuntime);
var model = LambdaFunctionModelBuilder.Build(lambdaMethodModel, configureMethodModel, context, isExecutable, serializerInfo, defaultRuntime);

// If there are more than one event, report them as errors
if (model.LambdaMethod.Events.Count > 1)
Expand Down Expand Up @@ -334,9 +334,10 @@ private bool HasSerializerAttribute(GeneratorExecutionContext context, IMethodSy
return methodModel.ContainingAssembly.HasAttribute(context, TypeFullNames.LambdaSerializerAttribute);
}

private string GetSerializerAttribute(GeneratorExecutionContext context, IMethodSymbol methodModel)
private LambdaSerializerInfo GetSerializerInfoAttribute(GeneratorExecutionContext context, IMethodSymbol methodModel)
{
var serializerString = DEFAULT_LAMBDA_SERIALIZER;
string serializerJsonContext = null;

ISymbol symbol = null;

Expand All @@ -357,19 +358,24 @@ private string GetSerializerAttribute(GeneratorExecutionContext context, IMethod
// Else return the default serializer.
else
{
return serializerString;
return new LambdaSerializerInfo(serializerString, serializerJsonContext);
}

var attribute = symbol.GetAttributes().FirstOrDefault(attr => attr.AttributeClass.Name == TypeFullNames.LambdaSerializerAttributeWithoutNamespace);

var serializerValue = attribute.ConstructorArguments.FirstOrDefault(kvp => kvp.Type.Name == nameof(Type)).Value;

if(serializerValue is INamedTypeSymbol typeSymbol && typeSymbol.Name.Contains("SourceGeneratorLambdaJsonSerializer") && typeSymbol.TypeArguments.Length == 1)
{
serializerJsonContext = typeSymbol.TypeArguments[0].ToString();
}

if (serializerValue != null)
{
serializerString = serializerValue.ToString();
}

return serializerString;
return new LambdaSerializerInfo(serializerString, serializerJsonContext);
}

public void Initialize(GeneratorInitializationContext context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public class LambdaFunctionModel : ILambdaFunctionSerializable
/// <summary>
/// Gets or sets fully qualified name of the serializer used for serialization or deserialization.
/// </summary>
public string Serializer { get; set; } =
"Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer";
public LambdaSerializerInfo SerializerInfo { get; set; } =
new LambdaSerializerInfo("Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer", null);

/// <summary>
/// Gets or sets if the output is an executable.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ namespace Amazon.Lambda.Annotations.SourceGenerator.Models
/// </summary>
public static class LambdaFunctionModelBuilder
{
public static LambdaFunctionModel Build(IMethodSymbol lambdaMethodSymbol, IMethodSymbol configureMethodSymbol, GeneratorExecutionContext context, bool isExecutable, string serializer, string runtime)
public static LambdaFunctionModel Build(IMethodSymbol lambdaMethodSymbol, IMethodSymbol configureMethodSymbol, GeneratorExecutionContext context, bool isExecutable, LambdaSerializerInfo serializerInfo, string runtime)
{
var lambdaMethod = LambdaMethodModelBuilder.Build(lambdaMethodSymbol, configureMethodSymbol, context);
var generatedMethod = GeneratedMethodModelBuilder.Build(lambdaMethodSymbol, configureMethodSymbol, lambdaMethod, context);
var model = new LambdaFunctionModel()
{
GeneratedMethod = generatedMethod,
LambdaMethod = lambdaMethod,
Serializer = serializer,
SerializerInfo = serializerInfo,
StartupType = configureMethodSymbol != null ? TypeModelBuilder.Build(configureMethodSymbol.ContainingType, context) : null,
SourceGeneratorVersion = context.Compilation
.ReferencedAssemblyNames.FirstOrDefault(x => string.Equals(x.Name, "Amazon.Lambda.Annotations"))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Amazon.Lambda.Annotations.SourceGenerator.Models
{
/// <summary>
/// Information for the registered ILambdaSerializer in the Lambda project.
/// </summary>
public class LambdaSerializerInfo
{
/// <summary>
/// Construct an instance of LambdaSerializerInfo
/// </summary>
/// <param name="serializerName"></param>
/// <param name="serializerJsonContextName"></param>
public LambdaSerializerInfo(string serializerName, string serializerJsonContextName)
{
SerializerName = serializerName;
SerializerJsonContextName = serializerJsonContextName;
}

/// <summary>
/// The full name of the type registered as the ILambdaSerializer.
/// </summary>
public string SerializerName { get; }

/// <summary>
/// The full name of the type used as the generic parameter of the SourceGeneratorLambdaJsonSerializer.
/// </summary>
public string SerializerJsonContextName { get; }
}
}
Loading

0 comments on commit 146ce67

Please sign in to comment.