Skip to content

Commit

Permalink
Add trim support for Amazon.Lambda.Annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
normj committed Nov 14, 2023
1 parent 1ea4fba commit 1d508de
Show file tree
Hide file tree
Showing 30 changed files with 927 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,19 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Amazon.Lambda.Annotations.SourceGenerator.Models
{
public class LambdaSerializerInfo
{
public LambdaSerializerInfo(string serializerName, string serializerJsonContextName)
{
SerializerName = serializerName;
SerializerJsonContextName = serializerJsonContextName;
}

public string SerializerName { get; }

public string SerializerJsonContextName { get; }
}
}
Loading

0 comments on commit 1d508de

Please sign in to comment.