Skip to content

Commit e861641

Browse files
committed
update and add examples to new APIs
motivation: keep code up to date with API changes changes: * take advantage of @main where possible * move the top level Sample code to a new "Simple" examples subdirectory * add a sample that demonstrates how to test a lambda now that SwiftPM can test executables directly * update the test-sample docker setup to build & test th new samples * fix a few typos and In/Out typealias left overs
1 parent 5a21193 commit e861641

File tree

19 files changed

+175
-88
lines changed

19 files changed

+175
-88
lines changed

Examples/LambdaFunctions/Sources/Benchmark/main.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import NIO
1919
// use this example which is more performant.
2020
// `EventLoopLambdaHandler` does not offload the Lambda processing to a separate thread
2121
// while the closure-based handlers do.
22-
Lambda.run { $0.eventLoop.makeSucceededFuture(BenchmarkHandler()) }
2322

2423
struct BenchmarkHandler: EventLoopLambdaHandler {
2524
typealias Event = String
@@ -29,3 +28,5 @@ struct BenchmarkHandler: EventLoopLambdaHandler {
2928
context.eventLoop.makeSucceededFuture("hello, world!")
3029
}
3130
}
31+
32+
Lambda.run { $0.eventLoop.makeSucceededFuture(BenchmarkHandler()) }

Examples/LocalDebugging/MyLambda/Sources/MyLambda/MyLambdaHandler.swift renamed to Examples/LocalDebugging/MyLambda/Lambda.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ import Shared
1717

1818
// set LOCAL_LAMBDA_SERVER_ENABLED env variable to "true" to start
1919
// a local server simulator which will allow local debugging
20+
2021
@main
21-
struct MyLambdaHandler: LambdaHandler {
22+
struct MyLambda: LambdaHandler {
2223
typealias Event = Request
2324
typealias Output = Response
2425

Examples/LocalDebugging/MyLambda/Package.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ let package = Package(
1919
],
2020
targets: [
2121
.executableTarget(
22-
name: "MyLambda", dependencies: [
22+
name: "MyLambda",
23+
dependencies: [
2324
.product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"),
2425
.product(name: "Shared", package: "Shared"),
25-
]
26+
],
27+
path: "."
2628
),
2729
]
2830
)

Examples/LocalDebugging/MyLambda/Tests/LinuxMain.swift

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

Examples/LocalDebugging/Shared/Tests/LinuxMain.swift

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

Sources/CodableSample/main.swift renamed to Examples/Simple/Codable/Lambda.swift

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the SwiftAWSLambdaRuntime open source project
44
//
5-
// Copyright (c) 2017-2020 Apple Inc. and the SwiftAWSLambdaRuntime project authors
5+
// Copyright (c) 2021 Apple Inc. and the SwiftAWSLambdaRuntime project authors
66
// Licensed under Apache License v2.0
77
//
88
// See LICENSE.txt for license information
@@ -24,23 +24,19 @@ struct Response: Codable {
2424
}
2525

2626
// in this example we are receiving and responding with codables. Request and Response above are examples of how to use
27-
// codables to model your reqeuest and response objects
28-
struct Handler: EventLoopLambdaHandler {
27+
// codables to model your request and response objects
28+
29+
@main
30+
struct MyLambda: LambdaHandler {
2931
typealias Event = Request
3032
typealias Output = Response
3133

32-
func handle(_ event: Request, context: Lambda.Context) -> EventLoopFuture<Response> {
34+
init(context: Lambda.InitializationContext) async throws {
35+
// setup your resources that you want to reuse for every invocation here.
36+
}
37+
38+
func handle(_ event: Request, context: Lambda.Context) async throws -> Response {
3339
// as an example, respond with the input event's reversed body
34-
context.eventLoop.makeSucceededFuture(Response(body: String(event.body.reversed())))
40+
Response(body: String(event.body.reversed()))
3541
}
3642
}
37-
38-
Lambda.run { $0.eventLoop.makeSucceededFuture(Handler()) }
39-
40-
// MARK: - this can also be expressed as a closure:
41-
42-
/*
43-
Lambda.run { (_, request: Request, callback) in
44-
callback(.success(Response(body: String(request.body.reversed()))))
45-
}
46-
*/

Examples/Simple/Codable/Package.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// swift-tools-version:5.5
2+
3+
import PackageDescription
4+
5+
let package = Package(
6+
name: "swift-aws-lambda-runtime-example",
7+
platforms: [
8+
.macOS(.v12),
9+
],
10+
products: [
11+
.executable(name: "MyLambda", targets: ["MyLambda"]),
12+
],
13+
dependencies: [
14+
// this is the dependency on the swift-aws-lambda-runtime library
15+
// in real-world projects this would say
16+
// .package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", from: "1.0.0")
17+
.package(name: "swift-aws-lambda-runtime", path: "../../.."),
18+
],
19+
targets: [
20+
.executableTarget(
21+
name: "MyLambda",
22+
dependencies: [
23+
.product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"),
24+
],
25+
path: "."
26+
),
27+
]
28+
)

Sources/StringSample/main.swift renamed to Examples/Simple/String/Lambda.swift

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the SwiftAWSLambdaRuntime open source project
44
//
5-
// Copyright (c) 2017-2018 Apple Inc. and the SwiftAWSLambdaRuntime project authors
5+
// Copyright (c) 2021 Apple Inc. and the SwiftAWSLambdaRuntime project authors
66
// Licensed under Apache License v2.0
77
//
88
// See LICENSE.txt for license information
@@ -13,17 +13,20 @@
1313
//===----------------------------------------------------------------------===//
1414

1515
import AWSLambdaRuntimeCore
16-
import NIOCore
1716

1817
// in this example we are receiving and responding with strings
19-
struct Handler: EventLoopLambdaHandler {
18+
19+
@main
20+
struct MyLambda: LambdaHandler {
2021
typealias Event = String
2122
typealias Output = String
2223

23-
func handle(_ event: String, context: Lambda.Context) -> EventLoopFuture<String> {
24+
init(context: Lambda.InitializationContext) async throws {
25+
// setup your resources that you want to reuse for every invocation here.
26+
}
27+
28+
func handle(_ event: String, context: Lambda.Context) async throws -> String {
2429
// as an example, respond with the event's reversed body
25-
context.eventLoop.makeSucceededFuture(String(event.reversed()))
30+
String(event.reversed())
2631
}
2732
}
28-
29-
Lambda.run { $0.eventLoop.makeSucceededFuture(Handler()) }

Examples/Simple/String/Package.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// swift-tools-version:5.5
2+
3+
import PackageDescription
4+
5+
let package = Package(
6+
name: "swift-aws-lambda-runtime-example",
7+
platforms: [
8+
.macOS(.v12),
9+
],
10+
products: [
11+
.executable(name: "MyLambda", targets: ["MyLambda"]),
12+
],
13+
dependencies: [
14+
// this is the dependency on the swift-aws-lambda-runtime library
15+
// in real-world projects this would say
16+
// .package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", from: "1.0.0")
17+
.package(name: "swift-aws-lambda-runtime", path: "../../.."),
18+
],
19+
targets: [
20+
.executableTarget(
21+
name: "MyLambda",
22+
dependencies: [
23+
.product(name: "AWSLambdaRuntimeCore", package: "swift-aws-lambda-runtime"),
24+
],
25+
path: "."
26+
),
27+
]
28+
)

Examples/Simple/Testing/Package.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// swift-tools-version:5.5
2+
3+
import PackageDescription
4+
5+
let package = Package(
6+
name: "swift-aws-lambda-runtime-example",
7+
platforms: [
8+
.macOS(.v12),
9+
],
10+
products: [
11+
.executable(name: "MyLambda", targets: ["MyLambda"]),
12+
],
13+
dependencies: [
14+
// this is the dependency on the swift-aws-lambda-runtime library
15+
// in real-world projects this would say
16+
// .package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", from: "1.0.0")
17+
.package(name: "swift-aws-lambda-runtime", path: "../../.."),
18+
],
19+
targets: [
20+
.executableTarget(
21+
name: "MyLambda",
22+
dependencies: [
23+
.product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"),
24+
.product(name: "AWSLambdaTesting", package: "swift-aws-lambda-runtime"),
25+
],
26+
path: "Sources"
27+
),
28+
.testTarget(name: "MyLambdaTests", dependencies: ["MyLambda"], path: "Tests"),
29+
]
30+
)

0 commit comments

Comments
 (0)