|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the SwiftAWSLambdaRuntime open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2017-2022 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 | +#if FoundationJSONSupport |
| 16 | +import NIOCore |
| 17 | + |
| 18 | +#if canImport(FoundationEssentials) |
| 19 | +import FoundationEssentials |
| 20 | +#else |
| 21 | +import struct Foundation.Data |
| 22 | +import class Foundation.JSONDecoder |
| 23 | +import class Foundation.JSONEncoder |
| 24 | +#endif |
| 25 | + |
| 26 | +public struct LambdaJSONEventDecoder: LambdaEventDecoder { |
| 27 | + @usableFromInline let jsonDecoder: JSONDecoder |
| 28 | + |
| 29 | + @inlinable |
| 30 | + public init(_ jsonDecoder: JSONDecoder) { |
| 31 | + self.jsonDecoder = jsonDecoder |
| 32 | + } |
| 33 | + |
| 34 | + @inlinable |
| 35 | + public func decode<Event>(_ type: Event.Type, from buffer: NIOCore.ByteBuffer) throws -> Event |
| 36 | + where Event: Decodable { |
| 37 | + try buffer.getJSONDecodable( |
| 38 | + Event.self, |
| 39 | + decoder: self.jsonDecoder, |
| 40 | + at: buffer.readerIndex, |
| 41 | + length: buffer.readableBytes |
| 42 | + )! // must work, enough readable bytes |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +public struct LambdaJSONOutputEncoder<Output: Encodable>: LambdaOutputEncoder { |
| 47 | + @usableFromInline let jsonEncoder: JSONEncoder |
| 48 | + |
| 49 | + @inlinable |
| 50 | + public init(_ jsonEncoder: JSONEncoder) { |
| 51 | + self.jsonEncoder = jsonEncoder |
| 52 | + } |
| 53 | + |
| 54 | + @inlinable |
| 55 | + public func encode(_ value: Output, into buffer: inout ByteBuffer) throws { |
| 56 | + try buffer.writeJSONEncodable(value, encoder: self.jsonEncoder) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +extension LambdaCodableAdapter { |
| 61 | + /// Initializes an instance given an encoder, decoder, and a handler with a non-`Void` output. |
| 62 | + /// - Parameters: |
| 63 | + /// - encoder: The encoder object that will be used to encode the generic `Output` obtained from the `handler`'s `outputWriter` into a `ByteBuffer`. By default, a JSONEncoder is used. |
| 64 | + /// - decoder: The decoder object that will be used to decode the received `ByteBuffer` event into the generic `Event` type served to the `handler`. By default, a JSONDecoder is used. |
| 65 | + /// - handler: The handler object. |
| 66 | + public init( |
| 67 | + encoder: JSONEncoder = JSONEncoder(), |
| 68 | + decoder: JSONDecoder = JSONDecoder(), |
| 69 | + handler: sending Handler |
| 70 | + ) |
| 71 | + where |
| 72 | + Output: Encodable, |
| 73 | + Output == Handler.Output, |
| 74 | + Encoder == LambdaJSONOutputEncoder<Output>, |
| 75 | + Decoder == LambdaJSONEventDecoder |
| 76 | + { |
| 77 | + self.init( |
| 78 | + encoder: LambdaJSONOutputEncoder(encoder), |
| 79 | + decoder: LambdaJSONEventDecoder(decoder), |
| 80 | + handler: handler |
| 81 | + ) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +extension LambdaRuntime { |
| 86 | + /// Initialize an instance with a `LambdaHandler` defined in the form of a closure **with a non-`Void` return type**. |
| 87 | + /// - Parameters: |
| 88 | + /// - decoder: The decoder object that will be used to decode the incoming `ByteBuffer` event into the generic `Event` type. `JSONDecoder()` used as default. |
| 89 | + /// - encoder: The encoder object that will be used to encode the generic `Output` into a `ByteBuffer`. `JSONEncoder()` used as default. |
| 90 | + /// - body: The handler in the form of a closure. |
| 91 | + public convenience init<Event: Decodable, Output>( |
| 92 | + decoder: JSONDecoder = JSONDecoder(), |
| 93 | + encoder: JSONEncoder = JSONEncoder(), |
| 94 | + body: sending @escaping (Event, LambdaContext) async throws -> Output |
| 95 | + ) |
| 96 | + where |
| 97 | + Handler == LambdaCodableAdapter< |
| 98 | + LambdaHandlerAdapter<Event, Output, ClosureHandler<Event, Output>>, |
| 99 | + Event, |
| 100 | + Output, |
| 101 | + LambdaJSONEventDecoder, |
| 102 | + LambdaJSONOutputEncoder<Output> |
| 103 | + > |
| 104 | + { |
| 105 | + let handler = LambdaCodableAdapter( |
| 106 | + encoder: encoder, |
| 107 | + decoder: decoder, |
| 108 | + handler: LambdaHandlerAdapter(handler: ClosureHandler(body: body)) |
| 109 | + ) |
| 110 | + |
| 111 | + self.init(handler: handler) |
| 112 | + } |
| 113 | + |
| 114 | + /// Initialize an instance with a `LambdaHandler` defined in the form of a closure **with a `Void` return type**. |
| 115 | + /// - Parameter body: The handler in the form of a closure. |
| 116 | + /// - Parameter decoder: The decoder object that will be used to decode the incoming `ByteBuffer` event into the generic `Event` type. `JSONDecoder()` used as default. |
| 117 | + public convenience init<Event: Decodable>( |
| 118 | + decoder: JSONDecoder = JSONDecoder(), |
| 119 | + body: sending @escaping (Event, LambdaContext) async throws -> Void |
| 120 | + ) |
| 121 | + where |
| 122 | + Handler == LambdaCodableAdapter< |
| 123 | + LambdaHandlerAdapter<Event, Void, ClosureHandler<Event, Void>>, |
| 124 | + Event, |
| 125 | + Void, |
| 126 | + LambdaJSONEventDecoder, |
| 127 | + VoidEncoder |
| 128 | + > |
| 129 | + { |
| 130 | + let handler = LambdaCodableAdapter( |
| 131 | + decoder: LambdaJSONEventDecoder(decoder), |
| 132 | + handler: LambdaHandlerAdapter(handler: ClosureHandler(body: body)) |
| 133 | + ) |
| 134 | + |
| 135 | + self.init(handler: handler) |
| 136 | + } |
| 137 | +} |
| 138 | +#endif // trait: FoundationJSONSupport |
0 commit comments