diff --git a/Sources/AWSLambdaRuntime/Lambda+Codable.swift b/Sources/AWSLambdaRuntime/Lambda+Codable.swift index 81445c43..d2ae5ebb 100644 --- a/Sources/AWSLambdaRuntime/Lambda+Codable.swift +++ b/Sources/AWSLambdaRuntime/Lambda+Codable.swift @@ -92,10 +92,7 @@ public extension EventLoopLambdaHandler where In: Decodable { /// Implementation of `Out` to `ByteBuffer` encoding public extension EventLoopLambdaHandler where Out: Encodable { func encode(allocator: ByteBufferAllocator, value: Out) throws -> ByteBuffer? { - // nio will resize the buffer if necessary - var buffer = allocator.buffer(capacity: 1024) - try self.encoder.encode(value, into: &buffer) - return buffer + try self.encoder.encode(value, using: allocator) } } @@ -120,7 +117,7 @@ public protocol LambdaCodableDecoder { } public protocol LambdaCodableEncoder { - func encode(_ value: T, into buffer: inout ByteBuffer) throws + func encode(_ value: T, using allocator: ByteBufferAllocator) throws -> ByteBuffer } private extension Lambda { @@ -129,4 +126,11 @@ private extension Lambda { } extension JSONDecoder: LambdaCodableDecoder {} -extension JSONEncoder: LambdaCodableEncoder {} +extension JSONEncoder: LambdaCodableEncoder { + public func encode(_ 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 + } +}