Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update DynamoDB.Decoder to handle binary strings #61

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions Sources/AWSLambdaEvents/DynamoDB.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import struct Foundation.Date
#else
@preconcurrency import struct Foundation.Date
#endif
import struct Foundation.Data

// https://docs.aws.amazon.com/lambda/latest/dg/with-ddb.html
public struct DynamoDBEvent: Decodable, Sendable {
Expand Down Expand Up @@ -506,6 +507,9 @@ extension DynamoDBEvent {

func decode<T>(_ 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)
}

Expand Down Expand Up @@ -619,11 +623,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 {
Expand Down