Skip to content

Reorder Event and Context. #224

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Examples/LambdaFunctions/Sources/Benchmark/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ struct BenchmarkHandler: EventLoopLambdaHandler {
typealias In = String
typealias Out = String

func handle(context: Lambda.Context, event: String) -> EventLoopFuture<String> {
func handle(event: String, context: Lambda.Context) -> EventLoopFuture<String> {
context.eventLoop.makeSucceededFuture("hello, world!")
}
}
10 changes: 5 additions & 5 deletions Sources/AWSLambdaRuntimeCore/LambdaHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Out> {
public func handle(event: In, context: Lambda.Context) -> EventLoopFuture<Out> {
let promise = context.eventLoop.makePromise(of: Out.self)
promise.completeWithTask {
try await self.handle(event: event, context: context)
Expand Down Expand Up @@ -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<Out>
func handle(event: In, context: Lambda.Context) -> EventLoopFuture<Out>

/// Encode a response of type `Out` to `ByteBuffer`
/// Concrete Lambda handlers implement this method to provide coding functionality.
Expand All @@ -106,15 +106,15 @@ 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<ByteBuffer?> {
public func handle(event: ByteBuffer, context: Lambda.Context) -> EventLoopFuture<ByteBuffer?> {
let input: In
do {
input = try self.decode(buffer: event)
} catch {
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 {
Expand Down Expand Up @@ -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<ByteBuffer?>
func handle(event: ByteBuffer, context: Lambda.Context) -> EventLoopFuture<ByteBuffer?>

/// Clean up the Lambda resources asynchronously.
/// Concrete Lambda handlers implement this method to shutdown resources like `HTTPClient`s and database connections.
Expand Down
2 changes: 1 addition & 1 deletion Sources/AWSLambdaRuntimeCore/LambdaRunner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Sources/AWSLambdaTesting/Lambda+Testing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/CodableSample/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ struct Handler: EventLoopLambdaHandler {
typealias In = Request
typealias Out = Response

func handle(context: Lambda.Context, event: Request) -> EventLoopFuture<Response> {
func handle(event: Request, context: Lambda.Context) -> EventLoopFuture<Response> {
// as an example, respond with the input event's reversed body
context.eventLoop.makeSucceededFuture(Response(body: String(event.body.reversed())))
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/StringSample/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ struct Handler: EventLoopLambdaHandler {
typealias In = String
typealias Out = String

func handle(context: Lambda.Context, event: String) -> EventLoopFuture<String> {
func handle(event: String, context: Lambda.Context) -> EventLoopFuture<String> {
// as an example, respond with the event's reversed body
context.eventLoop.makeSucceededFuture(String(event.reversed()))
}
Expand Down
6 changes: 3 additions & 3 deletions Tests/AWSLambdaRuntimeCoreTests/LambdaHandlerTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ class LambdaHandlerTest: XCTestCase {
typealias In = String
typealias Out = String

func handle(context: Lambda.Context, event: String) -> EventLoopFuture<String> {
func handle(event: String, context: Lambda.Context) -> EventLoopFuture<String> {
context.eventLoop.makeSucceededFuture(event)
}
}
Expand All @@ -181,7 +181,7 @@ class LambdaHandlerTest: XCTestCase {
typealias In = String
typealias Out = Void

func handle(context: Lambda.Context, event: String) -> EventLoopFuture<Void> {
func handle(event: String, context: Lambda.Context) -> EventLoopFuture<Void> {
context.eventLoop.makeSucceededFuture(())
}
}
Expand All @@ -203,7 +203,7 @@ class LambdaHandlerTest: XCTestCase {
typealias In = String
typealias Out = String

func handle(context: Lambda.Context, event: String) -> EventLoopFuture<String> {
func handle(event: String, context: Lambda.Context) -> EventLoopFuture<String> {
context.eventLoop.makeFailedFuture(TestError("boom"))
}
}
Expand Down
4 changes: 2 additions & 2 deletions Tests/AWSLambdaRuntimeCoreTests/LambdaHandlers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ struct EchoHandler: EventLoopLambdaHandler {
typealias In = String
typealias Out = String

func handle(context: Lambda.Context, event: String) -> EventLoopFuture<String> {
func handle(event: String, context: Lambda.Context) -> EventLoopFuture<String> {
context.eventLoop.makeSucceededFuture(event)
}
}
Expand All @@ -34,7 +34,7 @@ struct FailedHandler: EventLoopLambdaHandler {
self.reason = reason
}

func handle(context: Lambda.Context, event: String) -> EventLoopFuture<Void> {
func handle(event: String, context: Lambda.Context) -> EventLoopFuture<Void> {
context.eventLoop.makeFailedFuture(TestError(self.reason))
}
}
2 changes: 1 addition & 1 deletion Tests/AWSLambdaRuntimeCoreTests/LambdaLifecycleTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class LambdaLifecycleTest: XCTestCase {
self.shutdown = shutdown
}

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

Expand Down
12 changes: 6 additions & 6 deletions Tests/AWSLambdaRuntimeTests/Lambda+CodeableTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class CodableLambdaTest: XCTestCase {

let expected: Request

func handle(context: Lambda.Context, event: Request) -> EventLoopFuture<Void> {
func handle(event: Request, context: Lambda.Context) -> EventLoopFuture<Void> {
XCTAssertEqual(event, self.expected)
return context.eventLoop.makeSucceededVoidFuture()
}
Expand All @@ -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)
}

Expand All @@ -68,7 +68,7 @@ class CodableLambdaTest: XCTestCase {

let expected: Request

func handle(context: Lambda.Context, event: Request) -> EventLoopFuture<Response> {
func handle(event: Request, context: Lambda.Context) -> EventLoopFuture<Response> {
XCTAssertEqual(event, self.expected)
return context.eventLoop.makeSucceededFuture(Response(requestId: event.requestId))
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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)
}
Expand Down