diff --git a/Sources/AWSLambdaEvents/DynamoDB.swift b/Sources/AWSLambdaEvents/DynamoDB.swift index ab5fca9..40de379 100644 --- a/Sources/AWSLambdaEvents/DynamoDB.swift +++ b/Sources/AWSLambdaEvents/DynamoDB.swift @@ -17,6 +17,7 @@ import FoundationEssentials #else import Foundation #endif +import struct Foundation.Data // https://docs.aws.amazon.com/lambda/latest/dg/with-ddb.html public struct DynamoDBEvent: Decodable, Sendable { @@ -518,6 +519,9 @@ extension DynamoDBEvent { func decode(_ type: T.Type, forKey key: K) throws -> T where T: Decodable { let decoder = try self.decoderForKey(key) + if case .list(let list) = decoder.value, list.isEmpty { + return [] as! T + } return try T(from: decoder) } @@ -650,11 +654,14 @@ extension DynamoDBEvent { } func decode(_: String.Type) throws -> String { - guard case .string(let string) = self.value else { + switch self.value { + case .string(let string): + return string + case .binary(let binary): + return Data(binary).base64EncodedString() + default: throw self.createTypeMismatchError(type: String.self, value: self.value) } - - return string } func decode(_: Double.Type) throws -> Double {