-
Notifications
You must be signed in to change notification settings - Fork 18
Adds various null checks required for AWS SDK v4 #88
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
base: master
Are you sure you want to change the base?
Changes from 7 commits
5e055fe
2b99e3d
eeacc63
9608c99
1cf08fa
8751759
3f76966
a042d1e
956abd7
fb2a1e3
fb4d91a
80eb473
2fcbfb5
e11260f
6f0c6ac
cd78f70
3c5ee5d
c78f8ee
94e692f
e0e4789
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -280,7 +280,7 @@ type TableContext<'TRecord> | |
| failwithf "GetItem request returned error %O" response.HttpStatusCode | ||
|
|
||
| if response.IsItemSet then | ||
| return Some response.Item | ||
| return Option.ofObj response.Item | ||
| else | ||
| return None | ||
| } | ||
|
|
@@ -311,11 +311,11 @@ type TableContext<'TRecord> | |
| let! ct = Async.CancellationToken | ||
| let! response = client.BatchGetItemAsync(request, ct) |> Async.AwaitTaskCorrect | ||
| maybeReport | ||
| |> Option.iter (fun r -> r BatchGetItems (List.ofSeq response.ConsumedCapacity) response.Responses[tableName].Count) | ||
| |> Option.iter (fun r -> r BatchGetItems (if isNull response.ConsumedCapacity then [] else List.ofSeq response.ConsumedCapacity) (if isNull response.Responses then 0 else response.Responses[tableName].Count)) | ||
| if response.HttpStatusCode <> HttpStatusCode.OK then | ||
| failwithf "BatchGetItem request returned error %O" response.HttpStatusCode | ||
|
|
||
| return response.Responses[tableName] | ||
| return if isNull response.Responses then ResizeArray() else response.Responses[tableName] | ||
| } | ||
|
|
||
| let queryPaginatedAsync | ||
|
|
@@ -374,7 +374,9 @@ type TableContext<'TRecord> | |
| emitMetrics () | ||
| failwithf "Query request returned error %O" response.HttpStatusCode | ||
|
|
||
| downloaded.AddRange response.Items | ||
| if notNull response.Items then | ||
| downloaded.AddRange response.Items | ||
|
|
||
| if response.LastEvaluatedKey <> null && response.LastEvaluatedKey.Count > 0 then | ||
| lastEvaluatedKey <- Some response.LastEvaluatedKey | ||
| if limit.IsDownloadIncomplete downloaded.Count then | ||
|
|
@@ -456,7 +458,9 @@ type TableContext<'TRecord> | |
| emitMetrics () | ||
| failwithf "Scan request returned error %O" response.HttpStatusCode | ||
|
|
||
| downloaded.AddRange response.Items | ||
| if notNull response.Items then | ||
| downloaded.AddRange response.Items | ||
|
|
||
| consumedCapacity.Add response.ConsumedCapacity | ||
| if response.LastEvaluatedKey <> null && response.LastEvaluatedKey.Count > 0 then | ||
| lastEvaluatedKey <- Some response.LastEvaluatedKey | ||
|
|
@@ -578,15 +582,18 @@ type TableContext<'TRecord> | |
| let! ct = Async.CancellationToken | ||
| let! response = client.BatchWriteItemAsync(pbr, ct) |> Async.AwaitTaskCorrect | ||
| let unprocessed = | ||
| match response.UnprocessedItems.TryGetValue tableName with | ||
| | true, reqs -> | ||
| reqs | ||
| |> Seq.choose (fun r -> r.PutRequest |> Option.ofObj) | ||
| |> Seq.map _.Item | ||
| |> Seq.toArray | ||
| | false, _ -> [||] | ||
| if isNull response.UnprocessedItems then | ||
|
||
| [||] | ||
| else | ||
| match response.UnprocessedItems.TryGetValue tableName with | ||
| | true, reqs -> | ||
| reqs | ||
| |> Seq.choose (fun r -> r.PutRequest |> Option.ofObj) | ||
| |> Seq.map _.Item | ||
| |> Seq.toArray | ||
| | false, _ -> [||] | ||
| maybeReport | ||
| |> Option.iter (fun r -> r BatchWriteItems (Seq.toList response.ConsumedCapacity) (items.Length - unprocessed.Length)) | ||
| |> Option.iter (fun r -> r BatchWriteItems (if isNull response.ConsumedCapacity then [] else Seq.toList response.ConsumedCapacity) (items.Length - unprocessed.Length)) | ||
|
||
| if response.HttpStatusCode <> HttpStatusCode.OK then | ||
| failwithf "BatchWriteItem put request returned error %O" response.HttpStatusCode | ||
|
|
||
|
|
@@ -841,15 +848,18 @@ type TableContext<'TRecord> | |
| let! ct = Async.CancellationToken | ||
| let! response = client.BatchWriteItemAsync(request, ct) |> Async.AwaitTaskCorrect | ||
| let unprocessed = | ||
| match response.UnprocessedItems.TryGetValue tableName with | ||
| | true, reqs -> | ||
| reqs | ||
| |> Seq.choose (fun r -> r.DeleteRequest |> Option.ofObj) | ||
| |> Seq.map _.Key | ||
| |> Seq.toArray | ||
| | false, _ -> [||] | ||
| if isNull response.UnprocessedItems then | ||
| [||] | ||
| else | ||
| match response.UnprocessedItems.TryGetValue tableName with | ||
| | true, reqs -> | ||
| reqs | ||
| |> Seq.choose (fun r -> r.DeleteRequest |> Option.ofObj) | ||
| |> Seq.map _.Key | ||
| |> Seq.toArray | ||
| | false, _ -> [||] | ||
| maybeReport | ||
| |> Option.iter (fun r -> r BatchWriteItems (Seq.toList response.ConsumedCapacity) (keys.Length - unprocessed.Length)) | ||
| |> Option.iter (fun r -> r BatchWriteItems (if isNull response.ConsumedCapacity then [] else Seq.toList response.ConsumedCapacity) (keys.Length - unprocessed.Length)) | ||
| if response.HttpStatusCode <> HttpStatusCode.OK then | ||
| failwithf "BatchWriteItem deletion request returned error %O" response.HttpStatusCode | ||
|
|
||
|
|
@@ -1412,7 +1422,7 @@ and Transaction(client: IAmazonDynamoDB, ?metricsCollector: RequestMetrics -> un | |
| let writer = AttributeWriter() | ||
| req.UpdateExpression <- updater.UpdateOps.Write writer | ||
| precondition |> Option.iter (fun cond -> req.ConditionExpression <- cond.Conditional.Write writer) | ||
|
|
||
| req.ExpressionAttributeNames <- writer.Names | ||
| req.ExpressionAttributeValues <- writer.Values | ||
| transactionItems.Add(TransactWriteItem(Update = req)) | ||
|
|
@@ -1455,7 +1465,10 @@ and Transaction(client: IAmazonDynamoDB, ?metricsCollector: RequestMetrics -> un | |
| let! response = client.TransactWriteItemsAsync(req, ct) |> Async.AwaitTaskCorrect | ||
| maybeReport | ||
| |> Option.iter (fun r -> | ||
| response.ConsumedCapacity | ||
| (if isNull response.ConsumedCapacity then | ||
|
||
| ResizeArray() | ||
| else | ||
| response.ConsumedCapacity) | ||
| |> Seq.groupBy _.TableName | ||
| |> Seq.iter (fun (tableName, consumedCapacity) -> | ||
| r tableName Operation.TransactWriteItems (Seq.toList consumedCapacity) (Seq.length transactionItems))) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know you're doing it for consistency but the guard guarantees it won't be null (in fact the guard will return Null if the Item.Count = 0)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I had to return an option either way so figured why not.
I guess
Some response.Itemmight be marginally more efficient since it is one fewernullcheck?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TL;DR Its more about not putting 'just in case' code that's not justified as that makes the reader have to think more (and/or if you were trying to autogen coverage of all paths, you'd be trying to generate a null with the guard returning true, which would be impossible)
It happens to be more efficient, but its more about keeping the code straightforward and not putting diffs in unless there is an actual correctness improvement - the
IsItemSetabsolutely guarantees a non-null value, and the general pattern should be to think about guarding all inspections of properties and then either do the work conditionally, or substitute an empty if the arrays are being processed in some way.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense; updated