Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
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: 0 additions & 3 deletions Examples/Benchmark/BenchmarkHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ import NIOCore

@main
struct BenchmarkHandler: EventLoopLambdaHandler {
typealias Event = String
typealias Output = String

static func makeHandler(context: LambdaInitializationContext) -> EventLoopFuture<Self> {
context.eventLoop.makeSucceededFuture(BenchmarkHandler())
}
Expand Down
2 changes: 1 addition & 1 deletion Examples/Benchmark/Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.5
// swift-tools-version:5.7

import PackageDescription

Expand Down
2 changes: 1 addition & 1 deletion Examples/Deployment/Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.5
// swift-tools-version:5.7

import PackageDescription

Expand Down
5 changes: 1 addition & 4 deletions Examples/Deployment/Sources/Benchmark/BenchmarkHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
//===----------------------------------------------------------------------===//

import AWSLambdaRuntimeCore
import NIO
import NIOCore

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

@main
struct BenchmarkHandler: EventLoopLambdaHandler {
typealias Event = String
typealias Output = String

static func makeHandler(context: LambdaInitializationContext) -> EventLoopFuture<Self> {
context.eventLoop.makeSucceededFuture(BenchmarkHandler())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,7 @@ import AWSLambdaRuntime

// introductory example, the obligatory "hello, world!"
@main
struct HelloWorldHandler: LambdaHandler {
typealias Event = String
typealias Output = String

init(context: LambdaInitializationContext) async throws {
// setup your resources that you want to reuse here.
}

struct HelloWorldHandler: SimpleLambdaHandler {
func handle(_ event: String, context: LambdaContext) async throws -> String {
"hello, world"
}
Expand Down
9 changes: 1 addition & 8 deletions Examples/Echo/Lambda.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,7 @@ import AWSLambdaRuntime
// in this example we are receiving and responding with strings

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

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

struct MyLambda: SimpleLambdaHandler {
func handle(_ input: String, context: LambdaContext) async throws -> String {
// as an example, respond with the input's reversed
String(input.reversed())
Expand Down
2 changes: 1 addition & 1 deletion Examples/Echo/Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.5
// swift-tools-version:5.7

import PackageDescription

Expand Down
7 changes: 1 addition & 6 deletions Examples/ErrorHandling/Lambda.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,7 @@ import AWSLambdaRuntime
// MARK: - Run Lambda

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

init(context: LambdaInitializationContext) async throws {}

struct MyLambda: SimpleLambdaHandler {
func handle(_ request: Request, context: LambdaContext) async throws -> Response {
// switch over the error type "requested" by the request, and trigger such error accordingly
switch request.error {
Expand Down
2 changes: 1 addition & 1 deletion Examples/ErrorHandling/Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.5
// swift-tools-version:5.7

import PackageDescription

Expand Down
3 changes: 0 additions & 3 deletions Examples/Foundation/Lambda.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ import Logging

@main
struct MyLambda: LambdaHandler {
typealias Event = Request
typealias Output = [Exchange]

let calculator: ExchangeRatesCalculator

init(context: LambdaInitializationContext) async throws {
Expand Down
2 changes: 1 addition & 1 deletion Examples/Foundation/Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.5
// swift-tools-version:5.7

import PackageDescription

Expand Down
9 changes: 1 addition & 8 deletions Examples/JSON/Lambda.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,7 @@ struct Response: Codable {
// codables to model your request and response objects

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

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

struct MyLambda: SimpleLambdaHandler {
func handle(_ event: Request, context: LambdaContext) async throws -> Response {
// as an example, respond with the input event's reversed body
Response(body: String(event.body.reversed()))
Expand Down
2 changes: 1 addition & 1 deletion Examples/JSON/Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.5
// swift-tools-version:5.7

import PackageDescription

Expand Down
9 changes: 1 addition & 8 deletions Examples/LocalDebugging/MyLambda/Lambda.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,7 @@ import Shared
// a local server simulator which will allow local debugging

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

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

struct MyLambda: SimpleLambdaHandler {
func handle(_ request: Request, context: LambdaContext) async throws -> Response {
// TODO: something useful
Response(message: "Hello, \(request.name)!")
Expand Down
2 changes: 1 addition & 1 deletion Examples/LocalDebugging/MyLambda/Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.5
// swift-tools-version:5.7

import PackageDescription

Expand Down
2 changes: 1 addition & 1 deletion Examples/Testing/Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.5
// swift-tools-version:5.7

import PackageDescription

Expand Down
9 changes: 1 addition & 8 deletions Examples/Testing/Sources/Lambda.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,7 @@ import AWSLambdaRuntime
// in this example we are receiving and responding with strings

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

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

struct MyLambda: SimpleLambdaHandler {
func handle(_ event: String, context: LambdaContext) async throws -> String {
// as an example, respond with the event's reversed body
String(event.reversed())
Expand Down
4 changes: 2 additions & 2 deletions Examples/Testing/Tests/LambdaTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import AWSLambdaTesting
import XCTest

class LambdaTest: XCTestCase {
func testIt() throws {
func testIt() async throws {
let input = UUID().uuidString
let result = try Lambda.test(MyLambda.self, with: input)
let result = try await Lambda.test(MyLambda.self, with: input)
XCTAssertEqual(result, String(input.reversed()))
}
}
55 changes: 0 additions & 55 deletions [email protected]

This file was deleted.

55 changes: 0 additions & 55 deletions [email protected]

This file was deleted.

59 changes: 45 additions & 14 deletions Sources/AWSLambdaRuntime/Lambda+Codable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,59 @@ import class Foundation.JSONEncoder
import NIOCore
import NIOFoundationCompat

// MARK: - Codable support
// MARK: - LambdaHandler Codable support

/// Implementation of a`ByteBuffer` to `Event` decoding.
/// Implementation of `ByteBuffer` to `Event` decoding.
@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
extension LambdaHandler where Event: Decodable {
@inlinable
public func decode(buffer: ByteBuffer) throws -> Event {
try self.decoder.decode(Event.self, from: buffer)
}
}

/// Implementation of `Output` to `ByteBuffer` encoding.
@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
extension LambdaHandler where Output: Encodable {
@inlinable
public func encode(value: Output, into buffer: inout ByteBuffer) throws {
try self.encoder.encode(value, into: &buffer)
}
}

/// Default `ByteBuffer` to `Event` decoder using Foundation's `JSONDecoder`.
/// Advanced users who want to inject their own codec can do it by overriding these functions.
@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
extension LambdaHandler where Event: Decodable {
public var decoder: LambdaCodableDecoder {
Lambda.defaultJSONDecoder
}
}

/// Default `Output` to `ByteBuffer` encoder using Foundation's `JSONEncoder`.
/// Advanced users who want to inject their own codec can do it by overriding these functions.
@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
extension LambdaHandler where Output: Encodable {
public var encoder: LambdaCodableEncoder {
Lambda.defaultJSONEncoder
}
}

// MARK: - EventLoopLambdaHandler Codable support

/// Implementation of `ByteBuffer` to `Event` decoding.
extension EventLoopLambdaHandler where Event: Decodable {
@inlinable
public func decode(buffer: ByteBuffer) throws -> Event {
try self.decoder.decode(Event.self, from: buffer)
}
}

/// Implementation of `Output` to `ByteBuffer` encoding.
/// Implementation of `Output` to `ByteBuffer` encoding.
extension EventLoopLambdaHandler where Output: Encodable {
@inlinable
public func encode(allocator: ByteBufferAllocator, value: Output) throws -> ByteBuffer? {
try self.encoder.encode(value, using: allocator)
public func encode(value: Output, into buffer: inout ByteBuffer) throws {
try self.encoder.encode(value, into: &buffer)
}
}

Expand All @@ -58,7 +96,7 @@ public protocol LambdaCodableDecoder {
}

public protocol LambdaCodableEncoder {
func encode<T: Encodable>(_ value: T, using allocator: ByteBufferAllocator) throws -> ByteBuffer
func encode<T: Encodable>(_ value: T, into buffer: inout ByteBuffer) throws
}

extension Lambda {
Expand All @@ -68,11 +106,4 @@ extension Lambda {

extension JSONDecoder: LambdaCodableDecoder {}

extension JSONEncoder: LambdaCodableEncoder {
public func encode<T>(_ value: T, using allocator: ByteBufferAllocator) throws -> ByteBuffer where T: Encodable {
// nio will resize the buffer if necessary
var buffer = allocator.buffer(capacity: 1024)
try self.encode(value, into: &buffer)
return buffer
}
}
extension JSONEncoder: LambdaCodableEncoder {}
Loading