From 733b0d8939db53afdea186902f354dba3a2e9211 Mon Sep 17 00:00:00 2001 From: Fabian Fett Date: Sat, 25 Apr 2020 14:14:06 +0200 Subject: [PATCH] LambdaCodableEncoder should be less Foundation biased --- Sources/AWSLambdaRuntime/Lambda+Codable.swift | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) 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 + } +}