Skip to content

Commit c915322

Browse files
tomerdyim-leefabianfett
authored
API Refactoring (#273)
motivation: define stable API in preperation 1.0 release changes: * require swift 5.7, remove redundant backwards compatibility code * make LambdaHandler, EventLoopLambdaHandler, and ByteBufferLambdaHandler disjointed protocols to reduce API surface area * create coding wrappers for LambdaHandler and EventLoopLambdaHandler to provide bridge to ByteBufferLambdaHandler * reuse output ByteBuffer to reduce allocations * add new SimpleLambdaHandler with no-op initializer for simple lambda use cases * update callsites and tests * update examples Co-authored-by: Yim Lee <yim_lee@apple.com> Co-authored-by: Fabian Fett <fabianfett@apple.com>
1 parent 98a23b6 commit c915322

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+798
-580
lines changed

Examples/Benchmark/BenchmarkHandler.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@ import NIOCore
2222

2323
@main
2424
struct BenchmarkHandler: EventLoopLambdaHandler {
25-
typealias Event = String
26-
typealias Output = String
27-
2825
static func makeHandler(context: LambdaInitializationContext) -> EventLoopFuture<Self> {
2926
context.eventLoop.makeSucceededFuture(BenchmarkHandler())
3027
}

Examples/Benchmark/Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version:5.5
1+
// swift-tools-version:5.7
22

33
import PackageDescription
44

Examples/Deployment/Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version:5.5
1+
// swift-tools-version:5.7
22

33
import PackageDescription
44

Examples/Deployment/Sources/Benchmark/BenchmarkHandler.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
//===----------------------------------------------------------------------===//
1414

1515
import AWSLambdaRuntimeCore
16-
import NIO
16+
import NIOCore
1717

1818
// If you would like to benchmark Swift's Lambda Runtime,
1919
// use this example which is more performant.
@@ -22,9 +22,6 @@ import NIO
2222

2323
@main
2424
struct BenchmarkHandler: EventLoopLambdaHandler {
25-
typealias Event = String
26-
typealias Output = String
27-
2825
static func makeHandler(context: LambdaInitializationContext) -> EventLoopFuture<Self> {
2926
context.eventLoop.makeSucceededFuture(BenchmarkHandler())
3027
}

Examples/Deployment/Sources/HelloWorld/HelloWorldHandler.swift

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,7 @@ import AWSLambdaRuntime
1616

1717
// introductory example, the obligatory "hello, world!"
1818
@main
19-
struct HelloWorldHandler: LambdaHandler {
20-
typealias Event = String
21-
typealias Output = String
22-
23-
init(context: LambdaInitializationContext) async throws {
24-
// setup your resources that you want to reuse here.
25-
}
26-
19+
struct HelloWorldHandler: SimpleLambdaHandler {
2720
func handle(_ event: String, context: LambdaContext) async throws -> String {
2821
"hello, world"
2922
}

Examples/Echo/Lambda.swift

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,7 @@ import AWSLambdaRuntime
1717
// in this example we are receiving and responding with strings
1818

1919
@main
20-
struct MyLambda: LambdaHandler {
21-
typealias Event = String
22-
typealias Output = String
23-
24-
init(context: LambdaInitializationContext) async throws {
25-
// setup your resources that you want to reuse for every invocation here.
26-
}
27-
20+
struct MyLambda: SimpleLambdaHandler {
2821
func handle(_ input: String, context: LambdaContext) async throws -> String {
2922
// as an example, respond with the input's reversed
3023
String(input.reversed())

Examples/Echo/Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version:5.5
1+
// swift-tools-version:5.7
22

33
import PackageDescription
44

Examples/ErrorHandling/Lambda.swift

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,7 @@ import AWSLambdaRuntime
1717
// MARK: - Run Lambda
1818

1919
@main
20-
struct MyLambda: LambdaHandler {
21-
typealias Event = Request
22-
typealias Output = Response
23-
24-
init(context: LambdaInitializationContext) async throws {}
25-
20+
struct MyLambda: SimpleLambdaHandler {
2621
func handle(_ request: Request, context: LambdaContext) async throws -> Response {
2722
// switch over the error type "requested" by the request, and trigger such error accordingly
2823
switch request.error {

Examples/ErrorHandling/Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version:5.5
1+
// swift-tools-version:5.7
22

33
import PackageDescription
44

Examples/Foundation/Lambda.swift

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

2626
@main
2727
struct MyLambda: LambdaHandler {
28-
typealias Event = Request
29-
typealias Output = [Exchange]
30-
3128
let calculator: ExchangeRatesCalculator
3229

3330
init(context: LambdaInitializationContext) async throws {

Examples/Foundation/Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version:5.5
1+
// swift-tools-version:5.7
22

33
import PackageDescription
44

Examples/JSON/Lambda.swift

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,7 @@ struct Response: Codable {
2626
// codables to model your request and response objects
2727

2828
@main
29-
struct MyLambda: LambdaHandler {
30-
typealias Event = Request
31-
typealias Output = Response
32-
33-
init(context: LambdaInitializationContext) async throws {
34-
// setup your resources that you want to reuse for every invocation here.
35-
}
36-
29+
struct MyLambda: SimpleLambdaHandler {
3730
func handle(_ event: Request, context: LambdaContext) async throws -> Response {
3831
// as an example, respond with the input event's reversed body
3932
Response(body: String(event.body.reversed()))

Examples/JSON/Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version:5.5
1+
// swift-tools-version:5.7
22

33
import PackageDescription
44

Examples/LocalDebugging/MyLambda/Lambda.swift

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,7 @@ import Shared
1919
// a local server simulator which will allow local debugging
2020

2121
@main
22-
struct MyLambda: LambdaHandler {
23-
typealias Event = Request
24-
typealias Output = Response
25-
26-
init(context: LambdaInitializationContext) async throws {
27-
// setup your resources that you want to reuse for every invocation here.
28-
}
29-
22+
struct MyLambda: SimpleLambdaHandler {
3023
func handle(_ request: Request, context: LambdaContext) async throws -> Response {
3124
// TODO: something useful
3225
Response(message: "Hello, \(request.name)!")

Examples/LocalDebugging/MyLambda/Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version:5.5
1+
// swift-tools-version:5.7
22

33
import PackageDescription
44

Examples/Testing/Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version:5.5
1+
// swift-tools-version:5.7
22

33
import PackageDescription
44

Examples/Testing/Sources/Lambda.swift

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,7 @@ import AWSLambdaRuntime
1717
// in this example we are receiving and responding with strings
1818

1919
@main
20-
struct MyLambda: LambdaHandler {
21-
typealias Event = String
22-
typealias Output = String
23-
24-
init(context: LambdaInitializationContext) async throws {
25-
// setup your resources that you want to reuse for every invocation here.
26-
}
27-
20+
struct MyLambda: SimpleLambdaHandler {
2821
func handle(_ event: String, context: LambdaContext) async throws -> String {
2922
// as an example, respond with the event's reversed body
3023
String(event.reversed())

Examples/Testing/Tests/LambdaTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ import AWSLambdaTesting
1818
import XCTest
1919

2020
class LambdaTest: XCTestCase {
21-
func testIt() throws {
21+
func testIt() async throws {
2222
let input = UUID().uuidString
23-
let result = try Lambda.test(MyLambda.self, with: input)
23+
let result = try await Lambda.test(MyLambda.self, with: input)
2424
XCTAssertEqual(result, String(input.reversed()))
2525
}
2626
}

Package.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
// swift-tools-version:5.6
1+
// swift-tools-version:5.7
22

33
import PackageDescription
44

55
let package = Package(
66
name: "swift-aws-lambda-runtime",
7+
platforms: [
8+
.macOS(.v12),
9+
.iOS(.v15),
10+
.tvOS(.v15),
11+
.watchOS(.v8),
12+
],
713
products: [
814
// this library exports `AWSLambdaRuntimeCore` and adds Foundation convenience methods
915
.library(name: "AWSLambdaRuntime", targets: ["AWSLambdaRuntime"]),
@@ -15,7 +21,7 @@ let package = Package(
1521
.library(name: "AWSLambdaTesting", targets: ["AWSLambdaTesting"]),
1622
],
1723
dependencies: [
18-
.package(url: "https://github.com/apple/swift-nio.git", .upToNextMajor(from: "2.33.0")),
24+
.package(url: "https://github.com/apple/swift-nio.git", .upToNextMajor(from: "2.43.1")),
1925
.package(url: "https://github.com/apple/swift-log.git", .upToNextMajor(from: "1.4.2")),
2026
.package(url: "https://github.com/swift-server/swift-backtrace.git", .upToNextMajor(from: "1.2.3")),
2127
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"),

Package@swift-5.4.swift

Lines changed: 0 additions & 55 deletions
This file was deleted.

Package@swift-5.5.swift

Lines changed: 0 additions & 55 deletions
This file was deleted.

0 commit comments

Comments
 (0)