Skip to content

Commit

Permalink
Rename payload to event (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
fabianfett authored Jun 7, 2020
1 parent 71587ea commit 0008e59
Show file tree
Hide file tree
Showing 29 changed files with 145 additions and 145 deletions.
2 changes: 1 addition & 1 deletion Examples/LambdaFunctions/Sources/APIGateway/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ struct APIGatewayProxyLambda: EventLoopLambdaHandler {
public typealias In = APIGateway.V2.Request
public typealias Out = APIGateway.V2.Response

public func handle(context: Lambda.Context, payload: APIGateway.V2.Request) -> EventLoopFuture<APIGateway.V2.Response> {
public func handle(context: Lambda.Context, event: APIGateway.V2.Request) -> EventLoopFuture<APIGateway.V2.Response> {
context.logger.debug("hello, api gateway!")
return context.eventLoop.makeSucceededFuture(APIGateway.V2.Response(statusCode: .ok, body: "hello, world!"))
}
Expand Down
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, payload: String) -> EventLoopFuture<String> {
func handle(context: Lambda.Context, event: String) -> EventLoopFuture<String> {
context.eventLoop.makeSucceededFuture("hello, world!")
}
}
4 changes: 2 additions & 2 deletions Examples/LocalDebugging/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ Start with running the `MyLambda` target.
* Set the `LOCAL_LAMBDA_SERVER_ENABLED` environment variable to `true` by editing the `MyLambda` scheme under `Run`.
* Hit `Run`
* Once it is up you should see a log message in the Xcode console saying
`LocalLambdaServer started and listening on 127.0.0.1:7000, receiving payloads on /invoke`
which means the local emulator is up and receiving traffic on port `7000` and expecting payloads on the `/invoke` endpoint.
`LocalLambdaServer started and listening on 127.0.0.1:7000, receiving events on /invoke`
which means the local emulator is up and receiving traffic on port `7000` and expecting events on the `/invoke` endpoint.

Continue to run the `MyApp` target
* Switch to the `MyApp` scheme and select a simulator destination.
Expand Down
6 changes: 3 additions & 3 deletions Sources/AWSLambdaEvents/Cloudwatch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import struct Foundation.Date

/// EventBridge has the same payloads/notification types as CloudWatch
/// EventBridge has the same events/notification types as CloudWatch
typealias EventBridge = Cloudwatch

public protocol CloudwatchDetail: Decodable {
Expand Down Expand Up @@ -66,7 +66,7 @@ public enum Cloudwatch {

let detailType = try container.decode(String.self, forKey: .detailType)
guard detailType.lowercased() == Detail.name.lowercased() else {
throw PayloadTypeMismatch(name: detailType, type: Detail.self)
throw DetailTypeMismatch(name: detailType, type: Detail.self)
}

self.detail = try container.decode(Detail.self, forKey: .detail)
Expand Down Expand Up @@ -122,7 +122,7 @@ public enum Cloudwatch {
}
}

struct PayloadTypeMismatch: Error {
struct DetailTypeMismatch: Error {
let name: String
let type: Any
}
Expand Down
10 changes: 5 additions & 5 deletions Sources/AWSLambdaRuntime/Lambda+Codable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import class Foundation.JSONEncoder
import NIO
import NIOFoundationCompat

/// Extension to the `Lambda` companion to enable execution of Lambdas that take and return `Codable` payloads.
/// Extension to the `Lambda` companion to enable execution of Lambdas that take and return `Codable` events.
extension Lambda {
/// An asynchronous Lambda Closure that takes a `In: Decodable` and returns a `Result<Out: Encodable, Error>` via a completion handler.
public typealias CodableClosure<In: Decodable, Out: Encodable> = (Lambda.Context, In, @escaping (Result<Out, Error>) -> Void) -> Void
Expand Down Expand Up @@ -57,8 +57,8 @@ internal struct CodableClosureWrapper<In: Decodable, Out: Encodable>: LambdaHand
self.closure = closure
}

func handle(context: Lambda.Context, payload: In, callback: @escaping (Result<Out, Error>) -> Void) {
self.closure(context, payload, callback)
func handle(context: Lambda.Context, event: In, callback: @escaping (Result<Out, Error>) -> Void) {
self.closure(context, event, callback)
}
}

Expand All @@ -72,8 +72,8 @@ internal struct CodableVoidClosureWrapper<In: Decodable>: LambdaHandler {
self.closure = closure
}

func handle(context: Lambda.Context, payload: In, callback: @escaping (Result<Out, Error>) -> Void) {
self.closure(context, payload, callback)
func handle(context: Lambda.Context, event: In, callback: @escaping (Result<Out, Error>) -> Void) {
self.closure(context, event, callback)
}
}

Expand Down
8 changes: 4 additions & 4 deletions Sources/AWSLambdaRuntimeCore/Lambda+LocalServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ import NIOHTTP1
// For example:
//
// try Lambda.withLocalServer {
// Lambda.run { (context: Lambda.Context, payload: String, callback: @escaping (Result<String, Error>) -> Void) in
// callback(.success("Hello, \(payload)!"))
// Lambda.run { (context: Lambda.Context, event: String, callback: @escaping (Result<String, Error>) -> Void) in
// callback(.success("Hello, \(event)!"))
// }
// }
extension Lambda {
/// Execute code in the context of a mock Lambda server.
///
/// - parameters:
/// - invocationEndpoint: The endpoint to post payloads to.
/// - invocationEndpoint: The endpoint to post events to.
/// - body: Code to run within the context of the mock server. Typically this would be a Lambda.run function call.
///
/// - note: This API is designed stricly for local testing and is behind a DEBUG flag
Expand Down Expand Up @@ -77,7 +77,7 @@ private enum LocalLambda {
guard channel.localAddress != nil else {
return channel.eventLoop.makeFailedFuture(ServerError.cantBind)
}
self.logger.info("LocalLambdaServer started and listening on \(self.host):\(self.port), receiving payloads on \(self.invocationEndpoint)")
self.logger.info("LocalLambdaServer started and listening on \(self.host):\(self.port), receiving events on \(self.invocationEndpoint)")
return channel.eventLoop.makeSucceededFuture(())
}
}
Expand Down
10 changes: 5 additions & 5 deletions Sources/AWSLambdaRuntimeCore/Lambda+String.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
//===----------------------------------------------------------------------===//
import NIO

/// Extension to the `Lambda` companion to enable execution of Lambdas that take and return `String` payloads.
/// Extension to the `Lambda` companion to enable execution of Lambdas that take and return `String` events.
extension Lambda {
/// An asynchronous Lambda Closure that takes a `String` and returns a `Result<String, Error>` via a completion handler.
public typealias StringClosure = (Lambda.Context, String, @escaping (Result<String, Error>) -> Void) -> Void
Expand Down Expand Up @@ -64,8 +64,8 @@ internal struct StringClosureWrapper: LambdaHandler {
self.closure = closure
}

func handle(context: Lambda.Context, payload: In, callback: @escaping (Result<Out, Error>) -> Void) {
self.closure(context, payload, callback)
func handle(context: Lambda.Context, event: In, callback: @escaping (Result<Out, Error>) -> Void) {
self.closure(context, event, callback)
}
}

Expand All @@ -79,8 +79,8 @@ internal struct StringVoidClosureWrapper: LambdaHandler {
self.closure = closure
}

func handle(context: Lambda.Context, payload: In, callback: @escaping (Result<Out, Error>) -> Void) {
self.closure(context, payload, callback)
func handle(context: Lambda.Context, event: In, callback: @escaping (Result<Out, Error>) -> Void) {
self.closure(context, event, callback)
}
}

Expand Down
22 changes: 11 additions & 11 deletions Sources/AWSLambdaRuntimeCore/LambdaHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ public protocol LambdaHandler: EventLoopLambdaHandler {
///
/// - parameters:
/// - context: Runtime `Context`.
/// - payload: Payload of type `In` representing the event or request.
/// - event: Event of type `In` representing the event or request.
/// - callback: Completion handler to report the result of the Lambda back to the runtime engine.
/// The completion handler expects a `Result` with either a response of type `Out` or an `Error`
func handle(context: Lambda.Context, payload: In, callback: @escaping (Result<Out, Error>) -> Void)
func handle(context: Lambda.Context, event: In, callback: @escaping (Result<Out, Error>) -> Void)
}

internal extension Lambda {
Expand All @@ -52,11 +52,11 @@ public extension LambdaHandler {
/// `LambdaHandler` is offloading the processing to a `DispatchQueue`
/// This is slower but safer, in case the implementation blocks the `EventLoop`
/// Performance sensitive Lambdas should be based on `EventLoopLambdaHandler` which does not offload.
func handle(context: Lambda.Context, payload: In) -> EventLoopFuture<Out> {
func handle(context: Lambda.Context, event: In) -> EventLoopFuture<Out> {
let promise = context.eventLoop.makePromise(of: Out.self)
// FIXME: reusable DispatchQueue
self.offloadQueue.async {
self.handle(context: context, payload: payload, callback: promise.completeWith)
self.handle(context: context, event: event, callback: promise.completeWith)
}
return promise.futureResult
}
Expand All @@ -80,11 +80,11 @@ public protocol EventLoopLambdaHandler: ByteBufferLambdaHandler {
///
/// - parameters:
/// - context: Runtime `Context`.
/// - payload: Payload of type `In` representing the event or request.
/// - event: Event of type `In` 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(context: Lambda.Context, payload: In) -> EventLoopFuture<Out>
func handle(context: Lambda.Context, event: In) -> EventLoopFuture<Out>

/// Encode a response of type `Out` to `ByteBuffer`
/// Concrete Lambda handlers implement this method to provide coding functionality.
Expand All @@ -107,12 +107,12 @@ public protocol EventLoopLambdaHandler: ByteBufferLambdaHandler {

public extension EventLoopLambdaHandler {
/// Driver for `ByteBuffer` -> `In` decoding and `Out` -> `ByteBuffer` encoding
func handle(context: Lambda.Context, payload: ByteBuffer) -> EventLoopFuture<ByteBuffer?> {
switch self.decodeIn(buffer: payload) {
func handle(context: Lambda.Context, event: ByteBuffer) -> EventLoopFuture<ByteBuffer?> {
switch self.decodeIn(buffer: event) {
case .failure(let error):
return context.eventLoop.makeFailedFuture(CodecError.requestDecoding(error))
case .success(let `in`):
return self.handle(context: context, payload: `in`).flatMapThrowing { out in
return self.handle(context: context, event: `in`).flatMapThrowing { out in
switch self.encodeOut(allocator: context.allocator, value: out) {
case .failure(let error):
throw CodecError.responseEncoding(error)
Expand Down Expand Up @@ -159,11 +159,11 @@ public protocol ByteBufferLambdaHandler {
///
/// - parameters:
/// - context: Runtime `Context`.
/// - payload: The event or request payload encoded as `ByteBuffer`.
/// - event: The event or input payload encoded as `ByteBuffer`.
///
/// - 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, payload: ByteBuffer) -> EventLoopFuture<ByteBuffer?>
func handle(context: Lambda.Context, event: ByteBuffer) -> EventLoopFuture<ByteBuffer?>
}

private enum CodecError: Error {
Expand Down
4 changes: 2 additions & 2 deletions Sources/AWSLambdaRuntimeCore/LambdaRunner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ 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, payload in
}.flatMap { invocation, event in
// 2. send invocation to handler
self.isGettingNextInvocation = false
let context = Context(logger: logger, eventLoop: self.eventLoop, invocation: invocation)
logger.debug("sending invocation to lambda handler \(handler)")
return handler.handle(context: context, payload: payload)
return handler.handle(context: context, event: event)
.mapResult { result in
if case .failure(let error) = result {
logger.warning("lambda handler returned an error: \(error)")
Expand Down
4 changes: 2 additions & 2 deletions Sources/AWSLambdaRuntimeCore/LambdaRuntimeClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ extension Lambda {
throw RuntimeError.badStatusCode(response.status)
}
let invocation = try Invocation(headers: response.headers)
guard let payload = response.body else {
guard let event = response.body else {
throw RuntimeError.noBody
}
return (invocation, payload)
return (invocation, event)
}.flatMapErrorThrowing { error in
switch error {
case HTTPClient.Errors.timeout:
Expand Down
24 changes: 12 additions & 12 deletions Sources/AWSLambdaTesting/Lambda+Testing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
// typealias In = String
// typealias Out = String
//
// func handle(context: Lambda.Context, payload: String) -> EventLoopFuture<String> {
// return context.eventLoop.makeSucceededFuture("echo" + payload)
// func handle(context: Lambda.Context, event: String) -> EventLoopFuture<String> {
// return context.eventLoop.makeSucceededFuture("echo" + event)
// }
// }
//
Expand Down Expand Up @@ -59,36 +59,36 @@ extension Lambda {
}

public static func test(_ closure: @escaping Lambda.StringClosure,
with payload: String,
with event: String,
using config: TestConfig = .init()) throws -> String {
try Self.test(StringClosureWrapper(closure), with: payload, using: config)
try Self.test(StringClosureWrapper(closure), with: event, using: config)
}

public static func test(_ closure: @escaping Lambda.StringVoidClosure,
with payload: String,
with event: String,
using config: TestConfig = .init()) throws {
_ = try Self.test(StringVoidClosureWrapper(closure), with: payload, using: config)
_ = try Self.test(StringVoidClosureWrapper(closure), with: event, using: config)
}

public static func test<In: Decodable, Out: Encodable>(
_ closure: @escaping Lambda.CodableClosure<In, Out>,
with payload: In,
with event: In,
using config: TestConfig = .init()
) throws -> Out {
try Self.test(CodableClosureWrapper(closure), with: payload, using: config)
try Self.test(CodableClosureWrapper(closure), with: event, using: config)
}

public static func test<In: Decodable>(
_ closure: @escaping Lambda.CodableVoidClosure<In>,
with payload: In,
with event: In,
using config: TestConfig = .init()
) throws {
_ = try Self.test(CodableVoidClosureWrapper(closure), with: payload, using: config)
_ = try Self.test(CodableVoidClosureWrapper(closure), with: event, using: config)
}

public static func test<In, Out, Handler: EventLoopLambdaHandler>(
_ handler: Handler,
with payload: In,
with event: In,
using config: TestConfig = .init()
) throws -> Out where Handler.In == In, Handler.Out == Out {
let logger = Logger(label: "test")
Expand All @@ -105,7 +105,7 @@ extension Lambda {
eventLoop: eventLoop)

return try eventLoop.flatSubmit {
handler.handle(context: context, payload: payload)
handler.handle(context: context, event: event)
}.wait()
}
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/CodableSample/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ struct Handler: EventLoopLambdaHandler {
typealias In = Request
typealias Out = Response

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

Expand Down
10 changes: 5 additions & 5 deletions Sources/StringSample/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ struct Handler: EventLoopLambdaHandler {
typealias In = String
typealias Out = String

func handle(context: Lambda.Context, payload: String) -> EventLoopFuture<String> {
// as an example, respond with the reverse the input payload
context.eventLoop.makeSucceededFuture(String(payload.reversed()))
func handle(context: Lambda.Context, event: String) -> EventLoopFuture<String> {
// as an example, respond with the event's reversed body
context.eventLoop.makeSucceededFuture(String(event.reversed()))
}
}

Expand All @@ -31,7 +31,7 @@ Lambda.run(Handler())
// MARK: - this can also be expressed as a closure:

/*
Lambda.run { (_, payload: String, callback) in
callback(.success(String(payload.reversed())))
Lambda.run { (_, event: String, callback) in
callback(.success(String(event.reversed())))
}
*/
6 changes: 3 additions & 3 deletions Tests/AWSLambdaEventsTests/ALBTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import XCTest

class ALBTests: XCTestCase {
static let exampleSingleValueHeadersPayload = """
static let exampleSingleValueHeadersEventBody = """
{
"requestContext":{
"elb":{
Expand Down Expand Up @@ -44,8 +44,8 @@ class ALBTests: XCTestCase {
}
"""

func testRequestWithSingleValueHeadersPayload() {
let data = ALBTests.exampleSingleValueHeadersPayload.data(using: .utf8)!
func testRequestWithSingleValueHeadersEvent() {
let data = ALBTests.exampleSingleValueHeadersEventBody.data(using: .utf8)!
do {
let decoder = JSONDecoder()

Expand Down
4 changes: 2 additions & 2 deletions Tests/AWSLambdaEventsTests/APIGateway+V2Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import XCTest

class APIGatewayV2Tests: XCTestCase {
static let exampleGetPayload = """
static let exampleGetEventBody = """
{
"routeKey":"GET /hello",
"version":"2.0",
Expand Down Expand Up @@ -77,7 +77,7 @@ class APIGatewayV2Tests: XCTestCase {
// MARK: Decoding

func testRequestDecodingExampleGetRequest() {
let data = APIGatewayV2Tests.exampleGetPayload.data(using: .utf8)!
let data = APIGatewayV2Tests.exampleGetEventBody.data(using: .utf8)!
var req: APIGateway.V2.Request?
XCTAssertNoThrow(req = try JSONDecoder().decode(APIGateway.V2.Request.self, from: data))

Expand Down
Loading

0 comments on commit 0008e59

Please sign in to comment.