Skip to content

update and add examples to new APIs #228

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 3 commits into from
Sep 25, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion Examples/LambdaFunctions/Sources/Benchmark/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import NIO
// use this example which is more performant.
// `EventLoopLambdaHandler` does not offload the Lambda processing to a separate thread
// while the closure-based handlers do.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the description here should also be modified?

Lambda.run { $0.eventLoop.makeSucceededFuture(BenchmarkHandler()) }

struct BenchmarkHandler: EventLoopLambdaHandler {
typealias Event = String
Expand All @@ -29,3 +28,5 @@ struct BenchmarkHandler: EventLoopLambdaHandler {
context.eventLoop.makeSucceededFuture("hello, world!")
}
}

Lambda.run { $0.eventLoop.makeSucceededFuture(BenchmarkHandler()) }
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ import Shared

// set LOCAL_LAMBDA_SERVER_ENABLED env variable to "true" to start
// a local server simulator which will allow local debugging

@main
struct MyLambdaHandler: LambdaHandler {
struct MyLambda: LambdaHandler {
typealias Event = Request
typealias Output = Response

Expand Down
6 changes: 4 additions & 2 deletions Examples/LocalDebugging/MyLambda/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ let package = Package(
],
targets: [
.executableTarget(
name: "MyLambda", dependencies: [
name: "MyLambda",
dependencies: [
.product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"),
.product(name: "Shared", package: "Shared"),
]
],
path: "."
),
]
)
15 changes: 0 additions & 15 deletions Examples/LocalDebugging/MyLambda/Tests/LinuxMain.swift

This file was deleted.

15 changes: 0 additions & 15 deletions Examples/LocalDebugging/Shared/Tests/LinuxMain.swift

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the SwiftAWSLambdaRuntime open source project
//
// Copyright (c) 2017-2020 Apple Inc. and the SwiftAWSLambdaRuntime project authors
// Copyright (c) 2021 Apple Inc. and the SwiftAWSLambdaRuntime project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
Expand All @@ -24,23 +24,19 @@ struct Response: Codable {
}

// in this example we are receiving and responding with codables. Request and Response above are examples of how to use
// codables to model your reqeuest and response objects
struct Handler: EventLoopLambdaHandler {
// codables to model your request and response objects

@main
struct MyLambda: LambdaHandler {
typealias Event = Request
typealias Output = Response

func handle(_ event: Request, context: Lambda.Context) -> EventLoopFuture<Response> {
init(context: Lambda.InitializationContext) async throws {
// setup your resources that you want to reuse for every invocation here.
}

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

Lambda.run { $0.eventLoop.makeSucceededFuture(Handler()) }

// MARK: - this can also be expressed as a closure:

/*
Lambda.run { (_, request: Request, callback) in
callback(.success(Response(body: String(request.body.reversed()))))
}
*/
28 changes: 28 additions & 0 deletions Examples/Simple/Codable/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// swift-tools-version:5.5

import PackageDescription

let package = Package(
name: "swift-aws-lambda-runtime-example",
platforms: [
.macOS(.v12),
],
products: [
.executable(name: "MyLambda", targets: ["MyLambda"]),
],
dependencies: [
// this is the dependency on the swift-aws-lambda-runtime library
// in real-world projects this would say
// .package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", from: "1.0.0")
.package(name: "swift-aws-lambda-runtime", path: "../../.."),
],
targets: [
.executableTarget(
name: "MyLambda",
dependencies: [
.product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"),
],
path: "."
),
]
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the SwiftAWSLambdaRuntime open source project
//
// Copyright (c) 2017-2018 Apple Inc. and the SwiftAWSLambdaRuntime project authors
// Copyright (c) 2021 Apple Inc. and the SwiftAWSLambdaRuntime project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
Expand All @@ -13,17 +13,20 @@
//===----------------------------------------------------------------------===//

import AWSLambdaRuntimeCore
import NIOCore

// in this example we are receiving and responding with strings
struct Handler: EventLoopLambdaHandler {

@main
struct MyLambda: LambdaHandler {
typealias Event = String
typealias Output = String

func handle(_ event: String, context: Lambda.Context) -> EventLoopFuture<String> {
init(context: Lambda.InitializationContext) async throws {
// setup your resources that you want to reuse for every invocation here.
}

func handle(_ event: String, context: Lambda.Context) async throws -> String {
// as an example, respond with the event's reversed body
context.eventLoop.makeSucceededFuture(String(event.reversed()))
String(event.reversed())
}
}

Lambda.run { $0.eventLoop.makeSucceededFuture(Handler()) }
28 changes: 28 additions & 0 deletions Examples/Simple/String/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// swift-tools-version:5.5

import PackageDescription

let package = Package(
name: "swift-aws-lambda-runtime-example",
platforms: [
.macOS(.v12),
],
products: [
.executable(name: "MyLambda", targets: ["MyLambda"]),
],
dependencies: [
// this is the dependency on the swift-aws-lambda-runtime library
// in real-world projects this would say
// .package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", from: "1.0.0")
.package(name: "swift-aws-lambda-runtime", path: "../../.."),
],
targets: [
.executableTarget(
name: "MyLambda",
dependencies: [
.product(name: "AWSLambdaRuntimeCore", package: "swift-aws-lambda-runtime"),
],
path: "."
),
]
)
30 changes: 30 additions & 0 deletions Examples/Simple/Testing/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// swift-tools-version:5.5

import PackageDescription

let package = Package(
name: "swift-aws-lambda-runtime-example",
platforms: [
.macOS(.v12),
],
products: [
.executable(name: "MyLambda", targets: ["MyLambda"]),
],
dependencies: [
// this is the dependency on the swift-aws-lambda-runtime library
// in real-world projects this would say
// .package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", from: "1.0.0")
.package(name: "swift-aws-lambda-runtime", path: "../../.."),
],
targets: [
.executableTarget(
name: "MyLambda",
dependencies: [
.product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"),
.product(name: "AWSLambdaTesting", package: "swift-aws-lambda-runtime"),
],
path: "Sources"
),
.testTarget(name: "MyLambdaTests", dependencies: ["MyLambda"], path: "Tests"),
]
)
32 changes: 32 additions & 0 deletions Examples/Simple/Testing/Sources/Lambda.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftAWSLambdaRuntime open source project
//
// Copyright (c) 2021 Apple Inc. and the SwiftAWSLambdaRuntime project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

import AWSLambdaRuntime

// in this example we are receiving and responding with strings

@main
struct MyLambda: LambdaHandler {
typealias Event = String
typealias Output = String

init(context: Lambda.InitializationContext) async throws {
// setup your resources that you want to reuse for every invocation here.
}

func handle(_ event: String, context: Lambda.Context) async throws -> String {
// as an example, respond with the event's reversed body
String(event.reversed())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the SwiftAWSLambdaRuntime open source project
//
// Copyright (c) 2020 Apple Inc. and the SwiftAWSLambdaRuntime project authors
// Copyright (c) 2021 Apple Inc. and the SwiftAWSLambdaRuntime project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
Expand All @@ -12,4 +12,15 @@
//
//===----------------------------------------------------------------------===//

preconditionFailure("use `swift test --enable-test-discovery`")
import AWSLambdaRuntime
import AWSLambdaTesting
@testable import MyLambda
import XCTest

class LambdaTest: XCTestCase {
func testIt() throws {
let input = UUID().uuidString
let result = try Lambda.test(MyLambda.self, with: input)
XCTAssertEqual(result, String(input.reversed()))
}
}
2 changes: 0 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,5 @@ let package = Package(
.product(name: "NIOHTTP1", package: "swift-nio"),
.product(name: "NIO", package: "swift-nio"),
]),
.target(name: "StringSample", dependencies: ["AWSLambdaRuntime"]),
.target(name: "CodableSample", dependencies: ["AWSLambdaRuntime"]),
]
)
10 changes: 4 additions & 6 deletions Sources/AWSLambdaTesting/Lambda+Testing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,16 @@
//===----------------------------------------------------------------------===//

// This functionality is designed to help with Lambda unit testing with XCTest
// #if filter required for release builds which do not support @testable import
// @testable is used to access of internal functions
// For exmaple:
// For example:
//
// func test() {
// struct MyLambda: LambdaHandler {
// typealias In = String
// typealias Out = String
// typealias Event = String
// typealias Output = String
//
// init(context: Lambda.InitializationContext) {}
//
// func handle(event: String, context: Lambda.Context) async throws -> String {
// func handle(_ event: String, context: Lambda.Context) async throws -> String {
// "echo" + event
// }
// }
Expand Down
8 changes: 4 additions & 4 deletions Tests/AWSLambdaRuntimeCoreTests/LambdaHandlers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ import AWSLambdaRuntimeCore
import NIOCore

struct EchoHandler: EventLoopLambdaHandler {
typealias In = String
typealias Out = String
typealias Event = String
typealias Output = String

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

struct FailedHandler: EventLoopLambdaHandler {
typealias In = String
typealias Out = Void
typealias Event = String
typealias Output = Void

private let reason: String

Expand Down
2 changes: 1 addition & 1 deletion Tests/AWSLambdaRuntimeTests/Lambda+CodeableTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ class CodableLambdaTest: XCTestCase {
}
#endif

// convencience method
// convenience method
func newContext() -> Lambda.Context {
Lambda.Context(requestID: UUID().uuidString,
traceID: "abc123",
Expand Down
15 changes: 0 additions & 15 deletions Tests/LinuxMain.swift

This file was deleted.

6 changes: 5 additions & 1 deletion docker/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,12 @@ services:
<<: *common
command: >-
/bin/bash -clx "
swift build --package-path Examples/Simple/String &&
swift build --package-path Examples/Simple/Codable &&
swift test --package-path Examples/Simple/Testing &&
swift build --package-path Examples/LambdaFunctions &&
swift build --package-path Examples/LocalDebugging/MyLambda"
swift build --package-path Examples/LocalDebugging/MyLambda
"

# util

Expand Down
2 changes: 1 addition & 1 deletion scripts/soundness.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ here="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

function replace_acceptable_years() {
# this needs to replace all acceptable forms with 'YEARS'
sed -e 's/2017-2018/YEARS/' -e 's/2017-2020/YEARS/' -e 's/2017-2021/YEARS/' -e 's/2019/YEARS/' -e 's/2020/YEARS/'
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/'
}

printf "=> Checking for unacceptable language... "
Expand Down