|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the SwiftAWSLambdaRuntime open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2024 Apple Inc. and the SwiftAWSLambdaRuntime project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +import NIOCore |
| 16 | + |
| 17 | +/// The protocol a decoder must conform to so that it can be used with ``LambdaCodableAdapter`` to decode incoming |
| 18 | +/// ``ByteBuffer`` events. |
| 19 | +package protocol LambdaEventDecoder { |
| 20 | + /// Decode the ``ByteBuffer`` representing the received event into the generic ``Event`` type |
| 21 | + /// the handler will receive. |
| 22 | + /// - Parameters: |
| 23 | + /// - type: The type of the object to decode the buffer into. |
| 24 | + /// - buffer: The buffer to be decoded. |
| 25 | + /// - Returns: An object containing the decoded data. |
| 26 | + func decode<Event: Decodable>(_ type: Event.Type, from buffer: ByteBuffer) throws -> Event |
| 27 | +} |
| 28 | + |
| 29 | +/// The protocol an encoder must conform to so that it can be used with ``LambdaCodableAdapter`` to encode the generic |
| 30 | +/// ``Output`` object into a ``ByteBuffer``. |
| 31 | +package protocol LambdaOutputEncoder { |
| 32 | + associatedtype Output |
| 33 | + |
| 34 | + /// Encode the generic type `Output` the handler has returned into a ``ByteBuffer``. |
| 35 | + /// - Parameters: |
| 36 | + /// - value: The object to encode into a ``ByteBuffer``. |
| 37 | + /// - buffer: The ``ByteBuffer`` where the encoded value will be written to. |
| 38 | + func encode(_ value: Output, into buffer: inout ByteBuffer) throws |
| 39 | +} |
| 40 | + |
| 41 | +package struct VoidEncoder: LambdaOutputEncoder { |
| 42 | + package typealias Output = Void |
| 43 | + |
| 44 | + @inlinable |
| 45 | + package func encode(_ value: Void, into buffer: inout NIOCore.ByteBuffer) throws {} |
| 46 | +} |
| 47 | + |
| 48 | +/// Adapts a ``NewLambdaHandler`` conforming handler to conform to ``LambdaWithBackgroundProcessingHandler``. |
| 49 | +package struct LambdaHandlerAdapter< |
| 50 | + Event: Decodable, |
| 51 | + Output, |
| 52 | + Handler: NewLambdaHandler |
| 53 | +>: LambdaWithBackgroundProcessingHandler where Handler.Event == Event, Handler.Output == Output { |
| 54 | + @usableFromInline let handler: Handler |
| 55 | + |
| 56 | + /// Initializes an instance given a concrete handler. |
| 57 | + /// - Parameter handler: The ``NewLambdaHandler`` conforming handler that is to be adapted to ``LambdaWithBackgroundProcessingHandler``. |
| 58 | + @inlinable |
| 59 | + package init(handler: Handler) { |
| 60 | + self.handler = handler |
| 61 | + } |
| 62 | + |
| 63 | + /// Passes the generic ``Event`` object to the ``NewLambdaHandler/handle(_:context:)`` function, and |
| 64 | + /// the resulting output is then written to ``LambdaWithBackgroundProcessingHandler``'s `outputWriter`. |
| 65 | + /// - Parameters: |
| 66 | + /// - event: The received event. |
| 67 | + /// - outputWriter: The writer to write the computed response to. |
| 68 | + /// - context: The ``NewLambdaContext`` containing the invocation's metadata. |
| 69 | + @inlinable |
| 70 | + package func handle( |
| 71 | + _ event: Event, |
| 72 | + outputWriter: some LambdaResponseWriter<Output>, |
| 73 | + context: NewLambdaContext |
| 74 | + ) async throws { |
| 75 | + let output = try await self.handler.handle(event, context: context) |
| 76 | + try await outputWriter.write(output) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +/// Adapts a ``LambdaWithBackgroundProcessingHandler`` conforming handler to conform to ``StreamingLambdaHandler``. |
| 81 | +package struct LambdaCodableAdapter< |
| 82 | + Handler: LambdaWithBackgroundProcessingHandler, |
| 83 | + Event: Decodable, |
| 84 | + Output, |
| 85 | + Decoder: LambdaEventDecoder, |
| 86 | + Encoder: LambdaOutputEncoder |
| 87 | +>: StreamingLambdaHandler where Handler.Event == Event, Handler.Output == Output, Encoder.Output == Output { |
| 88 | + @usableFromInline let handler: Handler |
| 89 | + @usableFromInline let encoder: Encoder |
| 90 | + @usableFromInline let decoder: Decoder |
| 91 | + // |
| 92 | + @usableFromInline var byteBuffer: ByteBuffer = .init() |
| 93 | + |
| 94 | + /// Initializes an instance given an encoder, decoder, and a handler with a non-`Void` output. |
| 95 | + /// - Parameters: |
| 96 | + /// - encoder: The encoder object that will be used to encode the generic ``Output`` obtained from the `handler`'s `outputWriter` into a ``ByteBuffer``. |
| 97 | + /// - decoder: The decoder object that will be used to decode the received ``ByteBuffer`` event into the generic ``Event`` type served to the `handler`. |
| 98 | + /// - handler: The handler object. |
| 99 | + @inlinable |
| 100 | + package init(encoder: Encoder, decoder: Decoder, handler: Handler) where Output: Encodable { |
| 101 | + self.encoder = encoder |
| 102 | + self.decoder = decoder |
| 103 | + self.handler = handler |
| 104 | + } |
| 105 | + |
| 106 | + /// Initializes an instance given a decoder, and a handler with a `Void` output. |
| 107 | + /// - Parameters: |
| 108 | + /// - decoder: The decoder object that will be used to decode the received ``ByteBuffer`` event into the generic ``Event`` type served to the `handler`. |
| 109 | + /// - handler: The handler object. |
| 110 | + @inlinable |
| 111 | + package init(decoder: Decoder, handler: Handler) where Output == Void, Encoder == VoidEncoder { |
| 112 | + self.encoder = VoidEncoder() |
| 113 | + self.decoder = decoder |
| 114 | + self.handler = handler |
| 115 | + } |
| 116 | + |
| 117 | + /// A ``StreamingLambdaHandler/handle(_:responseWriter:context:)`` wrapper. |
| 118 | + /// - Parameters: |
| 119 | + /// - event: The received event. |
| 120 | + /// - outputWriter: The writer to write the computed response to. |
| 121 | + /// - context: The ``NewLambdaContext`` containing the invocation's metadata. |
| 122 | + @inlinable |
| 123 | + package mutating func handle<Writer: LambdaResponseStreamWriter>( |
| 124 | + _ request: ByteBuffer, |
| 125 | + responseWriter: Writer, |
| 126 | + context: NewLambdaContext |
| 127 | + ) async throws { |
| 128 | + let event = try self.decoder.decode(Event.self, from: request) |
| 129 | + |
| 130 | + let writer = LambdaCodableResponseWriter<Output, Encoder, Writer>( |
| 131 | + encoder: self.encoder, |
| 132 | + streamWriter: responseWriter |
| 133 | + ) |
| 134 | + try await self.handler.handle(event, outputWriter: writer, context: context) |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +/// A ``LambdaResponseStreamWriter`` wrapper that conforms to ``LambdaResponseWriter``. |
| 139 | +package struct LambdaCodableResponseWriter<Output, Encoder: LambdaOutputEncoder, Base: LambdaResponseStreamWriter>: |
| 140 | + LambdaResponseWriter |
| 141 | +where Output == Encoder.Output { |
| 142 | + @usableFromInline let underlyingStreamWriter: Base |
| 143 | + @usableFromInline let encoder: Encoder |
| 144 | + |
| 145 | + /// Initializes an instance given an encoder and an underlying ``LambdaResponseStreamWriter``. |
| 146 | + /// - Parameters: |
| 147 | + /// - encoder: The encoder object that will be used to encode the generic ``Output`` into a ``ByteBuffer``, which will then be passed to `streamWriter`. |
| 148 | + /// - streamWriter: The underlying ``LambdaResponseStreamWriter`` that will be wrapped. |
| 149 | + @inlinable |
| 150 | + package init(encoder: Encoder, streamWriter: Base) { |
| 151 | + self.encoder = encoder |
| 152 | + self.underlyingStreamWriter = streamWriter |
| 153 | + } |
| 154 | + |
| 155 | + @inlinable |
| 156 | + package func write(_ output: Output) async throws { |
| 157 | + var outputBuffer = ByteBuffer() |
| 158 | + try self.encoder.encode(output, into: &outputBuffer) |
| 159 | + try await self.underlyingStreamWriter.writeAndFinish(outputBuffer) |
| 160 | + } |
| 161 | +} |
0 commit comments