swift-server / swift-aws-lambda-runtime

Swift implementation of AWS Lambda Runtime
Apache License 2.0
1.13k stars 102 forks source link

[RFC] Drop event label from handle methods in LambdaHandlers #225

Closed fabianfett closed 2 years ago

fabianfett commented 3 years ago

Drop event label from handle methods in LambdaHandlers protocols

Motivation:

In Lambda functions user's handle events from different sources. Those events can be S3Events, but also APIGatewayRequests. Let's look at a Lambda, that integrates with APIGateway.

@main
struct EchoLambda: AsyncLambdaHandler {
    typealias Event = APIGatewayV2Request
    typealias Output = APIGatewayV2Response

    init(context: Lambda.InitializationContext) async throws {}

    func handle(event: APIGatewayV2Request, context: Lambda.Context) async throws -> APIGatewayV2Response {
        APIGatewayV2Response(statusCode: .ok, body: event.body)
    }
}

The argument in the handle method is labeled event even though, in this case request would be clearly a better name. The user could use request as a parameter name by explicitly specifying the parameter name:

func handle(event request: APIGatewayV2Request, context: Lambda.Context) async throws -> APIGatewayV2Response

However event request is not easy to read and the parameter name overwrite is not the most known swift feature. For this reason I would advice against this option.

For this reason I propose dropping the argument label and specifying a parameter name event to guide users. New function signature:

func handle(_ event: In, context: Lambda.Context) async throws -> Out

This would allow users to write lambdas, that integrate with APIGateway as such:

func handle(_ request: APIGatewayV2Request, context: Lambda.Context) async throws -> APIGatewayV2Response

Modifications:

Result:

A cleaner, more flexible API.

adam-fowler commented 3 years ago

This change makes sense. It'll cause some annoyance as it changes the signature of a protocol requirement but I can live with that.

kneekey23 commented 3 years ago

From a different perspective in all the lambda docs we do refer to the object being passed to lambda as the event. If you lack familiarity with aws and swift runtime is your first encounter removing the label could be confusing for some people. Just another thought here...

kneekey23 commented 3 years ago

The word event is used for example in this doc several times even in reference to an api gateway request: https://docs.aws.amazon.com/lambda/latest/dg/lambda-services.html

fabianfett commented 2 years ago

cc @bmoffatt

tomerd commented 2 years ago

it is the "swift way" to drop label on first arguments when their meaning / usage can be easily inferred from the method name or the general context. I dont have a strong opinion here (there are two sides to this coin, as @kneekey23 points out), and in any case we should use the term "event" in the API docs to connect it back to the AWS terminology, even if we dont have a label

fabianfett commented 2 years ago

@swift-server-bot test this please

fabianfett commented 2 years ago

@tomerd Updated the name of the event In type to Event. Out is now called Output. This should make adoption even easier. Please re-review.

tomerd commented 2 years ago

some CI failures, seems like some test code needs to be further adjusted