Skip to content

Commit 9ef3ddb

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 9ef3ddb

File tree

20 files changed

+176
-87
lines changed

20 files changed

+176
-87
lines changed
File renamed without changes.

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 & 6 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
@@ -16,14 +16,18 @@ import AWSLambdaRuntimeCore
1616
import NIOCore
1717

1818
// in this example we are receiving and responding with strings
19-
struct Handler: EventLoopLambdaHandler {
19+
20+
@main
21+
struct MyLambda: LambdaHandler {
2022
typealias Event = String
2123
typealias Output = String
2224

23-
func handle(_ event: String, context: Lambda.Context) -> EventLoopFuture<String> {
25+
init(context: Lambda.InitializationContext) async throws {
26+
// setup your resources that you want to reuse for every invocation here.
27+
}
28+
29+
func handle(_ event: String, context: Lambda.Context) async throws -> String {
2430
// as an example, respond with the event's reversed body
25-
context.eventLoop.makeSucceededFuture(String(event.reversed()))
31+
String(event.reversed())
2632
}
2733
}
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+
)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the SwiftAWSLambdaRuntime open source project
4+
//
5+
// Copyright (c) 2021 Apple Inc. and the SwiftAWSLambdaRuntime project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
import AWSLambdaRuntime
16+
import NIOCore
17+
18+
// in this example we are receiving and responding with strings
19+
20+
@main
21+
struct MyLambda: LambdaHandler {
22+
typealias Event = String
23+
typealias Output = String
24+
25+
init(context: Lambda.InitializationContext) async throws {
26+
// setup your resources that you want to reuse for every invocation here.
27+
}
28+
29+
func handle(_ event: String, context: Lambda.Context) async throws -> String {
30+
// as an example, respond with the event's reversed body
31+
String(event.reversed())
32+
}
33+
}

Examples/LambdaFunctions/Tests/LinuxMain.swift renamed to Examples/Simple/Testing/Tests/LambdaTests.swift

Lines changed: 13 additions & 2 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) 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
@@ -12,4 +12,15 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15-
preconditionFailure("use `swift test --enable-test-discovery`")
15+
import AWSLambdaRuntime
16+
import AWSLambdaTesting
17+
@testable import MyLambda
18+
import XCTest
19+
20+
class LambdaTest: XCTestCase {
21+
func testIt() throws {
22+
let input = UUID().uuidString
23+
let result = try Lambda.test(MyLambda.self, with: input)
24+
XCTAssertEqual(result, String(input.reversed()))
25+
}
26+
}

Package.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,5 @@ let package = Package(
5252
.product(name: "NIOHTTP1", package: "swift-nio"),
5353
.product(name: "NIO", package: "swift-nio"),
5454
]),
55-
.target(name: "StringSample", dependencies: ["AWSLambdaRuntime"]),
56-
.target(name: "CodableSample", dependencies: ["AWSLambdaRuntime"]),
5755
]
5856
)

Sources/AWSLambdaTesting/Lambda+Testing.swift

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,16 @@
1313
//===----------------------------------------------------------------------===//
1414

1515
// This functionality is designed to help with Lambda unit testing with XCTest
16-
// #if filter required for release builds which do not support @testable import
17-
// @testable is used to access of internal functions
18-
// For exmaple:
16+
// For example:
1917
//
2018
// func test() {
2119
// struct MyLambda: LambdaHandler {
22-
// typealias In = String
23-
// typealias Out = String
20+
// typealias Event = String
21+
// typealias Output = String
2422
//
2523
// init(context: Lambda.InitializationContext) {}
2624
//
27-
// func handle(event: String, context: Lambda.Context) async throws -> String {
25+
// func handle(_ event: String, context: Lambda.Context) async throws -> String {
2826
// "echo" + event
2927
// }
3028
// }

Tests/AWSLambdaRuntimeCoreTests/LambdaHandlers.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@ import AWSLambdaRuntimeCore
1616
import NIOCore
1717

1818
struct EchoHandler: EventLoopLambdaHandler {
19-
typealias In = String
20-
typealias Out = String
19+
typealias Event = String
20+
typealias Output = String
2121

2222
func handle(_ event: String, context: Lambda.Context) -> EventLoopFuture<String> {
2323
context.eventLoop.makeSucceededFuture(event)
2424
}
2525
}
2626

2727
struct FailedHandler: EventLoopLambdaHandler {
28-
typealias In = String
29-
typealias Out = Void
28+
typealias Event = String
29+
typealias Output = Void
3030

3131
private let reason: String
3232

Tests/AWSLambdaRuntimeTests/Lambda+CodeableTest.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ class CodableLambdaTest: XCTestCase {
145145
}
146146
#endif
147147

148-
// convencience method
148+
// convenience method
149149
func newContext() -> Lambda.Context {
150150
Lambda.Context(requestID: UUID().uuidString,
151151
traceID: "abc123",

Tests/LinuxMain.swift

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

docker/docker-compose.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,12 @@ services:
3434
<<: *common
3535
command: >-
3636
/bin/bash -clx "
37+
swift build --package-path Examples/Simple/String &&
38+
swift build --package-path Examples/Simple/Codable &&
39+
swift test --package-path Examples/Simple/Testing &&
3740
swift build --package-path Examples/LambdaFunctions &&
38-
swift build --package-path Examples/LocalDebugging/MyLambda"
41+
swift build --package-path Examples/LocalDebugging/MyLambda
42+
"
3943
4044
# util
4145

scripts/soundness.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ here="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
1919

2020
function replace_acceptable_years() {
2121
# this needs to replace all acceptable forms with 'YEARS'
22-
sed -e 's/2017-2018/YEARS/' -e 's/2017-2020/YEARS/' -e 's/2017-2021/YEARS/' -e 's/2019/YEARS/' -e 's/2020/YEARS/'
22+
sed -e 's/2017-2018/YEARS/' -e 's/2017-2020/YEARS/' -e 's/2017-2021/YEARS/' -e 's/2019/YEARS/' -e 's/2020/YEARS/' -e 's/2021/YEARS/'
2323
}
2424

2525
printf "=> Checking for unacceptable language... "

0 commit comments

Comments
 (0)