mirror of
https://github.com/swift-server/swift-aws-lambda-runtime.git
synced 2026-06-02 07:27:33 +00:00
motivation: define stable API in preperation 1.0 release changes: * require swift 5.7, remove redundant backwards compatibility code * make LambdaHandler, EventLoopLambdaHandler, and ByteBufferLambdaHandler disjointed protocols to reduce API surface area * create coding wrappers for LambdaHandler and EventLoopLambdaHandler to provide bridge to ByteBufferLambdaHandler * reuse output ByteBuffer to reduce allocations * add new SimpleLambdaHandler with no-op initializer for simple lambda use cases * update callsites and tests * update examples Co-authored-by: Yim Lee <yim_lee@apple.com> Co-authored-by: Fabian Fett <fabianfett@apple.com>
78 lines
2.5 KiB
Swift
78 lines
2.5 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the SwiftAWSLambdaRuntime open source project
|
|
//
|
|
// Copyright (c) 2017-2022 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 NIOCore
|
|
|
|
// MARK: - SimpleLambdaHandler String support
|
|
|
|
extension SimpleLambdaHandler where Event == String {
|
|
/// Implementation of a `ByteBuffer` to `String` decoding.
|
|
@inlinable
|
|
public func decode(buffer: ByteBuffer) throws -> Event {
|
|
guard let value = buffer.getString(at: buffer.readerIndex, length: buffer.readableBytes) else {
|
|
throw CodecError.invalidString
|
|
}
|
|
return value
|
|
}
|
|
}
|
|
|
|
extension SimpleLambdaHandler where Output == String {
|
|
/// Implementation of `String` to `ByteBuffer` encoding.
|
|
@inlinable
|
|
public func encode(value: Output, into buffer: inout ByteBuffer) throws {
|
|
buffer.writeString(value)
|
|
}
|
|
}
|
|
|
|
// MARK: - LambdaHandler String support
|
|
|
|
extension LambdaHandler where Event == String {
|
|
/// Implementation of a `ByteBuffer` to `String` decoding.
|
|
@inlinable
|
|
public func decode(buffer: ByteBuffer) throws -> Event {
|
|
guard let value = buffer.getString(at: buffer.readerIndex, length: buffer.readableBytes) else {
|
|
throw CodecError.invalidString
|
|
}
|
|
return value
|
|
}
|
|
}
|
|
|
|
extension LambdaHandler where Output == String {
|
|
/// Implementation of `String` to `ByteBuffer` encoding.
|
|
@inlinable
|
|
public func encode(value: Output, 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 -> Event {
|
|
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: Output, into buffer: inout ByteBuffer) throws {
|
|
buffer.writeString(value)
|
|
}
|
|
}
|