Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Change response type to Stream when Lambda function uses APIGateway events #1746

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,61 +53,44 @@ private static IList<string> BuildUsings(LambdaMethodModel lambdaMethodModel,
{
namespaces.Add("Amazon.Lambda.Annotations.APIGateway");
}
if (lambdaMethodModel.Events.Contains(EventType.API))
{
namespaces.Add("System.Text.Json");
}

return namespaces;
}

private static TypeModel BuildResponseType(IMethodSymbol lambdaMethodSymbol,
LambdaMethodModel lambdaMethodModel, GeneratorExecutionContext context)
{
var task = context.Compilation.GetTypeByMetadataName(TypeFullNames.Task1);

if (lambdaMethodModel.ReturnsIHttpResults)
{
var typeStream = context.Compilation.GetTypeByMetadataName(TypeFullNames.Stream);
if (lambdaMethodModel.ReturnsGenericTask)
{
var genericTask = task.Construct(typeStream);
return TypeModelBuilder.Build(genericTask, context);
}
return TypeModelBuilder.Build(typeStream, context);
}


if (lambdaMethodSymbol.HasAttribute(context, TypeFullNames.RestApiAttribute))
if (lambdaMethodModel.Events.Contains(EventType.API))
{
var symbol = lambdaMethodModel.ReturnsVoidOrGenericTask ?
task.Construct(context.Compilation.GetTypeByMetadataName(TypeFullNames.APIGatewayProxyResponse)):
context.Compilation.GetTypeByMetadataName(TypeFullNames.APIGatewayProxyResponse);
return TypeModelBuilder.Build(symbol, context);
}
else if (lambdaMethodSymbol.HasAttribute(context, TypeFullNames.HttpApiAttribute))
{
var version = GetHttpApiVersion(lambdaMethodSymbol, context);
switch (version)
if (lambdaMethodSymbol.HasAttribute(context, TypeFullNames.HttpApiAttribute))
{
case HttpApiVersion.V1:
var version = GetHttpApiVersion(lambdaMethodSymbol, context);
if (version != HttpApiVersion.V1 && version != HttpApiVersion.V2)
{
var symbol = lambdaMethodModel.ReturnsVoidOrGenericTask ?
task.Construct(context.Compilation.GetTypeByMetadataName(TypeFullNames.APIGatewayProxyResponse)):
context.Compilation.GetTypeByMetadataName(TypeFullNames.APIGatewayProxyResponse);
return TypeModelBuilder.Build(symbol, context);
}
case HttpApiVersion.V2:
{
var symbol = lambdaMethodModel.ReturnsVoidOrGenericTask ?
task.Construct(context.Compilation.GetTypeByMetadataName(TypeFullNames.APIGatewayHttpApiV2ProxyResponse)):
context.Compilation.GetTypeByMetadataName(TypeFullNames.APIGatewayHttpApiV2ProxyResponse);
return TypeModelBuilder.Build(symbol, context);
}
default:
throw new ArgumentOutOfRangeException();
};
}

return CreateStreamTypeModel(context, lambdaMethodModel);
}
else

return lambdaMethodModel.ReturnType;
}

private static TypeModel CreateStreamTypeModel(GeneratorExecutionContext context, LambdaMethodModel lambdaMethodModel)
{
var task = context.Compilation.GetTypeByMetadataName(TypeFullNames.Task1);
var typeStream = context.Compilation.GetTypeByMetadataName(TypeFullNames.Stream);
if (lambdaMethodModel.ReturnsVoidOrGenericTask)
{
return lambdaMethodModel.ReturnType;
var genericTask = task.Construct(typeStream);
return TypeModelBuilder.Build(genericTask, context);
}
return TypeModelBuilder.Build(typeStream, context);
}

private static HttpApiVersion GetHttpApiVersion(IMethodSymbol lambdaMethodSymbol, GeneratorExecutionContext context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,34 @@ public bool ReturnsIHttpResults
{
return true;
}
if(ReturnsGenericTask && ReturnType.TypeArguments.Count == 1 && ReturnType.TypeArguments[0].FullName == TypeFullNames.IHttpResult)

return ReturnsGenericTask && ReturnType.TypeArguments.Count == 1 && ReturnType.TypeArguments[0].FullName == TypeFullNames.IHttpResult;
}
}

/// <summary>
/// Returns true if the Lambda function returns either of the following types
/// 1. APIGatewayProxyResponse or Task<APIGatewayProxyResponse>
/// 2. APIGatewayHttpApiV2ProxyResponse or Task<APIGatewayHttpApiV2ProxyResponse>
/// </summary>
public bool ReturnsApiGatewayResponse
{
get
{
if(ReturnsVoid)
{
return true;
return false;
}

if(ReturnType.FullName == TypeFullNames.APIGatewayProxyResponse || ReturnType.FullName == TypeFullNames.APIGatewayHttpApiV2ProxyResponse)
{
return true;
}
if(ReturnsGenericTask && ReturnType.TypeArguments.Count == 1)
{
return ReturnType.TypeArguments[0].FullName == TypeFullNames.APIGatewayProxyResponse
|| ReturnType.TypeArguments[0].FullName == TypeFullNames.APIGatewayHttpApiV2ProxyResponse;
}

return false;
}
Expand Down
Loading
Loading