Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
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
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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,6 @@ 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.
}

func handle(_ event: String, context: LambdaContext) async throws -> String {
"hello, world"
}
Expand Down
7 changes: 0 additions & 7 deletions Examples/Echo/Lambda.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,6 @@ import AWSLambdaRuntime

@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.
}

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
2 changes: 0 additions & 2 deletions Examples/ErrorHandling/Lambda.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ struct MyLambda: LambdaHandler {
typealias Event = Request
typealias Output = Response

init(context: LambdaInitializationContext) async throws {}

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
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
4 changes: 0 additions & 4 deletions Examples/JSON/Lambda.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ 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.
}

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
4 changes: 0 additions & 4 deletions Examples/LocalDebugging/MyLambda/Lambda.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ 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.
}

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
7 changes: 0 additions & 7 deletions Examples/Testing/Sources/Lambda.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,6 @@ import AWSLambdaRuntime

@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.
}

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.

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

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

/// Implementation of a`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 that 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 that 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 a`ByteBuffer` to `Event` decoding.
extension EventLoopLambdaHandler where Event: Decodable {
Expand All @@ -32,8 +70,8 @@ extension EventLoopLambdaHandler where Event: Decodable {
/// 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 {}
41 changes: 31 additions & 10 deletions Sources/AWSLambdaRuntimeCore/Lambda+String.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,46 @@
//===----------------------------------------------------------------------===//
import NIOCore

extension EventLoopLambdaHandler where Event == String {
// MARK: - LambdaHandler String support

@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
extension LambdaHandler where Event == String {
/// Implementation of a `ByteBuffer` to `String` decoding.
@inlinable
public func decode(buffer: ByteBuffer) throws -> String {
var buffer = buffer
guard let string = buffer.readString(length: buffer.readableBytes) else {
fatalError("buffer.readString(length: buffer.readableBytes) failed")
guard let value = buffer.getString(at: buffer.readerIndex, length: buffer.readableBytes) else {
throw CodecError.invalidString
}
return string
return value
}
}

extension EventLoopLambdaHandler where Output == String {
@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
extension LambdaHandler where Output == String {
/// Implementation of `String` to `ByteBuffer` encoding.
@inlinable
public func encode(allocator: ByteBufferAllocator, value: String) throws -> ByteBuffer? {
// FIXME: reusable buffer
var buffer = allocator.buffer(capacity: value.utf8.count)
public func encode(value: String, into buffer: inout ByteBuffer) throws {
buffer.writeString(value)
}
}

// MARK: - EventLoopLambdaHandler String support

extension EventLoopLambdaHandler where Event == String {
/// Implementation of `String` to `ByteBuffer` encoding.
@inlinable
public func decode(buffer: ByteBuffer) throws -> String {
guard let value = buffer.getString(at: buffer.readerIndex, length: buffer.readableBytes) else {
throw CodecError.invalidString
}
return value
}
}

extension EventLoopLambdaHandler where Output == String {
/// Implementation of a `ByteBuffer` to `String` decoding.
@inlinable
public func encode(value: String, into buffer: inout ByteBuffer) throws {
buffer.writeString(value)
return buffer
}
}
Loading