Skip to content

Commit dbd675d

Browse files
authored
Reorder Event and Context. (#224)
motivation: follow API design guidelines changes: Reorder `event` and `context` n public APIs
1 parent 454fe2e commit dbd675d

File tree

10 files changed

+22
-22
lines changed

10 files changed

+22
-22
lines changed

Examples/LambdaFunctions/Sources/Benchmark/main.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ struct BenchmarkHandler: EventLoopLambdaHandler {
2525
typealias In = String
2626
typealias Out = String
2727

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

Sources/AWSLambdaRuntimeCore/LambdaHandler.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public protocol LambdaHandler: EventLoopLambdaHandler {
4343

4444
@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
4545
extension LambdaHandler {
46-
public func handle(context: Lambda.Context, event: In) -> EventLoopFuture<Out> {
46+
public func handle(event: In, context: Lambda.Context) -> EventLoopFuture<Out> {
4747
let promise = context.eventLoop.makePromise(of: Out.self)
4848
promise.completeWithTask {
4949
try await self.handle(event: event, context: context)
@@ -82,7 +82,7 @@ public protocol EventLoopLambdaHandler: ByteBufferLambdaHandler {
8282
///
8383
/// - Returns: An `EventLoopFuture` to report the result of the Lambda back to the runtime engine.
8484
/// The `EventLoopFuture` should be completed with either a response of type `Out` or an `Error`
85-
func handle(context: Lambda.Context, event: In) -> EventLoopFuture<Out>
85+
func handle(event: In, context: Lambda.Context) -> EventLoopFuture<Out>
8686

8787
/// Encode a response of type `Out` to `ByteBuffer`
8888
/// Concrete Lambda handlers implement this method to provide coding functionality.
@@ -106,15 +106,15 @@ public protocol EventLoopLambdaHandler: ByteBufferLambdaHandler {
106106
extension EventLoopLambdaHandler {
107107
/// Driver for `ByteBuffer` -> `In` decoding and `Out` -> `ByteBuffer` encoding
108108
@inlinable
109-
public func handle(context: Lambda.Context, event: ByteBuffer) -> EventLoopFuture<ByteBuffer?> {
109+
public func handle(event: ByteBuffer, context: Lambda.Context) -> EventLoopFuture<ByteBuffer?> {
110110
let input: In
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(context: context, event: input).flatMapThrowing { output in
117+
return self.handle(event: input, context: context).flatMapThrowing { output in
118118
do {
119119
return try self.encode(allocator: context.allocator, value: output)
120120
} catch {
@@ -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(context: Lambda.Context, event: ByteBuffer) -> 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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ extension Lambda {
7070
allocator: self.allocator,
7171
invocation: invocation)
7272
logger.debug("sending invocation to lambda handler \(handler)")
73-
return handler.handle(context: context, event: event)
73+
return handler.handle(event: event, 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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ extension Lambda {
9797
let handler = try promise.futureResult.wait()
9898

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

Sources/CodableSample/main.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ struct Handler: EventLoopLambdaHandler {
2929
typealias In = Request
3030
typealias Out = Response
3131

32-
func handle(context: Lambda.Context, event: Request) -> 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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ struct Handler: EventLoopLambdaHandler {
2020
typealias In = String
2121
typealias Out = String
2222

23-
func handle(context: Lambda.Context, event: String) -> 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
}

Tests/AWSLambdaRuntimeCoreTests/LambdaHandlerTest.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ class LambdaHandlerTest: XCTestCase {
159159
typealias In = String
160160
typealias Out = String
161161

162-
func handle(context: Lambda.Context, event: String) -> EventLoopFuture<String> {
162+
func handle(event: String, context: Lambda.Context) -> EventLoopFuture<String> {
163163
context.eventLoop.makeSucceededFuture(event)
164164
}
165165
}
@@ -181,7 +181,7 @@ class LambdaHandlerTest: XCTestCase {
181181
typealias In = String
182182
typealias Out = Void
183183

184-
func handle(context: Lambda.Context, event: String) -> EventLoopFuture<Void> {
184+
func handle(event: String, context: Lambda.Context) -> EventLoopFuture<Void> {
185185
context.eventLoop.makeSucceededFuture(())
186186
}
187187
}
@@ -203,7 +203,7 @@ class LambdaHandlerTest: XCTestCase {
203203
typealias In = String
204204
typealias Out = String
205205

206-
func handle(context: Lambda.Context, event: String) -> EventLoopFuture<String> {
206+
func handle(event: String, context: Lambda.Context) -> EventLoopFuture<String> {
207207
context.eventLoop.makeFailedFuture(TestError("boom"))
208208
}
209209
}

Tests/AWSLambdaRuntimeCoreTests/LambdaHandlers.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ struct EchoHandler: EventLoopLambdaHandler {
1919
typealias In = String
2020
typealias Out = String
2121

22-
func handle(context: Lambda.Context, event: String) -> EventLoopFuture<String> {
22+
func handle(event: String, context: Lambda.Context) -> EventLoopFuture<String> {
2323
context.eventLoop.makeSucceededFuture(event)
2424
}
2525
}
@@ -34,7 +34,7 @@ struct FailedHandler: EventLoopLambdaHandler {
3434
self.reason = reason
3535
}
3636

37-
func handle(context: Lambda.Context, event: String) -> EventLoopFuture<Void> {
37+
func handle(event: String, context: Lambda.Context) -> EventLoopFuture<Void> {
3838
context.eventLoop.makeFailedFuture(TestError(self.reason))
3939
}
4040
}

Tests/AWSLambdaRuntimeCoreTests/LambdaLifecycleTest.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class LambdaLifecycleTest: XCTestCase {
5454
self.shutdown = shutdown
5555
}
5656

57-
func handle(context: Lambda.Context, event: ByteBuffer) -> EventLoopFuture<ByteBuffer?> {
57+
func handle(event: ByteBuffer, context: Lambda.Context) -> EventLoopFuture<ByteBuffer?> {
5858
self.handler(context, event)
5959
}
6060

Tests/AWSLambdaRuntimeTests/Lambda+CodeableTest.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class CodableLambdaTest: XCTestCase {
4343

4444
let expected: Request
4545

46-
func handle(context: Lambda.Context, event: Request) -> EventLoopFuture<Void> {
46+
func handle(event: Request, context: Lambda.Context) -> EventLoopFuture<Void> {
4747
XCTAssertEqual(event, self.expected)
4848
return context.eventLoop.makeSucceededVoidFuture()
4949
}
@@ -52,7 +52,7 @@ class CodableLambdaTest: XCTestCase {
5252
let handler = Handler(expected: request)
5353

5454
XCTAssertNoThrow(inputBuffer = try JSONEncoder().encode(request, using: self.allocator))
55-
XCTAssertNoThrow(outputBuffer = try handler.handle(context: self.newContext(), event: XCTUnwrap(inputBuffer)).wait())
55+
XCTAssertNoThrow(outputBuffer = try handler.handle(event: XCTUnwrap(inputBuffer), context: self.newContext()).wait())
5656
XCTAssertNil(outputBuffer)
5757
}
5858

@@ -68,7 +68,7 @@ class CodableLambdaTest: XCTestCase {
6868

6969
let expected: Request
7070

71-
func handle(context: Lambda.Context, event: Request) -> EventLoopFuture<Response> {
71+
func handle(event: Request, context: Lambda.Context) -> EventLoopFuture<Response> {
7272
XCTAssertEqual(event, self.expected)
7373
return context.eventLoop.makeSucceededFuture(Response(requestId: event.requestId))
7474
}
@@ -77,7 +77,7 @@ class CodableLambdaTest: XCTestCase {
7777
let handler = Handler(expected: request)
7878

7979
XCTAssertNoThrow(inputBuffer = try JSONEncoder().encode(request, using: self.allocator))
80-
XCTAssertNoThrow(outputBuffer = try handler.handle(context: self.newContext(), event: XCTUnwrap(inputBuffer)).wait())
80+
XCTAssertNoThrow(outputBuffer = try handler.handle(event: XCTUnwrap(inputBuffer), context: self.newContext()).wait())
8181
XCTAssertNoThrow(response = try JSONDecoder().decode(Response.self, from: XCTUnwrap(outputBuffer)))
8282
XCTAssertEqual(response?.requestId, request.requestId)
8383
}
@@ -107,7 +107,7 @@ class CodableLambdaTest: XCTestCase {
107107
handler.expected = request
108108

109109
XCTAssertNoThrow(inputBuffer = try JSONEncoder().encode(request, using: self.allocator))
110-
XCTAssertNoThrow(outputBuffer = try handler.handle(context: self.newContext(), event: XCTUnwrap(inputBuffer)).wait())
110+
XCTAssertNoThrow(outputBuffer = try handler.handle(event: XCTUnwrap(inputBuffer), context: self.newContext()).wait())
111111
XCTAssertNil(outputBuffer)
112112
}
113113
}
@@ -138,7 +138,7 @@ class CodableLambdaTest: XCTestCase {
138138
handler.expected = request
139139

140140
XCTAssertNoThrow(inputBuffer = try JSONEncoder().encode(request, using: self.allocator))
141-
XCTAssertNoThrow(outputBuffer = try handler.handle(context: self.newContext(), event: XCTUnwrap(inputBuffer)).wait())
141+
XCTAssertNoThrow(outputBuffer = try handler.handle(event: XCTUnwrap(inputBuffer), context: self.newContext()).wait())
142142
XCTAssertNoThrow(response = try JSONDecoder().decode(Response.self, from: XCTUnwrap(outputBuffer)))
143143
XCTAssertEqual(response?.requestId, request.requestId)
144144
}

0 commit comments

Comments
 (0)