diff --git a/Examples/LambdaFunctions/Package.swift b/Examples/LambdaFunctions/Package.swift index 1c06c644..52a21b98 100644 --- a/Examples/LambdaFunctions/Package.swift +++ b/Examples/LambdaFunctions/Package.swift @@ -1,11 +1,11 @@ -// swift-tools-version:5.2 +// swift-tools-version:5.5 import PackageDescription let package = Package( name: "swift-aws-lambda-runtime-samples", platforms: [ - .macOS(.v10_13), + .macOS(.v12), ], products: [ // introductory example @@ -24,16 +24,16 @@ let package = Package( .package(name: "swift-aws-lambda-runtime", path: "../.."), ], targets: [ - .target(name: "HelloWorld", dependencies: [ - .product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"), - ]), - .target(name: "Benchmark", dependencies: [ + .executableTarget(name: "Benchmark", dependencies: [ .product(name: "AWSLambdaRuntimeCore", package: "swift-aws-lambda-runtime"), ]), - .target(name: "ErrorHandling", dependencies: [ + .executableTarget(name: "HelloWorld", dependencies: [ + .product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"), + ]), + .executableTarget(name: "ErrorHandling", dependencies: [ .product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"), ]), - .target(name: "CurrencyExchange", dependencies: [ + .executableTarget(name: "CurrencyExchange", dependencies: [ .product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"), ]), ] diff --git a/Examples/LambdaFunctions/Sources/Benchmark/main.swift b/Examples/LambdaFunctions/Sources/Benchmark/main.swift index 6e902a56..42ac289e 100644 --- a/Examples/LambdaFunctions/Sources/Benchmark/main.swift +++ b/Examples/LambdaFunctions/Sources/Benchmark/main.swift @@ -19,7 +19,7 @@ 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. -Lambda.run(BenchmarkHandler()) +Lambda.run { $0.eventLoop.makeSucceededFuture(BenchmarkHandler()) } struct BenchmarkHandler: EventLoopLambdaHandler { typealias In = String diff --git a/Examples/LambdaFunctions/Sources/CurrencyExchange/main.swift b/Examples/LambdaFunctions/Sources/CurrencyExchange/CurrencyExchangeHandler.swift similarity index 91% rename from Examples/LambdaFunctions/Sources/CurrencyExchange/main.swift rename to Examples/LambdaFunctions/Sources/CurrencyExchange/CurrencyExchangeHandler.swift index 149acb64..85a48f4f 100644 --- a/Examples/LambdaFunctions/Sources/CurrencyExchange/main.swift +++ b/Examples/LambdaFunctions/Sources/CurrencyExchange/CurrencyExchangeHandler.swift @@ -23,9 +23,30 @@ import Logging // MARK: - Run Lambda -Lambda.run { (context: Lambda.Context, _: Request, callback: @escaping (Result<[Exchange], Error>) -> Void) in - let calculator = ExchangeRatesCalculator() - calculator.run(logger: context.logger, callback: callback) +@main +struct CurrencyExchangeHandler: AsyncLambdaHandler { + typealias In = Request + typealias Out = [Exchange] + + let calculator: ExchangeRatesCalculator + + init(context: Lambda.InitializationContext) async throws { + // the ExchangeRatesCalculator() can be reused over and over + self.calculator = ExchangeRatesCalculator() + } + + func handle(event: Request, context: Lambda.Context) async throws -> [Exchange] { + try await withCheckedThrowingContinuation { continuation in + self.calculator.run(logger: context.logger) { result in + switch result { + case .success(let exchanges): + continuation.resume(returning: exchanges) + case .failure(let error): + continuation.resume(throwing: error) + } + } + } + } } // MARK: - Business Logic diff --git a/Examples/LambdaFunctions/Sources/ErrorHandling/main.swift b/Examples/LambdaFunctions/Sources/ErrorHandling/ErrorsHappenHandler.swift similarity index 65% rename from Examples/LambdaFunctions/Sources/ErrorHandling/main.swift rename to Examples/LambdaFunctions/Sources/ErrorHandling/ErrorsHappenHandler.swift index 9f2fce2e..866b7322 100644 --- a/Examples/LambdaFunctions/Sources/ErrorHandling/main.swift +++ b/Examples/LambdaFunctions/Sources/ErrorHandling/ErrorsHappenHandler.swift @@ -16,21 +16,29 @@ import AWSLambdaRuntime // MARK: - Run Lambda -// switch over the error type "requested" by the request, and trigger such error accordingly -Lambda.run { (context: Lambda.Context, request: Request, callback: (Result) -> Void) in - switch request.error { - // no error here! - case .none: - callback(.success(Response(awsRequestID: context.requestID, requestID: request.requestID, status: .ok))) - // trigger a "managed" error - domain specific business logic failure - case .managed: - callback(.success(Response(awsRequestID: context.requestID, requestID: request.requestID, status: .error))) - // trigger an "unmanaged" error - an unexpected Swift Error triggered while processing the request - case .unmanaged(let error): - callback(.failure(UnmanagedError(description: error))) - // trigger a "fatal" error - a panic type error which will crash the process - case .fatal: - fatalError("crash!") +@main +struct ErrorsHappenHandler: AsyncLambdaHandler { + typealias In = Request + typealias Out = Response + + init(context: Lambda.InitializationContext) async throws {} + + func handle(event request: Request, context: Lambda.Context) async throws -> Response { + // switch over the error type "requested" by the request, and trigger such error accordingly + switch request.error { + // no error here! + case .none: + return Response(awsRequestID: context.requestID, requestID: request.requestID, status: .ok) + // trigger a "managed" error - domain specific business logic failure + case .managed: + return Response(awsRequestID: context.requestID, requestID: request.requestID, status: .error) + // trigger an "unmanaged" error - an unexpected Swift Error triggered while processing the request + case .unmanaged(let error): + throw UnmanagedError(description: error) + // trigger a "fatal" error - a panic type error which will crash the process + case .fatal: + fatalError("crash!") + } } } diff --git a/Examples/LambdaFunctions/Sources/HelloWorld/main.swift b/Examples/LambdaFunctions/Sources/HelloWorld/HelloWorldHandler.swift similarity index 63% rename from Examples/LambdaFunctions/Sources/HelloWorld/main.swift rename to Examples/LambdaFunctions/Sources/HelloWorld/HelloWorldHandler.swift index 7535da97..8a0d7ce6 100644 --- a/Examples/LambdaFunctions/Sources/HelloWorld/main.swift +++ b/Examples/LambdaFunctions/Sources/HelloWorld/HelloWorldHandler.swift @@ -15,6 +15,16 @@ import AWSLambdaRuntime // introductory example, the obligatory "hello, world!" -Lambda.run { (_: Lambda.Context, _: String, callback: (Result) -> Void) in - callback(.success("hello, world!")) +@main +struct HelloWorldHandler: AsyncLambdaHandler { + typealias In = String + typealias Out = String + + init(context: Lambda.InitializationContext) async throws { + // setup your resources that you want to reuse here. + } + + func handle(event: String, context: Lambda.Context) async throws -> String { + "hello, world" + } } diff --git a/Examples/LocalDebugging/MyApp/MyApp.xcodeproj/project.pbxproj b/Examples/LocalDebugging/MyApp/MyApp.xcodeproj/project.pbxproj index fcf97682..9910fb6e 100644 --- a/Examples/LocalDebugging/MyApp/MyApp.xcodeproj/project.pbxproj +++ b/Examples/LocalDebugging/MyApp/MyApp.xcodeproj/project.pbxproj @@ -115,7 +115,7 @@ isa = PBXProject; attributes = { LastSwiftUpdateCheck = 1140; - LastUpgradeCheck = 1140; + LastUpgradeCheck = 1300; ORGANIZATIONNAME = "Tom Doron"; TargetAttributes = { F7B6C1F9246121E800607A89 = { @@ -205,6 +205,7 @@ CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; CLANG_WARN_STRICT_PROTOTYPES = YES; CLANG_WARN_SUSPICIOUS_MOVE = YES; @@ -229,7 +230,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 13.4; + IPHONEOS_DEPLOYMENT_TARGET = 15.0; MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; MTL_FAST_MATH = YES; ONLY_ACTIVE_ARCH = YES; @@ -265,6 +266,7 @@ CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; CLANG_WARN_STRICT_PROTOTYPES = YES; CLANG_WARN_SUSPICIOUS_MOVE = YES; @@ -283,7 +285,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 13.4; + IPHONEOS_DEPLOYMENT_TARGET = 15.0; MTL_ENABLE_DEBUG_INFO = NO; MTL_FAST_MATH = YES; SDKROOT = iphoneos; diff --git a/Examples/LocalDebugging/MyApp/MyApp.xcodeproj/xcshareddata/xcschemes/MyApp.xcscheme b/Examples/LocalDebugging/MyApp/MyApp.xcodeproj/xcshareddata/xcschemes/MyApp.xcscheme index dc471464..eb84eb92 100644 --- a/Examples/LocalDebugging/MyApp/MyApp.xcodeproj/xcshareddata/xcschemes/MyApp.xcscheme +++ b/Examples/LocalDebugging/MyApp/MyApp.xcodeproj/xcshareddata/xcschemes/MyApp.xcscheme @@ -1,6 +1,6 @@ ) -> Void) in - // TODO: something useful - callback(.success(Response(message: "Hello, \(request.name)!"))) +@main +struct MyLambdaHandler: AsyncLambdaHandler { + typealias In = Request + typealias Out = Response + + init(context: Lambda.InitializationContext) async throws { + // setup your resources that you want to reuse for every invocation here. + } + + func handle(event request: Request, context: Lambda.Context) async throws -> Response { + // TODO: something useful + Response(message: "Hello, \(request.name)!") + } }