From 501bc6e3720e3f76091701eff631b8826155386d Mon Sep 17 00:00:00 2001 From: Fabian Fett Date: Wed, 25 Aug 2021 09:42:46 +0200 Subject: [PATCH 1/2] Drop event label from handle method in LambdaHandlers --- .../LambdaFunctions/Sources/Benchmark/main.swift | 2 +- .../CurrencyExchangeHandler.swift | 2 +- .../ErrorHandling/ErrorsHappenHandler.swift | 2 +- .../Sources/HelloWorld/HelloWorldHandler.swift | 2 +- .../Sources/MyLambda/MyLambdaHandler.swift | 2 +- Sources/AWSLambdaRuntimeCore/LambdaHandler.swift | 14 +++++++------- Sources/AWSLambdaRuntimeCore/LambdaRunner.swift | 4 ++-- Sources/AWSLambdaTesting/Lambda+Testing.swift | 2 +- Sources/CodableSample/main.swift | 2 +- Sources/StringSample/main.swift | 2 +- .../LambdaHandlerTest.swift | 16 ++++++++-------- .../LambdaHandlers.swift | 4 ++-- .../LambdaLifecycleTest.swift | 2 +- .../Lambda+CodeableTest.swift | 16 ++++++++-------- Tests/AWSLambdaTestingTests/Tests.swift | 8 ++++---- 15 files changed, 40 insertions(+), 40 deletions(-) diff --git a/Examples/LambdaFunctions/Sources/Benchmark/main.swift b/Examples/LambdaFunctions/Sources/Benchmark/main.swift index 88346400..86f05bf8 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(event: String, context: Lambda.Context) -> EventLoopFuture { + func handle(_ event: String, context: Lambda.Context) -> EventLoopFuture { context.eventLoop.makeSucceededFuture("hello, world!") } } diff --git a/Examples/LambdaFunctions/Sources/CurrencyExchange/CurrencyExchangeHandler.swift b/Examples/LambdaFunctions/Sources/CurrencyExchange/CurrencyExchangeHandler.swift index 2df62afa..35dab14c 100644 --- a/Examples/LambdaFunctions/Sources/CurrencyExchange/CurrencyExchangeHandler.swift +++ b/Examples/LambdaFunctions/Sources/CurrencyExchange/CurrencyExchangeHandler.swift @@ -35,7 +35,7 @@ struct CurrencyExchangeHandler: LambdaHandler { self.calculator = ExchangeRatesCalculator() } - func handle(event: Request, context: Lambda.Context) async throws -> [Exchange] { + func handle(_ event: Request, context: Lambda.Context) async throws -> [Exchange] { try await withCheckedThrowingContinuation { continuation in self.calculator.run(logger: context.logger) { result in switch result { diff --git a/Examples/LambdaFunctions/Sources/ErrorHandling/ErrorsHappenHandler.swift b/Examples/LambdaFunctions/Sources/ErrorHandling/ErrorsHappenHandler.swift index 10f5cfd5..2491c1fc 100644 --- a/Examples/LambdaFunctions/Sources/ErrorHandling/ErrorsHappenHandler.swift +++ b/Examples/LambdaFunctions/Sources/ErrorHandling/ErrorsHappenHandler.swift @@ -23,7 +23,7 @@ struct ErrorsHappenHandler: LambdaHandler { init(context: Lambda.InitializationContext) async throws {} - func handle(event request: Request, context: Lambda.Context) async throws -> Response { + func handle(_ request: Request, context: Lambda.Context) async throws -> Response { // switch over the error type "requested" by the request, and trigger such error accordingly switch request.error { // no error here! diff --git a/Examples/LambdaFunctions/Sources/HelloWorld/HelloWorldHandler.swift b/Examples/LambdaFunctions/Sources/HelloWorld/HelloWorldHandler.swift index 06214244..4ee8bb8b 100644 --- a/Examples/LambdaFunctions/Sources/HelloWorld/HelloWorldHandler.swift +++ b/Examples/LambdaFunctions/Sources/HelloWorld/HelloWorldHandler.swift @@ -24,7 +24,7 @@ struct HelloWorldHandler: LambdaHandler { // setup your resources that you want to reuse here. } - func handle(event: String, context: Lambda.Context) async throws -> String { + func handle(_ event: String, context: Lambda.Context) async throws -> String { "hello, world" } } diff --git a/Examples/LocalDebugging/MyLambda/Sources/MyLambda/MyLambdaHandler.swift b/Examples/LocalDebugging/MyLambda/Sources/MyLambda/MyLambdaHandler.swift index 0c2225fb..18f268bb 100644 --- a/Examples/LocalDebugging/MyLambda/Sources/MyLambda/MyLambdaHandler.swift +++ b/Examples/LocalDebugging/MyLambda/Sources/MyLambda/MyLambdaHandler.swift @@ -26,7 +26,7 @@ struct MyLambdaHandler: LambdaHandler { // setup your resources that you want to reuse for every invocation here. } - func handle(event request: Request, context: Lambda.Context) async throws -> Response { + func handle(_ request: Request, context: Lambda.Context) async throws -> Response { // TODO: something useful Response(message: "Hello, \(request.name)!") } diff --git a/Sources/AWSLambdaRuntimeCore/LambdaHandler.swift b/Sources/AWSLambdaRuntimeCore/LambdaHandler.swift index 2dda45a9..30ceed00 100644 --- a/Sources/AWSLambdaRuntimeCore/LambdaHandler.swift +++ b/Sources/AWSLambdaRuntimeCore/LambdaHandler.swift @@ -38,15 +38,15 @@ public protocol LambdaHandler: EventLoopLambdaHandler { /// - context: Runtime `Context`. /// /// - Returns: A Lambda result ot type `Out`. - func handle(event: In, context: Lambda.Context) async throws -> Out + func handle(_ event: In, context: Lambda.Context) async throws -> Out } @available(macOS 12, iOS 15, tvOS 15, watchOS 8, *) extension LambdaHandler { - public func handle(event: In, context: Lambda.Context) -> 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) + try await self.handle(event, context: context) } return promise.futureResult } @@ -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(event: In, context: Lambda.Context) -> 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(event: ByteBuffer, context: Lambda.Context) -> 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(event: input, context: context).flatMapThrowing { output in + return self.handle(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(event: ByteBuffer, context: Lambda.Context) -> 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 70a71277..542ede87 100644 --- a/Sources/AWSLambdaRuntimeCore/LambdaRunner.swift +++ b/Sources/AWSLambdaRuntimeCore/LambdaRunner.swift @@ -62,7 +62,7 @@ extension Lambda { self.isGettingNextInvocation = true return self.runtimeClient.getNextInvocation(logger: logger).peekError { error in logger.error("could not fetch work from lambda runtime engine: \(error)") - }.flatMap { invocation, event in + }.flatMap { invocation, bytes in // 2. send invocation to handler self.isGettingNextInvocation = false let context = Context(logger: logger, @@ -70,7 +70,7 @@ extension Lambda { allocator: self.allocator, invocation: invocation) logger.debug("sending invocation to lambda handler \(handler)") - return handler.handle(event: event, context: context) + return handler.handle(bytes, 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 d3cbc760..c202559a 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(event: event, context: context) + handler.handle(event, context: context) }.wait() } } diff --git a/Sources/CodableSample/main.swift b/Sources/CodableSample/main.swift index 37c11718..7f91ab43 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(event: Request, context: Lambda.Context) -> 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 900e582f..861c7870 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(event: String, context: Lambda.Context) -> 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 d349d7dc..17785d88 100644 --- a/Tests/AWSLambdaRuntimeCoreTests/LambdaHandlerTest.swift +++ b/Tests/AWSLambdaRuntimeCoreTests/LambdaHandlerTest.swift @@ -39,7 +39,7 @@ class LambdaHandlerTest: XCTestCase { self.initialized = true } - func handle(event: String, context: Lambda.Context) async throws -> String { + func handle(_ event: String, context: Lambda.Context) async throws -> String { event } } @@ -68,7 +68,7 @@ class LambdaHandlerTest: XCTestCase { throw TestError("kaboom") } - func handle(event: String, context: Lambda.Context) async throws { + func handle(_ event: String, context: Lambda.Context) async throws { XCTFail("How can this be called if init failed") } } @@ -91,7 +91,7 @@ class LambdaHandlerTest: XCTestCase { init(context: Lambda.InitializationContext) {} - func handle(event: String, context: Lambda.Context) async throws -> String { + func handle(_ event: String, context: Lambda.Context) async throws -> String { event } } @@ -114,7 +114,7 @@ class LambdaHandlerTest: XCTestCase { init(context: Lambda.InitializationContext) {} - func handle(event: String, context: Lambda.Context) async throws {} + func handle(_ event: String, context: Lambda.Context) async throws {} } let maxTimes = Int.random(in: 1 ... 10) @@ -136,7 +136,7 @@ class LambdaHandlerTest: XCTestCase { init(context: Lambda.InitializationContext) {} - func handle(event: String, context: Lambda.Context) async throws -> String { + func handle(_ event: String, context: Lambda.Context) async throws -> String { throw TestError("boom") } } @@ -159,7 +159,7 @@ class LambdaHandlerTest: XCTestCase { typealias In = String typealias Out = String - func handle(event: String, context: Lambda.Context) -> 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(event: String, context: Lambda.Context) -> 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(event: String, context: Lambda.Context) -> 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 ed514376..67c12ae0 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(event: String, context: Lambda.Context) -> 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(event: String, context: Lambda.Context) -> 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 7b0226e6..b0c83113 100644 --- a/Tests/AWSLambdaRuntimeCoreTests/LambdaLifecycleTest.swift +++ b/Tests/AWSLambdaRuntimeCoreTests/LambdaLifecycleTest.swift @@ -54,7 +54,7 @@ class LambdaLifecycleTest: XCTestCase { self.shutdown = shutdown } - func handle(event: ByteBuffer, context: Lambda.Context) -> 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 c8520867..cb9c6f42 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(event: Request, context: Lambda.Context) -> 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(event: XCTUnwrap(inputBuffer), context: self.newContext()).wait()) + XCTAssertNoThrow(outputBuffer = try handler.handle(XCTUnwrap(inputBuffer), context: self.newContext()).wait()) XCTAssertNil(outputBuffer) } @@ -68,7 +68,7 @@ class CodableLambdaTest: XCTestCase { let expected: Request - func handle(event: Request, context: Lambda.Context) -> 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(event: XCTUnwrap(inputBuffer), context: self.newContext()).wait()) + XCTAssertNoThrow(outputBuffer = try handler.handle(XCTUnwrap(inputBuffer), context: self.newContext()).wait()) XCTAssertNoThrow(response = try JSONDecoder().decode(Response.self, from: XCTUnwrap(outputBuffer))) XCTAssertEqual(response?.requestId, request.requestId) } @@ -93,7 +93,7 @@ class CodableLambdaTest: XCTestCase { init(context: Lambda.InitializationContext) async throws {} - func handle(event: Request, context: Lambda.Context) async throws { + func handle(_ event: Request, context: Lambda.Context) async throws { XCTAssertEqual(event, self.expected) } } @@ -107,7 +107,7 @@ class CodableLambdaTest: XCTestCase { handler.expected = request XCTAssertNoThrow(inputBuffer = try JSONEncoder().encode(request, using: self.allocator)) - XCTAssertNoThrow(outputBuffer = try handler.handle(event: XCTUnwrap(inputBuffer), context: self.newContext()).wait()) + XCTAssertNoThrow(outputBuffer = try handler.handle(XCTUnwrap(inputBuffer), context: self.newContext()).wait()) XCTAssertNil(outputBuffer) } } @@ -122,7 +122,7 @@ class CodableLambdaTest: XCTestCase { init(context: Lambda.InitializationContext) async throws {} - func handle(event: Request, context: Lambda.Context) async throws -> Response { + func handle(_ event: Request, context: Lambda.Context) async throws -> Response { XCTAssertEqual(event, self.expected) return Response(requestId: event.requestId) } @@ -138,7 +138,7 @@ class CodableLambdaTest: XCTestCase { handler.expected = request XCTAssertNoThrow(inputBuffer = try JSONEncoder().encode(request, using: self.allocator)) - XCTAssertNoThrow(outputBuffer = try handler.handle(event: XCTUnwrap(inputBuffer), context: self.newContext()).wait()) + XCTAssertNoThrow(outputBuffer = try handler.handle(XCTUnwrap(inputBuffer), context: self.newContext()).wait()) XCTAssertNoThrow(response = try JSONDecoder().decode(Response.self, from: XCTUnwrap(outputBuffer))) XCTAssertEqual(response?.requestId, request.requestId) } diff --git a/Tests/AWSLambdaTestingTests/Tests.swift b/Tests/AWSLambdaTestingTests/Tests.swift index ae0efbcc..733af071 100644 --- a/Tests/AWSLambdaTestingTests/Tests.swift +++ b/Tests/AWSLambdaTestingTests/Tests.swift @@ -35,7 +35,7 @@ class LambdaTestingTests: XCTestCase { init(context: Lambda.InitializationContext) {} - func handle(event: Request, context: Lambda.Context) async throws -> Response { + func handle(_ event: Request, context: Lambda.Context) async throws -> Response { Response(message: "echo" + event.name) } } @@ -59,7 +59,7 @@ class LambdaTestingTests: XCTestCase { init(context: Lambda.InitializationContext) {} - func handle(event: Request, context: Lambda.Context) async throws { + func handle(_ event: Request, context: Lambda.Context) async throws { LambdaTestingTests.VoidLambdaHandlerInvokeCount += 1 } } @@ -79,7 +79,7 @@ class LambdaTestingTests: XCTestCase { init(context: Lambda.InitializationContext) {} - func handle(event: String, context: Lambda.Context) async throws { + func handle(_ event: String, context: Lambda.Context) async throws { throw MyError() } } @@ -96,7 +96,7 @@ class LambdaTestingTests: XCTestCase { init(context: Lambda.InitializationContext) {} - func handle(event: String, context: Lambda.Context) async throws -> String { + func handle(_ event: String, context: Lambda.Context) async throws -> String { try await Task.sleep(nanoseconds: 500 * 1000 * 1000) return event } From 8f3983771961651920f805d219038af2b5dd67cf Mon Sep 17 00:00:00 2001 From: Fabian Fett Date: Tue, 21 Sep 2021 21:39:48 +0200 Subject: [PATCH 2/2] Rename `In` to `Event` and `Out` to `Output` --- .../Sources/Benchmark/main.swift | 4 +- .../CurrencyExchangeHandler.swift | 4 +- .../ErrorHandling/ErrorsHappenHandler.swift | 4 +- .../HelloWorld/HelloWorldHandler.swift | 4 +- .../Sources/MyLambda/MyLambdaHandler.swift | 4 +- Sources/AWSLambdaRuntime/Lambda+Codable.swift | 22 +++++----- .../AWSLambdaRuntimeCore/Lambda+String.swift | 4 +- .../AWSLambdaRuntimeCore/LambdaHandler.swift | 44 +++++++++---------- Sources/AWSLambdaTesting/Lambda+Testing.swift | 4 +- Sources/CodableSample/main.swift | 4 +- Sources/StringSample/main.swift | 4 +- .../LambdaHandlerTest.swift | 32 +++++++------- .../Lambda+CodeableTest.swift | 16 +++---- Tests/AWSLambdaTestingTests/Tests.swift | 16 +++---- 14 files changed, 83 insertions(+), 83 deletions(-) diff --git a/Examples/LambdaFunctions/Sources/Benchmark/main.swift b/Examples/LambdaFunctions/Sources/Benchmark/main.swift index 86f05bf8..d0d54706 100644 --- a/Examples/LambdaFunctions/Sources/Benchmark/main.swift +++ b/Examples/LambdaFunctions/Sources/Benchmark/main.swift @@ -22,8 +22,8 @@ import NIO Lambda.run { $0.eventLoop.makeSucceededFuture(BenchmarkHandler()) } struct BenchmarkHandler: EventLoopLambdaHandler { - typealias In = String - typealias Out = String + typealias Event = String + typealias Output = String func handle(_ event: String, context: Lambda.Context) -> EventLoopFuture { context.eventLoop.makeSucceededFuture("hello, world!") diff --git a/Examples/LambdaFunctions/Sources/CurrencyExchange/CurrencyExchangeHandler.swift b/Examples/LambdaFunctions/Sources/CurrencyExchange/CurrencyExchangeHandler.swift index 35dab14c..1db53573 100644 --- a/Examples/LambdaFunctions/Sources/CurrencyExchange/CurrencyExchangeHandler.swift +++ b/Examples/LambdaFunctions/Sources/CurrencyExchange/CurrencyExchangeHandler.swift @@ -25,8 +25,8 @@ import Logging @main struct CurrencyExchangeHandler: LambdaHandler { - typealias In = Request - typealias Out = [Exchange] + typealias Event = Request + typealias Output = [Exchange] let calculator: ExchangeRatesCalculator diff --git a/Examples/LambdaFunctions/Sources/ErrorHandling/ErrorsHappenHandler.swift b/Examples/LambdaFunctions/Sources/ErrorHandling/ErrorsHappenHandler.swift index 2491c1fc..f7d47dd6 100644 --- a/Examples/LambdaFunctions/Sources/ErrorHandling/ErrorsHappenHandler.swift +++ b/Examples/LambdaFunctions/Sources/ErrorHandling/ErrorsHappenHandler.swift @@ -18,8 +18,8 @@ import AWSLambdaRuntime @main struct ErrorsHappenHandler: LambdaHandler { - typealias In = Request - typealias Out = Response + typealias Event = Request + typealias Output = Response init(context: Lambda.InitializationContext) async throws {} diff --git a/Examples/LambdaFunctions/Sources/HelloWorld/HelloWorldHandler.swift b/Examples/LambdaFunctions/Sources/HelloWorld/HelloWorldHandler.swift index 4ee8bb8b..ac795e65 100644 --- a/Examples/LambdaFunctions/Sources/HelloWorld/HelloWorldHandler.swift +++ b/Examples/LambdaFunctions/Sources/HelloWorld/HelloWorldHandler.swift @@ -17,8 +17,8 @@ import AWSLambdaRuntime // introductory example, the obligatory "hello, world!" @main struct HelloWorldHandler: LambdaHandler { - typealias In = String - typealias Out = String + typealias Event = String + typealias Output = String init(context: Lambda.InitializationContext) async throws { // setup your resources that you want to reuse here. diff --git a/Examples/LocalDebugging/MyLambda/Sources/MyLambda/MyLambdaHandler.swift b/Examples/LocalDebugging/MyLambda/Sources/MyLambda/MyLambdaHandler.swift index 18f268bb..3d4c88d3 100644 --- a/Examples/LocalDebugging/MyLambda/Sources/MyLambda/MyLambdaHandler.swift +++ b/Examples/LocalDebugging/MyLambda/Sources/MyLambda/MyLambdaHandler.swift @@ -19,8 +19,8 @@ import Shared // a local server simulator which will allow local debugging @main struct MyLambdaHandler: LambdaHandler { - typealias In = Request - typealias Out = Response + typealias Event = Request + typealias Output = Response init(context: Lambda.InitializationContext) async throws { // setup your resources that you want to reuse for every invocation here. diff --git a/Sources/AWSLambdaRuntime/Lambda+Codable.swift b/Sources/AWSLambdaRuntime/Lambda+Codable.swift index 960e068e..f7da53bd 100644 --- a/Sources/AWSLambdaRuntime/Lambda+Codable.swift +++ b/Sources/AWSLambdaRuntime/Lambda+Codable.swift @@ -21,33 +21,33 @@ import NIOFoundationCompat // MARK: - Codable support -/// Implementation of a`ByteBuffer` to `In` decoding -extension EventLoopLambdaHandler where In: Decodable { +/// Implementation of a`ByteBuffer` to `Event` decoding +extension EventLoopLambdaHandler where Event: Decodable { @inlinable - public func decode(buffer: ByteBuffer) throws -> In { - try self.decoder.decode(In.self, from: buffer) + public func decode(buffer: ByteBuffer) throws -> Event { + try self.decoder.decode(Event.self, from: buffer) } } -/// Implementation of `Out` to `ByteBuffer` encoding -extension EventLoopLambdaHandler where Out: Encodable { +/// Implementation of `Output` to `ByteBuffer` encoding +extension EventLoopLambdaHandler where Output: Encodable { @inlinable - public func encode(allocator: ByteBufferAllocator, value: Out) throws -> ByteBuffer? { + public func encode(allocator: ByteBufferAllocator, value: Output) throws -> ByteBuffer? { try self.encoder.encode(value, using: allocator) } } -/// Default `ByteBuffer` to `In` decoder using Foundation's JSONDecoder +/// Default `ByteBuffer` to `Event` decoder using Foundation's JSONDecoder /// Advanced users that want to inject their own codec can do it by overriding these functions. -extension EventLoopLambdaHandler where In: Decodable { +extension EventLoopLambdaHandler where Event: Decodable { public var decoder: LambdaCodableDecoder { Lambda.defaultJSONDecoder } } -/// Default `Out` to `ByteBuffer` encoder using Foundation's JSONEncoder +/// Default `Output` to `ByteBuffer` encoder using Foundation's JSONEncoder /// Advanced users that want to inject their own codec can do it by overriding these functions. -extension EventLoopLambdaHandler where Out: Encodable { +extension EventLoopLambdaHandler where Output: Encodable { public var encoder: LambdaCodableEncoder { Lambda.defaultJSONEncoder } diff --git a/Sources/AWSLambdaRuntimeCore/Lambda+String.swift b/Sources/AWSLambdaRuntimeCore/Lambda+String.swift index 800afc15..f5a68f27 100644 --- a/Sources/AWSLambdaRuntimeCore/Lambda+String.swift +++ b/Sources/AWSLambdaRuntimeCore/Lambda+String.swift @@ -13,7 +13,7 @@ //===----------------------------------------------------------------------===// import NIOCore -extension EventLoopLambdaHandler where In == String { +extension EventLoopLambdaHandler where Event == String { /// Implementation of a `ByteBuffer` to `String` decoding @inlinable public func decode(buffer: ByteBuffer) throws -> String { @@ -25,7 +25,7 @@ extension EventLoopLambdaHandler where In == String { } } -extension EventLoopLambdaHandler where Out == String { +extension EventLoopLambdaHandler where Output == String { /// Implementation of `String` to `ByteBuffer` encoding @inlinable public func encode(allocator: ByteBufferAllocator, value: String) throws -> ByteBuffer? { diff --git a/Sources/AWSLambdaRuntimeCore/LambdaHandler.swift b/Sources/AWSLambdaRuntimeCore/LambdaHandler.swift index 30ceed00..68d99f07 100644 --- a/Sources/AWSLambdaRuntimeCore/LambdaHandler.swift +++ b/Sources/AWSLambdaRuntimeCore/LambdaHandler.swift @@ -19,7 +19,7 @@ import NIOCore // MARK: - LambdaHandler #if compiler(>=5.5) -/// Strongly typed, processing protocol for a Lambda that takes a user defined `In` and returns a user defined `Out` async. +/// Strongly typed, processing protocol for a Lambda that takes a user defined `Event` and returns a user defined `Output` async. @available(macOS 12, iOS 15, tvOS 15, watchOS 8, *) public protocol LambdaHandler: EventLoopLambdaHandler { /// The Lambda initialization method @@ -34,17 +34,17 @@ public protocol LambdaHandler: EventLoopLambdaHandler { /// Concrete Lambda handlers implement this method to provide the Lambda functionality. /// /// - parameters: - /// - event: Event of type `In` representing the event or request. + /// - event: Event of type `Event` representing the event or request. /// - context: Runtime `Context`. /// - /// - Returns: A Lambda result ot type `Out`. - func handle(_ event: In, context: Lambda.Context) async throws -> Out + /// - Returns: A Lambda result ot type `Output`. + func handle(_ event: Event, context: Lambda.Context) async throws -> Output } @available(macOS 12, iOS 15, tvOS 15, watchOS 8, *) extension LambdaHandler { - public func handle(_ event: In, context: Lambda.Context) -> EventLoopFuture { - let promise = context.eventLoop.makePromise(of: Out.self) + public func handle(_ event: Event, context: Lambda.Context) -> EventLoopFuture { + let promise = context.eventLoop.makePromise(of: Output.self) promise.completeWithTask { try await self.handle(event, context: context) } @@ -62,52 +62,52 @@ extension LambdaHandler { // MARK: - EventLoopLambdaHandler -/// Strongly typed, `EventLoopFuture` based processing protocol for a Lambda that takes a user defined `In` and returns a user defined `Out` asynchronously. -/// `EventLoopLambdaHandler` extends `ByteBufferLambdaHandler`, performing `ByteBuffer` -> `In` decoding and `Out` -> `ByteBuffer` encoding. +/// Strongly typed, `EventLoopFuture` based processing protocol for a Lambda that takes a user defined `Event` and returns a user defined `Output` asynchronously. +/// `EventLoopLambdaHandler` extends `ByteBufferLambdaHandler`, performing `ByteBuffer` -> `Event` decoding and `Output` -> `ByteBuffer` encoding. /// /// - note: To implement a Lambda, implement either `LambdaHandler` or the `EventLoopLambdaHandler` protocol. /// The `LambdaHandler` will offload the Lambda execution to a `DispatchQueue` making processing safer but slower /// The `EventLoopLambdaHandler` will execute the Lambda on the same `EventLoop` as the core runtime engine, making the processing faster but requires /// more care from the implementation to never block the `EventLoop`. public protocol EventLoopLambdaHandler: ByteBufferLambdaHandler { - associatedtype In - associatedtype Out + associatedtype Event + associatedtype Output /// The Lambda handling method /// Concrete Lambda handlers implement this method to provide the Lambda functionality. /// /// - parameters: /// - context: Runtime `Context`. - /// - event: Event of type `In` representing the event or request. + /// - event: Event of type `Event` representing the event or request. /// /// - 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(_ event: In, context: Lambda.Context) -> EventLoopFuture + /// The `EventLoopFuture` should be completed with either a response of type `Output` or an `Error` + func handle(_ event: Event, context: Lambda.Context) -> EventLoopFuture - /// Encode a response of type `Out` to `ByteBuffer` + /// Encode a response of type `Output` to `ByteBuffer` /// Concrete Lambda handlers implement this method to provide coding functionality. /// - parameters: /// - allocator: A `ByteBufferAllocator` to help allocate the `ByteBuffer`. - /// - value: Response of type `Out`. + /// - value: Response of type `Output`. /// /// - Returns: A `ByteBuffer` with the encoded version of the `value`. - func encode(allocator: ByteBufferAllocator, value: Out) throws -> ByteBuffer? + func encode(allocator: ByteBufferAllocator, value: Output) throws -> ByteBuffer? - /// Decode a`ByteBuffer` to a request or event of type `In` + /// Decode a`ByteBuffer` to a request or event of type `Event` /// Concrete Lambda handlers implement this method to provide coding functionality. /// /// - parameters: /// - buffer: The `ByteBuffer` to decode. /// - /// - Returns: A request or event of type `In`. - func decode(buffer: ByteBuffer) throws -> In + /// - Returns: A request or event of type `Event`. + func decode(buffer: ByteBuffer) throws -> Event } extension EventLoopLambdaHandler { - /// Driver for `ByteBuffer` -> `In` decoding and `Out` -> `ByteBuffer` encoding + /// Driver for `ByteBuffer` -> `Event` decoding and `Output` -> `ByteBuffer` encoding @inlinable public func handle(_ event: ByteBuffer, context: Lambda.Context) -> EventLoopFuture { - let input: In + let input: Event do { input = try self.decode(buffer: event) } catch { @@ -125,7 +125,7 @@ extension EventLoopLambdaHandler { } /// Implementation of `ByteBuffer` to `Void` decoding -extension EventLoopLambdaHandler where Out == Void { +extension EventLoopLambdaHandler where Output == Void { @inlinable public func encode(allocator: ByteBufferAllocator, value: Void) throws -> ByteBuffer? { nil diff --git a/Sources/AWSLambdaTesting/Lambda+Testing.swift b/Sources/AWSLambdaTesting/Lambda+Testing.swift index c202559a..8f61419f 100644 --- a/Sources/AWSLambdaTesting/Lambda+Testing.swift +++ b/Sources/AWSLambdaTesting/Lambda+Testing.swift @@ -66,9 +66,9 @@ extension Lambda { public static func test( _ handlerType: Handler.Type, - with event: Handler.In, + with event: Handler.Event, using config: TestConfig = .init() - ) throws -> Handler.Out { + ) throws -> Handler.Output { let logger = Logger(label: "test") let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1) defer { diff --git a/Sources/CodableSample/main.swift b/Sources/CodableSample/main.swift index 7f91ab43..dae2b39e 100644 --- a/Sources/CodableSample/main.swift +++ b/Sources/CodableSample/main.swift @@ -26,8 +26,8 @@ struct Response: Codable { // in this example we are receiving and responding with codables. Request and Response above are examples of how to use // codables to model your reqeuest and response objects struct Handler: EventLoopLambdaHandler { - typealias In = Request - typealias Out = Response + typealias Event = Request + typealias Output = Response func handle(_ event: Request, context: Lambda.Context) -> EventLoopFuture { // as an example, respond with the input event's reversed body diff --git a/Sources/StringSample/main.swift b/Sources/StringSample/main.swift index 861c7870..d80820f8 100644 --- a/Sources/StringSample/main.swift +++ b/Sources/StringSample/main.swift @@ -17,8 +17,8 @@ import NIOCore // in this example we are receiving and responding with strings struct Handler: EventLoopLambdaHandler { - typealias In = String - typealias Out = String + typealias Event = String + typealias Output = String func handle(_ event: String, context: Lambda.Context) -> EventLoopFuture { // as an example, respond with the event's reversed body diff --git a/Tests/AWSLambdaRuntimeCoreTests/LambdaHandlerTest.swift b/Tests/AWSLambdaRuntimeCoreTests/LambdaHandlerTest.swift index 17785d88..edf597e4 100644 --- a/Tests/AWSLambdaRuntimeCoreTests/LambdaHandlerTest.swift +++ b/Tests/AWSLambdaRuntimeCoreTests/LambdaHandlerTest.swift @@ -28,8 +28,8 @@ class LambdaHandlerTest: XCTestCase { defer { XCTAssertNoThrow(try server.stop().wait()) } struct TestBootstrapHandler: LambdaHandler { - typealias In = String - typealias Out = String + typealias Event = String + typealias Output = String var initialized = false @@ -57,8 +57,8 @@ class LambdaHandlerTest: XCTestCase { defer { XCTAssertNoThrow(try server.stop().wait()) } struct TestBootstrapHandler: LambdaHandler { - typealias In = String - typealias Out = Void + typealias Event = String + typealias Output = Void var initialized = false @@ -86,8 +86,8 @@ class LambdaHandlerTest: XCTestCase { defer { XCTAssertNoThrow(try server.stop().wait()) } struct Handler: LambdaHandler { - typealias In = String - typealias Out = String + typealias Event = String + typealias Output = String init(context: Lambda.InitializationContext) {} @@ -109,8 +109,8 @@ class LambdaHandlerTest: XCTestCase { defer { XCTAssertNoThrow(try server.stop().wait()) } struct Handler: LambdaHandler { - typealias In = String - typealias Out = Void + typealias Event = String + typealias Output = Void init(context: Lambda.InitializationContext) {} @@ -131,8 +131,8 @@ class LambdaHandlerTest: XCTestCase { defer { XCTAssertNoThrow(try server.stop().wait()) } struct Handler: LambdaHandler { - typealias In = String - typealias Out = String + typealias Event = String + typealias Output = String init(context: Lambda.InitializationContext) {} @@ -156,8 +156,8 @@ class LambdaHandlerTest: XCTestCase { defer { XCTAssertNoThrow(try server.stop().wait()) } struct Handler: EventLoopLambdaHandler { - typealias In = String - typealias Out = String + typealias Event = String + typealias Output = String func handle(_ event: String, context: Lambda.Context) -> EventLoopFuture { context.eventLoop.makeSucceededFuture(event) @@ -178,8 +178,8 @@ class LambdaHandlerTest: XCTestCase { defer { XCTAssertNoThrow(try server.stop().wait()) } struct Handler: EventLoopLambdaHandler { - typealias In = String - typealias Out = Void + typealias Event = String + typealias Output = Void func handle(_ event: String, context: Lambda.Context) -> EventLoopFuture { context.eventLoop.makeSucceededFuture(()) @@ -200,8 +200,8 @@ class LambdaHandlerTest: XCTestCase { defer { XCTAssertNoThrow(try server.stop().wait()) } struct Handler: EventLoopLambdaHandler { - typealias In = String - typealias Out = String + typealias Event = String + typealias Output = String func handle(_ event: String, context: Lambda.Context) -> EventLoopFuture { context.eventLoop.makeFailedFuture(TestError("boom")) diff --git a/Tests/AWSLambdaRuntimeTests/Lambda+CodeableTest.swift b/Tests/AWSLambdaRuntimeTests/Lambda+CodeableTest.swift index cb9c6f42..770f1efb 100644 --- a/Tests/AWSLambdaRuntimeTests/Lambda+CodeableTest.swift +++ b/Tests/AWSLambdaRuntimeTests/Lambda+CodeableTest.swift @@ -38,8 +38,8 @@ class CodableLambdaTest: XCTestCase { var outputBuffer: ByteBuffer? struct Handler: EventLoopLambdaHandler { - typealias In = Request - typealias Out = Void + typealias Event = Request + typealias Output = Void let expected: Request @@ -63,8 +63,8 @@ class CodableLambdaTest: XCTestCase { var response: Response? struct Handler: EventLoopLambdaHandler { - typealias In = Request - typealias Out = Response + typealias Event = Request + typealias Output = Response let expected: Request @@ -86,8 +86,8 @@ class CodableLambdaTest: XCTestCase { @available(macOS 12, iOS 15, tvOS 15, watchOS 8, *) func testCodableVoidHandler() { struct Handler: LambdaHandler { - typealias In = Request - typealias Out = Void + typealias Event = Request + typealias Output = Void var expected: Request? @@ -115,8 +115,8 @@ class CodableLambdaTest: XCTestCase { @available(macOS 12, iOS 15, tvOS 15, watchOS 8, *) func testCodableHandler() { struct Handler: LambdaHandler { - typealias In = Request - typealias Out = Response + typealias Event = Request + typealias Output = Response var expected: Request? diff --git a/Tests/AWSLambdaTestingTests/Tests.swift b/Tests/AWSLambdaTestingTests/Tests.swift index 733af071..8b888931 100644 --- a/Tests/AWSLambdaTestingTests/Tests.swift +++ b/Tests/AWSLambdaTestingTests/Tests.swift @@ -30,8 +30,8 @@ class LambdaTestingTests: XCTestCase { } struct MyLambda: LambdaHandler { - typealias In = Request - typealias Out = Response + typealias Event = Request + typealias Output = Response init(context: Lambda.InitializationContext) {} @@ -54,8 +54,8 @@ class LambdaTestingTests: XCTestCase { } struct MyLambda: LambdaHandler { - typealias In = Request - typealias Out = Void + typealias Event = Request + typealias Output = Void init(context: Lambda.InitializationContext) {} @@ -74,8 +74,8 @@ class LambdaTestingTests: XCTestCase { struct MyError: Error {} struct MyLambda: LambdaHandler { - typealias In = String - typealias Out = Void + typealias Event = String + typealias Output = Void init(context: Lambda.InitializationContext) {} @@ -91,8 +91,8 @@ class LambdaTestingTests: XCTestCase { func testAsyncLongRunning() { struct MyLambda: LambdaHandler { - typealias In = String - typealias Out = String + typealias Event = String + typealias Output = String init(context: Lambda.InitializationContext) {}