Skip to content

Commit e5aa488

Browse files
authored
[RFC] Drop event label from handle methods in LambdaHandlers (#225)
* Drop event label from handle method in LambdaHandlers * Rename `In` to `Event` and `Out` to `Output` in `EventLoopLambdaHandler`
1 parent dbd675d commit e5aa488

File tree

17 files changed

+120
-120
lines changed

17 files changed

+120
-120
lines changed

Examples/LambdaFunctions/Sources/Benchmark/main.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ import NIO
2222
Lambda.run { $0.eventLoop.makeSucceededFuture(BenchmarkHandler()) }
2323

2424
struct BenchmarkHandler: EventLoopLambdaHandler {
25-
typealias In = String
26-
typealias Out = String
25+
typealias Event = String
26+
typealias Output = String
2727

28-
func handle(event: String, context: Lambda.Context) -> EventLoopFuture<String> {
28+
func handle(_ event: String, context: Lambda.Context) -> EventLoopFuture<String> {
2929
context.eventLoop.makeSucceededFuture("hello, world!")
3030
}
3131
}

Examples/LambdaFunctions/Sources/CurrencyExchange/CurrencyExchangeHandler.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ import Logging
2525

2626
@main
2727
struct CurrencyExchangeHandler: LambdaHandler {
28-
typealias In = Request
29-
typealias Out = [Exchange]
28+
typealias Event = Request
29+
typealias Output = [Exchange]
3030

3131
let calculator: ExchangeRatesCalculator
3232

@@ -35,7 +35,7 @@ struct CurrencyExchangeHandler: LambdaHandler {
3535
self.calculator = ExchangeRatesCalculator()
3636
}
3737

38-
func handle(event: Request, context: Lambda.Context) async throws -> [Exchange] {
38+
func handle(_ event: Request, context: Lambda.Context) async throws -> [Exchange] {
3939
try await withCheckedThrowingContinuation { continuation in
4040
self.calculator.run(logger: context.logger) { result in
4141
switch result {

Examples/LambdaFunctions/Sources/ErrorHandling/ErrorsHappenHandler.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ import AWSLambdaRuntime
1818

1919
@main
2020
struct ErrorsHappenHandler: LambdaHandler {
21-
typealias In = Request
22-
typealias Out = Response
21+
typealias Event = Request
22+
typealias Output = Response
2323

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

26-
func handle(event request: Request, context: Lambda.Context) async throws -> Response {
26+
func handle(_ request: Request, context: Lambda.Context) async throws -> Response {
2727
// switch over the error type "requested" by the request, and trigger such error accordingly
2828
switch request.error {
2929
// no error here!

Examples/LambdaFunctions/Sources/HelloWorld/HelloWorldHandler.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ import AWSLambdaRuntime
1717
// introductory example, the obligatory "hello, world!"
1818
@main
1919
struct HelloWorldHandler: LambdaHandler {
20-
typealias In = String
21-
typealias Out = String
20+
typealias Event = String
21+
typealias Output = String
2222

2323
init(context: Lambda.InitializationContext) async throws {
2424
// setup your resources that you want to reuse here.
2525
}
2626

27-
func handle(event: String, context: Lambda.Context) async throws -> String {
27+
func handle(_ event: String, context: Lambda.Context) async throws -> String {
2828
"hello, world"
2929
}
3030
}

Examples/LocalDebugging/MyLambda/Sources/MyLambda/MyLambdaHandler.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ import Shared
1919
// a local server simulator which will allow local debugging
2020
@main
2121
struct MyLambdaHandler: LambdaHandler {
22-
typealias In = Request
23-
typealias Out = Response
22+
typealias Event = Request
23+
typealias Output = Response
2424

2525
init(context: Lambda.InitializationContext) async throws {
2626
// setup your resources that you want to reuse for every invocation here.
2727
}
2828

29-
func handle(event request: Request, context: Lambda.Context) async throws -> Response {
29+
func handle(_ request: Request, context: Lambda.Context) async throws -> Response {
3030
// TODO: something useful
3131
Response(message: "Hello, \(request.name)!")
3232
}

Sources/AWSLambdaRuntime/Lambda+Codable.swift

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,33 +21,33 @@ import NIOFoundationCompat
2121

2222
// MARK: - Codable support
2323

24-
/// Implementation of a`ByteBuffer` to `In` decoding
25-
extension EventLoopLambdaHandler where In: Decodable {
24+
/// Implementation of a`ByteBuffer` to `Event` decoding
25+
extension EventLoopLambdaHandler where Event: Decodable {
2626
@inlinable
27-
public func decode(buffer: ByteBuffer) throws -> In {
28-
try self.decoder.decode(In.self, from: buffer)
27+
public func decode(buffer: ByteBuffer) throws -> Event {
28+
try self.decoder.decode(Event.self, from: buffer)
2929
}
3030
}
3131

32-
/// Implementation of `Out` to `ByteBuffer` encoding
33-
extension EventLoopLambdaHandler where Out: Encodable {
32+
/// Implementation of `Output` to `ByteBuffer` encoding
33+
extension EventLoopLambdaHandler where Output: Encodable {
3434
@inlinable
35-
public func encode(allocator: ByteBufferAllocator, value: Out) throws -> ByteBuffer? {
35+
public func encode(allocator: ByteBufferAllocator, value: Output) throws -> ByteBuffer? {
3636
try self.encoder.encode(value, using: allocator)
3737
}
3838
}
3939

40-
/// Default `ByteBuffer` to `In` decoder using Foundation's JSONDecoder
40+
/// Default `ByteBuffer` to `Event` decoder using Foundation's JSONDecoder
4141
/// Advanced users that want to inject their own codec can do it by overriding these functions.
42-
extension EventLoopLambdaHandler where In: Decodable {
42+
extension EventLoopLambdaHandler where Event: Decodable {
4343
public var decoder: LambdaCodableDecoder {
4444
Lambda.defaultJSONDecoder
4545
}
4646
}
4747

48-
/// Default `Out` to `ByteBuffer` encoder using Foundation's JSONEncoder
48+
/// Default `Output` to `ByteBuffer` encoder using Foundation's JSONEncoder
4949
/// Advanced users that want to inject their own codec can do it by overriding these functions.
50-
extension EventLoopLambdaHandler where Out: Encodable {
50+
extension EventLoopLambdaHandler where Output: Encodable {
5151
public var encoder: LambdaCodableEncoder {
5252
Lambda.defaultJSONEncoder
5353
}

Sources/AWSLambdaRuntimeCore/Lambda+String.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
//===----------------------------------------------------------------------===//
1414
import NIOCore
1515

16-
extension EventLoopLambdaHandler where In == String {
16+
extension EventLoopLambdaHandler where Event == String {
1717
/// Implementation of a `ByteBuffer` to `String` decoding
1818
@inlinable
1919
public func decode(buffer: ByteBuffer) throws -> String {
@@ -25,7 +25,7 @@ extension EventLoopLambdaHandler where In == String {
2525
}
2626
}
2727

28-
extension EventLoopLambdaHandler where Out == String {
28+
extension EventLoopLambdaHandler where Output == String {
2929
/// Implementation of `String` to `ByteBuffer` encoding
3030
@inlinable
3131
public func encode(allocator: ByteBufferAllocator, value: String) throws -> ByteBuffer? {

Sources/AWSLambdaRuntimeCore/LambdaHandler.swift

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import NIOCore
1919
// MARK: - LambdaHandler
2020

2121
#if compiler(>=5.5)
22-
/// Strongly typed, processing protocol for a Lambda that takes a user defined `In` and returns a user defined `Out` async.
22+
/// Strongly typed, processing protocol for a Lambda that takes a user defined `Event` and returns a user defined `Output` async.
2323
@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
2424
public protocol LambdaHandler: EventLoopLambdaHandler {
2525
/// The Lambda initialization method
@@ -34,19 +34,19 @@ public protocol LambdaHandler: EventLoopLambdaHandler {
3434
/// Concrete Lambda handlers implement this method to provide the Lambda functionality.
3535
///
3636
/// - parameters:
37-
/// - event: Event of type `In` representing the event or request.
37+
/// - event: Event of type `Event` representing the event or request.
3838
/// - context: Runtime `Context`.
3939
///
40-
/// - Returns: A Lambda result ot type `Out`.
41-
func handle(event: In, context: Lambda.Context) async throws -> Out
40+
/// - Returns: A Lambda result ot type `Output`.
41+
func handle(_ event: Event, context: Lambda.Context) async throws -> Output
4242
}
4343

4444
@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
4545
extension LambdaHandler {
46-
public func handle(event: In, context: Lambda.Context) -> EventLoopFuture<Out> {
47-
let promise = context.eventLoop.makePromise(of: Out.self)
46+
public func handle(_ event: Event, context: Lambda.Context) -> EventLoopFuture<Output> {
47+
let promise = context.eventLoop.makePromise(of: Output.self)
4848
promise.completeWithTask {
49-
try await self.handle(event: event, context: context)
49+
try await self.handle(event, context: context)
5050
}
5151
return promise.futureResult
5252
}
@@ -62,59 +62,59 @@ extension LambdaHandler {
6262

6363
// MARK: - EventLoopLambdaHandler
6464

65-
/// Strongly typed, `EventLoopFuture` based processing protocol for a Lambda that takes a user defined `In` and returns a user defined `Out` asynchronously.
66-
/// `EventLoopLambdaHandler` extends `ByteBufferLambdaHandler`, performing `ByteBuffer` -> `In` decoding and `Out` -> `ByteBuffer` encoding.
65+
/// Strongly typed, `EventLoopFuture` based processing protocol for a Lambda that takes a user defined `Event` and returns a user defined `Output` asynchronously.
66+
/// `EventLoopLambdaHandler` extends `ByteBufferLambdaHandler`, performing `ByteBuffer` -> `Event` decoding and `Output` -> `ByteBuffer` encoding.
6767
///
6868
/// - note: To implement a Lambda, implement either `LambdaHandler` or the `EventLoopLambdaHandler` protocol.
6969
/// The `LambdaHandler` will offload the Lambda execution to a `DispatchQueue` making processing safer but slower
7070
/// The `EventLoopLambdaHandler` will execute the Lambda on the same `EventLoop` as the core runtime engine, making the processing faster but requires
7171
/// more care from the implementation to never block the `EventLoop`.
7272
public protocol EventLoopLambdaHandler: ByteBufferLambdaHandler {
73-
associatedtype In
74-
associatedtype Out
73+
associatedtype Event
74+
associatedtype Output
7575

7676
/// The Lambda handling method
7777
/// Concrete Lambda handlers implement this method to provide the Lambda functionality.
7878
///
7979
/// - parameters:
8080
/// - context: Runtime `Context`.
81-
/// - event: Event of type `In` representing the event or request.
81+
/// - event: Event of type `Event` representing the event or request.
8282
///
8383
/// - Returns: An `EventLoopFuture` to report the result of the Lambda back to the runtime engine.
84-
/// The `EventLoopFuture` should be completed with either a response of type `Out` or an `Error`
85-
func handle(event: In, context: Lambda.Context) -> EventLoopFuture<Out>
84+
/// The `EventLoopFuture` should be completed with either a response of type `Output` or an `Error`
85+
func handle(_ event: Event, context: Lambda.Context) -> EventLoopFuture<Output>
8686

87-
/// Encode a response of type `Out` to `ByteBuffer`
87+
/// Encode a response of type `Output` to `ByteBuffer`
8888
/// Concrete Lambda handlers implement this method to provide coding functionality.
8989
/// - parameters:
9090
/// - allocator: A `ByteBufferAllocator` to help allocate the `ByteBuffer`.
91-
/// - value: Response of type `Out`.
91+
/// - value: Response of type `Output`.
9292
///
9393
/// - Returns: A `ByteBuffer` with the encoded version of the `value`.
94-
func encode(allocator: ByteBufferAllocator, value: Out) throws -> ByteBuffer?
94+
func encode(allocator: ByteBufferAllocator, value: Output) throws -> ByteBuffer?
9595

96-
/// Decode a`ByteBuffer` to a request or event of type `In`
96+
/// Decode a`ByteBuffer` to a request or event of type `Event`
9797
/// Concrete Lambda handlers implement this method to provide coding functionality.
9898
///
9999
/// - parameters:
100100
/// - buffer: The `ByteBuffer` to decode.
101101
///
102-
/// - Returns: A request or event of type `In`.
103-
func decode(buffer: ByteBuffer) throws -> In
102+
/// - Returns: A request or event of type `Event`.
103+
func decode(buffer: ByteBuffer) throws -> Event
104104
}
105105

106106
extension EventLoopLambdaHandler {
107-
/// Driver for `ByteBuffer` -> `In` decoding and `Out` -> `ByteBuffer` encoding
107+
/// Driver for `ByteBuffer` -> `Event` decoding and `Output` -> `ByteBuffer` encoding
108108
@inlinable
109-
public func handle(event: ByteBuffer, context: Lambda.Context) -> EventLoopFuture<ByteBuffer?> {
110-
let input: In
109+
public func handle(_ event: ByteBuffer, context: Lambda.Context) -> EventLoopFuture<ByteBuffer?> {
110+
let input: Event
111111
do {
112112
input = try self.decode(buffer: event)
113113
} catch {
114114
return context.eventLoop.makeFailedFuture(CodecError.requestDecoding(error))
115115
}
116116

117-
return self.handle(event: input, context: context).flatMapThrowing { output in
117+
return self.handle(input, context: context).flatMapThrowing { output in
118118
do {
119119
return try self.encode(allocator: context.allocator, value: output)
120120
} catch {
@@ -125,7 +125,7 @@ extension EventLoopLambdaHandler {
125125
}
126126

127127
/// Implementation of `ByteBuffer` to `Void` decoding
128-
extension EventLoopLambdaHandler where Out == Void {
128+
extension EventLoopLambdaHandler where Output == Void {
129129
@inlinable
130130
public func encode(allocator: ByteBufferAllocator, value: Void) throws -> ByteBuffer? {
131131
nil
@@ -148,7 +148,7 @@ public protocol ByteBufferLambdaHandler {
148148
///
149149
/// - Returns: An `EventLoopFuture` to report the result of the Lambda back to the runtime engine.
150150
/// The `EventLoopFuture` should be completed with either a response encoded as `ByteBuffer` or an `Error`
151-
func handle(event: ByteBuffer, context: Lambda.Context) -> EventLoopFuture<ByteBuffer?>
151+
func handle(_ event: ByteBuffer, context: Lambda.Context) -> EventLoopFuture<ByteBuffer?>
152152

153153
/// Clean up the Lambda resources asynchronously.
154154
/// Concrete Lambda handlers implement this method to shutdown resources like `HTTPClient`s and database connections.

Sources/AWSLambdaRuntimeCore/LambdaRunner.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,15 @@ extension Lambda {
6262
self.isGettingNextInvocation = true
6363
return self.runtimeClient.getNextInvocation(logger: logger).peekError { error in
6464
logger.error("could not fetch work from lambda runtime engine: \(error)")
65-
}.flatMap { invocation, event in
65+
}.flatMap { invocation, bytes in
6666
// 2. send invocation to handler
6767
self.isGettingNextInvocation = false
6868
let context = Context(logger: logger,
6969
eventLoop: self.eventLoop,
7070
allocator: self.allocator,
7171
invocation: invocation)
7272
logger.debug("sending invocation to lambda handler \(handler)")
73-
return handler.handle(event: event, context: context)
73+
return handler.handle(bytes, context: context)
7474
// Hopping back to "our" EventLoop is important in case the handler returns a future that
7575
// originiated from a foreign EventLoop/EventLoopGroup.
7676
// This can happen if the handler uses a library (lets say a DB client) that manages its own threads/loops

Sources/AWSLambdaTesting/Lambda+Testing.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ extension Lambda {
6666

6767
public static func test<Handler: LambdaHandler>(
6868
_ handlerType: Handler.Type,
69-
with event: Handler.In,
69+
with event: Handler.Event,
7070
using config: TestConfig = .init()
71-
) throws -> Handler.Out {
71+
) throws -> Handler.Output {
7272
let logger = Logger(label: "test")
7373
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
7474
defer {
@@ -97,7 +97,7 @@ extension Lambda {
9797
let handler = try promise.futureResult.wait()
9898

9999
return try eventLoop.flatSubmit {
100-
handler.handle(event: event, context: context)
100+
handler.handle(event, context: context)
101101
}.wait()
102102
}
103103
}

Sources/CodableSample/main.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ struct Response: Codable {
2626
// in this example we are receiving and responding with codables. Request and Response above are examples of how to use
2727
// codables to model your reqeuest and response objects
2828
struct Handler: EventLoopLambdaHandler {
29-
typealias In = Request
30-
typealias Out = Response
29+
typealias Event = Request
30+
typealias Output = Response
3131

32-
func handle(event: Request, context: Lambda.Context) -> EventLoopFuture<Response> {
32+
func handle(_ event: Request, context: Lambda.Context) -> EventLoopFuture<Response> {
3333
// as an example, respond with the input event's reversed body
3434
context.eventLoop.makeSucceededFuture(Response(body: String(event.body.reversed())))
3535
}

Sources/StringSample/main.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ import NIOCore
1717

1818
// in this example we are receiving and responding with strings
1919
struct Handler: EventLoopLambdaHandler {
20-
typealias In = String
21-
typealias Out = String
20+
typealias Event = String
21+
typealias Output = String
2222

23-
func handle(event: String, context: Lambda.Context) -> EventLoopFuture<String> {
23+
func handle(_ event: String, context: Lambda.Context) -> EventLoopFuture<String> {
2424
// as an example, respond with the event's reversed body
2525
context.eventLoop.makeSucceededFuture(String(event.reversed()))
2626
}

0 commit comments

Comments
 (0)