From 6b28816b04af4172dc33346b9b3267295b18be97 Mon Sep 17 00:00:00 2001 From: Fabian Fett Date: Wed, 25 Aug 2021 00:06:03 +0200 Subject: [PATCH] Reorder Event and Context. --- .../LambdaFunctions/Sources/Benchmark/main.swift | 2 +- Sources/AWSLambdaRuntimeCore/LambdaHandler.swift | 10 +++++----- Sources/AWSLambdaRuntimeCore/LambdaRunner.swift | 2 +- Sources/AWSLambdaTesting/Lambda+Testing.swift | 2 +- Sources/CodableSample/main.swift | 2 +- Sources/StringSample/main.swift | 2 +- .../LambdaHandlerTest.swift | 6 +++--- Tests/AWSLambdaRuntimeCoreTests/LambdaHandlers.swift | 4 ++-- .../LambdaLifecycleTest.swift | 2 +- .../AWSLambdaRuntimeTests/Lambda+CodeableTest.swift | 12 ++++++------ 10 files changed, 22 insertions(+), 22 deletions(-) diff --git a/Examples/LambdaFunctions/Sources/Benchmark/main.swift b/Examples/LambdaFunctions/Sources/Benchmark/main.swift index 42ac289e..88346400 100644 --- a/Examples/LambdaFunctions/Sources/Benchmark/main.swift +++ b/Examples/LambdaFunctions/Sources/Benchmark/main.swift @@ -25,7 +25,7 @@ struct BenchmarkHandler: EventLoopLambdaHandler { typealias In = String typealias Out = String - func handle(context: Lambda.Context, event: String) -> EventLoopFuture { + func handle(event: String, context: Lambda.Context) -> EventLoopFuture { context.eventLoop.makeSucceededFuture("hello, world!") } } diff --git a/Sources/AWSLambdaRuntimeCore/LambdaHandler.swift b/Sources/AWSLambdaRuntimeCore/LambdaHandler.swift index 5411de01..2dda45a9 100644 --- a/Sources/AWSLambdaRuntimeCore/LambdaHandler.swift +++ b/Sources/AWSLambdaRuntimeCore/LambdaHandler.swift @@ -43,7 +43,7 @@ public protocol LambdaHandler: EventLoopLambdaHandler { @available(macOS 12, iOS 15, tvOS 15, watchOS 8, *) extension LambdaHandler { - public func handle(context: Lambda.Context, event: In) -> EventLoopFuture { + public func handle(event: In, context: Lambda.Context) -> EventLoopFuture { let promise = context.eventLoop.makePromise(of: Out.self) promise.completeWithTask { try await self.handle(event: event, context: context) @@ -82,7 +82,7 @@ public protocol EventLoopLambdaHandler: ByteBufferLambdaHandler { /// /// - Returns: An `EventLoopFuture` to report the result of the Lambda back to the runtime engine. /// The `EventLoopFuture` should be completed with either a response of type `Out` or an `Error` - func handle(context: Lambda.Context, event: In) -> EventLoopFuture + func handle(event: In, context: Lambda.Context) -> EventLoopFuture /// Encode a response of type `Out` to `ByteBuffer` /// Concrete Lambda handlers implement this method to provide coding functionality. @@ -106,7 +106,7 @@ public protocol EventLoopLambdaHandler: ByteBufferLambdaHandler { extension EventLoopLambdaHandler { /// Driver for `ByteBuffer` -> `In` decoding and `Out` -> `ByteBuffer` encoding @inlinable - public func handle(context: Lambda.Context, event: ByteBuffer) -> EventLoopFuture { + public func handle(event: ByteBuffer, context: Lambda.Context) -> EventLoopFuture { let input: In do { input = try self.decode(buffer: event) @@ -114,7 +114,7 @@ extension EventLoopLambdaHandler { return context.eventLoop.makeFailedFuture(CodecError.requestDecoding(error)) } - return self.handle(context: context, event: input).flatMapThrowing { output in + return self.handle(event: input, context: context).flatMapThrowing { output in do { return try self.encode(allocator: context.allocator, value: output) } catch { @@ -148,7 +148,7 @@ public protocol ByteBufferLambdaHandler { /// /// - Returns: An `EventLoopFuture` to report the result of the Lambda back to the runtime engine. /// The `EventLoopFuture` should be completed with either a response encoded as `ByteBuffer` or an `Error` - func handle(context: Lambda.Context, event: ByteBuffer) -> EventLoopFuture + func handle(event: ByteBuffer, context: Lambda.Context) -> EventLoopFuture /// Clean up the Lambda resources asynchronously. /// Concrete Lambda handlers implement this method to shutdown resources like `HTTPClient`s and database connections. diff --git a/Sources/AWSLambdaRuntimeCore/LambdaRunner.swift b/Sources/AWSLambdaRuntimeCore/LambdaRunner.swift index 27487718..70a71277 100644 --- a/Sources/AWSLambdaRuntimeCore/LambdaRunner.swift +++ b/Sources/AWSLambdaRuntimeCore/LambdaRunner.swift @@ -70,7 +70,7 @@ extension Lambda { allocator: self.allocator, invocation: invocation) logger.debug("sending invocation to lambda handler \(handler)") - return handler.handle(context: context, event: event) + return handler.handle(event: event, context: context) // Hopping back to "our" EventLoop is important in case the handler returns a future that // originiated from a foreign EventLoop/EventLoopGroup. // This can happen if the handler uses a library (lets say a DB client) that manages its own threads/loops diff --git a/Sources/AWSLambdaTesting/Lambda+Testing.swift b/Sources/AWSLambdaTesting/Lambda+Testing.swift index 6d0018c3..d3cbc760 100644 --- a/Sources/AWSLambdaTesting/Lambda+Testing.swift +++ b/Sources/AWSLambdaTesting/Lambda+Testing.swift @@ -97,7 +97,7 @@ extension Lambda { let handler = try promise.futureResult.wait() return try eventLoop.flatSubmit { - handler.handle(context: context, event: event) + handler.handle(event: event, context: context) }.wait() } } diff --git a/Sources/CodableSample/main.swift b/Sources/CodableSample/main.swift index 987ea832..37c11718 100644 --- a/Sources/CodableSample/main.swift +++ b/Sources/CodableSample/main.swift @@ -29,7 +29,7 @@ struct Handler: EventLoopLambdaHandler { typealias In = Request typealias Out = Response - func handle(context: Lambda.Context, event: Request) -> EventLoopFuture { + func handle(event: Request, context: Lambda.Context) -> EventLoopFuture { // as an example, respond with the input event's reversed body context.eventLoop.makeSucceededFuture(Response(body: String(event.body.reversed()))) } diff --git a/Sources/StringSample/main.swift b/Sources/StringSample/main.swift index 599fa889..900e582f 100644 --- a/Sources/StringSample/main.swift +++ b/Sources/StringSample/main.swift @@ -20,7 +20,7 @@ struct Handler: EventLoopLambdaHandler { typealias In = String typealias Out = String - func handle(context: Lambda.Context, event: String) -> EventLoopFuture { + func handle(event: String, context: Lambda.Context) -> EventLoopFuture { // as an example, respond with the event's reversed body context.eventLoop.makeSucceededFuture(String(event.reversed())) } diff --git a/Tests/AWSLambdaRuntimeCoreTests/LambdaHandlerTest.swift b/Tests/AWSLambdaRuntimeCoreTests/LambdaHandlerTest.swift index acb3a071..d349d7dc 100644 --- a/Tests/AWSLambdaRuntimeCoreTests/LambdaHandlerTest.swift +++ b/Tests/AWSLambdaRuntimeCoreTests/LambdaHandlerTest.swift @@ -159,7 +159,7 @@ class LambdaHandlerTest: XCTestCase { typealias In = String typealias Out = String - func handle(context: Lambda.Context, event: String) -> EventLoopFuture { + func handle(event: String, context: Lambda.Context) -> EventLoopFuture { context.eventLoop.makeSucceededFuture(event) } } @@ -181,7 +181,7 @@ class LambdaHandlerTest: XCTestCase { typealias In = String typealias Out = Void - func handle(context: Lambda.Context, event: String) -> EventLoopFuture { + func handle(event: String, context: Lambda.Context) -> EventLoopFuture { context.eventLoop.makeSucceededFuture(()) } } @@ -203,7 +203,7 @@ class LambdaHandlerTest: XCTestCase { typealias In = String typealias Out = String - func handle(context: Lambda.Context, event: String) -> EventLoopFuture { + func handle(event: String, context: Lambda.Context) -> EventLoopFuture { context.eventLoop.makeFailedFuture(TestError("boom")) } } diff --git a/Tests/AWSLambdaRuntimeCoreTests/LambdaHandlers.swift b/Tests/AWSLambdaRuntimeCoreTests/LambdaHandlers.swift index 94202a01..ed514376 100644 --- a/Tests/AWSLambdaRuntimeCoreTests/LambdaHandlers.swift +++ b/Tests/AWSLambdaRuntimeCoreTests/LambdaHandlers.swift @@ -19,7 +19,7 @@ struct EchoHandler: EventLoopLambdaHandler { typealias In = String typealias Out = String - func handle(context: Lambda.Context, event: String) -> EventLoopFuture { + func handle(event: String, context: Lambda.Context) -> EventLoopFuture { context.eventLoop.makeSucceededFuture(event) } } @@ -34,7 +34,7 @@ struct FailedHandler: EventLoopLambdaHandler { self.reason = reason } - func handle(context: Lambda.Context, event: String) -> EventLoopFuture { + func handle(event: String, context: Lambda.Context) -> EventLoopFuture { context.eventLoop.makeFailedFuture(TestError(self.reason)) } } diff --git a/Tests/AWSLambdaRuntimeCoreTests/LambdaLifecycleTest.swift b/Tests/AWSLambdaRuntimeCoreTests/LambdaLifecycleTest.swift index fd6bd21f..7b0226e6 100644 --- a/Tests/AWSLambdaRuntimeCoreTests/LambdaLifecycleTest.swift +++ b/Tests/AWSLambdaRuntimeCoreTests/LambdaLifecycleTest.swift @@ -54,7 +54,7 @@ class LambdaLifecycleTest: XCTestCase { self.shutdown = shutdown } - func handle(context: Lambda.Context, event: ByteBuffer) -> EventLoopFuture { + func handle(event: ByteBuffer, context: Lambda.Context) -> EventLoopFuture { self.handler(context, event) } diff --git a/Tests/AWSLambdaRuntimeTests/Lambda+CodeableTest.swift b/Tests/AWSLambdaRuntimeTests/Lambda+CodeableTest.swift index 21f32a59..c8520867 100644 --- a/Tests/AWSLambdaRuntimeTests/Lambda+CodeableTest.swift +++ b/Tests/AWSLambdaRuntimeTests/Lambda+CodeableTest.swift @@ -43,7 +43,7 @@ class CodableLambdaTest: XCTestCase { let expected: Request - func handle(context: Lambda.Context, event: Request) -> EventLoopFuture { + func handle(event: Request, context: Lambda.Context) -> EventLoopFuture { XCTAssertEqual(event, self.expected) return context.eventLoop.makeSucceededVoidFuture() } @@ -52,7 +52,7 @@ class CodableLambdaTest: XCTestCase { let handler = Handler(expected: request) XCTAssertNoThrow(inputBuffer = try JSONEncoder().encode(request, using: self.allocator)) - XCTAssertNoThrow(outputBuffer = try handler.handle(context: self.newContext(), event: XCTUnwrap(inputBuffer)).wait()) + XCTAssertNoThrow(outputBuffer = try handler.handle(event: XCTUnwrap(inputBuffer), context: self.newContext()).wait()) XCTAssertNil(outputBuffer) } @@ -68,7 +68,7 @@ class CodableLambdaTest: XCTestCase { let expected: Request - func handle(context: Lambda.Context, event: Request) -> EventLoopFuture { + func handle(event: Request, context: Lambda.Context) -> EventLoopFuture { XCTAssertEqual(event, self.expected) return context.eventLoop.makeSucceededFuture(Response(requestId: event.requestId)) } @@ -77,7 +77,7 @@ class CodableLambdaTest: XCTestCase { let handler = Handler(expected: request) XCTAssertNoThrow(inputBuffer = try JSONEncoder().encode(request, using: self.allocator)) - XCTAssertNoThrow(outputBuffer = try handler.handle(context: self.newContext(), event: XCTUnwrap(inputBuffer)).wait()) + XCTAssertNoThrow(outputBuffer = try handler.handle(event: XCTUnwrap(inputBuffer), context: self.newContext()).wait()) XCTAssertNoThrow(response = try JSONDecoder().decode(Response.self, from: XCTUnwrap(outputBuffer))) XCTAssertEqual(response?.requestId, request.requestId) } @@ -107,7 +107,7 @@ class CodableLambdaTest: XCTestCase { handler.expected = request XCTAssertNoThrow(inputBuffer = try JSONEncoder().encode(request, using: self.allocator)) - XCTAssertNoThrow(outputBuffer = try handler.handle(context: self.newContext(), event: XCTUnwrap(inputBuffer)).wait()) + XCTAssertNoThrow(outputBuffer = try handler.handle(event: XCTUnwrap(inputBuffer), context: self.newContext()).wait()) XCTAssertNil(outputBuffer) } } @@ -138,7 +138,7 @@ class CodableLambdaTest: XCTestCase { handler.expected = request XCTAssertNoThrow(inputBuffer = try JSONEncoder().encode(request, using: self.allocator)) - XCTAssertNoThrow(outputBuffer = try handler.handle(context: self.newContext(), event: XCTUnwrap(inputBuffer)).wait()) + XCTAssertNoThrow(outputBuffer = try handler.handle(event: XCTUnwrap(inputBuffer), context: self.newContext()).wait()) XCTAssertNoThrow(response = try JSONDecoder().decode(Response.self, from: XCTUnwrap(outputBuffer))) XCTAssertEqual(response?.requestId, request.requestId) }