Skip to content

Commit 081be6c

Browse files
Add support for nil values for expressionAttributeValues
1 parent b7269f1 commit 081be6c

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

Sources/AwsDynamoDB/AwsDynamoDB.swift

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ public struct AwsDynamoDBTable {
171171
/// - completion: Completion closure that will be called when request has completed.
172172
/// - success: Bool value that will be `true` if request has succeeded, otherwise false.
173173
/// - error: Error if request has failed or `nil` if request has succeeded.
174-
public func update(key: (field :String, value: Any), conditionExpression: String? = nil, expressionAttributeNames: [String : String]? = nil, expressionAttributeValues: [String : Any]? = nil, updateExpression: String? = nil, completion: @escaping (Bool, Error?) -> Void) {
174+
public func update(key: (field :String, value: Any), conditionExpression: String? = nil, expressionAttributeNames: [String : String]? = nil, expressionAttributeValues: [String : Any?]? = nil, updateExpression: String? = nil, completion: @escaping (Bool, Error?) -> Void) {
175175
var params: [String : Any] = [ "TableName" : name,
176176
"Key" : toAwsJson(from: [key.field : key.value]) ]
177177
if let conditionExpression = conditionExpression {
@@ -349,7 +349,7 @@ public struct AwsDynamoDBTable {
349349
return toAwsJson(from: json)
350350
}
351351

352-
private func toAwsJson(from json: [String : Any]) -> [String : Any] {
352+
private func toAwsJson(from json: [String : Any?]) -> [String : Any] {
353353
var awsJson = [String : Any]()
354354

355355
json.forEach { (key, value) in
@@ -360,8 +360,11 @@ public struct AwsDynamoDBTable {
360360
return awsJson
361361
}
362362

363-
private func toAwsJsonValue(from value: Any) -> [String : Any]? {
364-
let value = "\(Mirror(reflecting: value).subjectType)" == "__NSCFBoolean" ? value as! Bool : value
363+
private func toAwsJsonValue(from value: Any?) -> [String : Any]? {
364+
guard var value = value else {
365+
return ["NULL" : true]
366+
}
367+
value = "\(Mirror(reflecting: value).subjectType)" == "__NSCFBoolean" ? value as! Bool : value
365368
switch value {
366369
case is String:
367370
return ["S" : value]
@@ -386,8 +389,6 @@ public struct AwsDynamoDBTable {
386389
case is [Data]:
387390
let dataArray = (value as! [Data]).map { $0.base64EncodedData() }
388391
return ["BS" : dataArray]
389-
case nil:
390-
return ["NULL" : true]
391392
default:
392393
return nil
393394
}

0 commit comments

Comments
 (0)