Skip to content

Commit c5d7cfc

Browse files
committed
DeleteItemDocumentOperationRequest implementation
1 parent cb22c1f commit c5d7cfc

File tree

5 files changed

+331
-8
lines changed

5 files changed

+331
-8
lines changed

sdk/src/Services/DynamoDBv2/Custom/DocumentModel/DocumentOperationRequest.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ public class DeleteItemDocumentOperationRequest : DocumentOperationRequest
6060
/// Flag specifying what values should be returned.
6161
/// </summary>
6262
public ReturnValues ReturnValues { get; set; }
63+
64+
/// <summary>
65+
/// Flag specifying what values should be returned.
66+
/// </summary>
67+
public ReturnValuesOnConditionCheckFailure ReturnValuesOnConditionCheckFailure { get; set; }
6368
}
6469

6570
/// <summary>

sdk/src/Services/DynamoDBv2/Custom/DocumentModel/Table.cs

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1718,6 +1718,7 @@ internal Document DeleteHelper(Key key, DeleteItemOperationConfig config)
17181718
return ret;
17191719
}
17201720

1721+
17211722
internal async Task<Document> DeleteHelperAsync(Key key, DeleteItemOperationConfig config, CancellationToken cancellationToken)
17221723
{
17231724
var currentConfig = config ?? new DeleteItemOperationConfig();
@@ -1761,6 +1762,85 @@ internal async Task<Document> DeleteHelperAsync(Key key, DeleteItemOperationConf
17611762
return ret;
17621763
}
17631764

1765+
1766+
internal Document DeleteHelper(DeleteItemDocumentOperationRequest request)
1767+
{
1768+
var req = MapDeleteItemOperationRequestToDeleteItemRequest(request);
1769+
1770+
#if NETSTANDARD
1771+
// Cast the IAmazonDynamoDB to the concrete client instead, so we can access the internal sync-over-async methods
1772+
var client = DDBClient as AmazonDynamoDBClient;
1773+
if (client == null)
1774+
{
1775+
throw new InvalidOperationException(
1776+
"Calling the synchronous DeleteItem from .NET or .NET Core requires initializing the Table " +
1777+
"with an actual AmazonDynamoDBClient. You can use a mocked or substitute IAmazonDynamoDB when calling DeleteItemAsync instead.");
1778+
}
1779+
#else
1780+
var client = DDBClient;
1781+
#endif
1782+
1783+
var attributes = client.DeleteItem(req).Attributes;
1784+
1785+
Document ret = null;
1786+
if (request.ReturnValues == ReturnValues.AllOldAttributes)
1787+
{
1788+
ret = this.FromAttributeMap(attributes);
1789+
}
1790+
1791+
return ret;
1792+
}
1793+
1794+
internal async Task<Document> DeleteHelperAsync(DeleteItemDocumentOperationRequest request, CancellationToken cancellationToken)
1795+
{
1796+
var req = MapDeleteItemOperationRequestToDeleteItemRequest(request);
1797+
1798+
this.UpdateRequestUserAgentDetails(req, isAsync: true);
1799+
1800+
var attributes = (await DDBClient.DeleteItemAsync(req, cancellationToken).ConfigureAwait(false)).Attributes;
1801+
1802+
Document ret = null;
1803+
if (request.ReturnValues == ReturnValues.AllOldAttributes)
1804+
{
1805+
ret = this.FromAttributeMap(attributes);
1806+
}
1807+
return ret;
1808+
}
1809+
1810+
private DeleteItemRequest MapDeleteItemOperationRequestToDeleteItemRequest(DeleteItemDocumentOperationRequest request)
1811+
{
1812+
if (request == null)
1813+
throw new ArgumentNullException(nameof(request));
1814+
1815+
if (request.Key == null || request.Key.Count == 0)
1816+
{
1817+
throw new InvalidOperationException("Key cannot be null or empty");
1818+
}
1819+
1820+
var req = new DeleteItemRequest
1821+
{
1822+
TableName = TableName,
1823+
Key = MakeKey(request.Key)
1824+
};
1825+
1826+
//todo: add integration test for ReturnValuesOnConditionCheckFailure
1827+
if (request.ReturnValues == ReturnValues.AllOldAttributes)
1828+
req.ReturnValues = EnumMapper.Convert(request.ReturnValues);
1829+
1830+
if(request.ReturnValuesOnConditionCheckFailure ==
1831+
ReturnValuesOnConditionCheckFailure.AllOldAttributes)
1832+
{
1833+
req.ReturnValuesOnConditionCheckFailure = EnumMapper.Convert(request.ReturnValuesOnConditionCheckFailure);
1834+
}
1835+
1836+
if(request.ConditionalExpression is { IsSet: true })
1837+
{
1838+
request.ConditionalExpression.ApplyExpression(req, this);
1839+
}
1840+
1841+
return req;
1842+
}
1843+
17641844
#endregion
17651845

17661846

@@ -1924,6 +2004,6 @@ public IDocumentTransactWrite CreateTransactWrite()
19242004
return new DocumentTransactWrite(this);
19252005
}
19262006
#endregion
1927-
2007+
19282008
}
19292009
}

sdk/src/Services/DynamoDBv2/Custom/DocumentModel/_async/Table.Async.cs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,14 @@ public partial interface ITable
274274
/// <returns>A Task that can be used to poll or wait for results, or both.</returns>
275275
Task<Document> DeleteItemAsync(IDictionary<string, DynamoDBEntry> key, DeleteItemOperationConfig config, CancellationToken cancellationToken = default(CancellationToken));
276276

277+
/// <summary>
278+
/// Initiates the asynchronous execution of the DeleteItem operation.
279+
/// </summary>
280+
/// <param name="request">The DeleteItemDocumentOperationRequest object containing all parameters for the delete operation.</param>
281+
/// <param name="cancellationToken">Token which can be used to cancel the task.</param>
282+
/// <returns>A Task that can be used to poll or wait for results, or both.</returns>
283+
Task<Document> DeleteItemAsync(DeleteItemDocumentOperationRequest request, CancellationToken cancellationToken = default(CancellationToken));
284+
277285
#endregion
278286
}
279287

@@ -536,15 +544,24 @@ public partial class Table : ITable
536544
}
537545
}
538546

539-
/// <inheritdoc/>
540-
public async Task<Document> DeleteItemAsync(IDictionary<string, DynamoDBEntry> key, DeleteItemOperationConfig config, CancellationToken cancellationToken = default(CancellationToken))
541-
{
542-
var operationName = DynamoDBTelemetry.ExtractOperationName(nameof(Table), nameof(DeleteItemAsync));
543-
using (DynamoDBTelemetry.CreateSpan(TracerProvider, operationName, spanKind: SpanKind.CLIENT))
547+
/// <inheritdoc/>
548+
public async Task<Document> DeleteItemAsync(IDictionary<string, DynamoDBEntry> key, DeleteItemOperationConfig config, CancellationToken cancellationToken = default(CancellationToken))
544549
{
545-
return await DeleteHelperAsync(MakeKey(key), config, cancellationToken).ConfigureAwait(false);
550+
var operationName = DynamoDBTelemetry.ExtractOperationName(nameof(Table), nameof(DeleteItemAsync));
551+
using (DynamoDBTelemetry.CreateSpan(TracerProvider, operationName, spanKind: SpanKind.CLIENT))
552+
{
553+
return await DeleteHelperAsync(MakeKey(key), config, cancellationToken).ConfigureAwait(false);
554+
}
555+
}
556+
/// <inheritdoc/>
557+
public async Task<Document> DeleteItemAsync(DeleteItemDocumentOperationRequest request, CancellationToken cancellationToken = default(CancellationToken))
558+
{
559+
var operationName = DynamoDBTelemetry.ExtractOperationName(nameof(Table), nameof(DeleteItemAsync));
560+
using (DynamoDBTelemetry.CreateSpan(TracerProvider, operationName, spanKind: SpanKind.CLIENT))
561+
{
562+
return await DeleteHelperAsync(request, cancellationToken).ConfigureAwait(false);
563+
}
546564
}
547-
}
548565

549566
#endregion
550567

sdk/src/Services/DynamoDBv2/Custom/DocumentModel/_bcl/Table.Sync.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,21 @@ public partial interface ITable
245245
/// <returns>True if deleted or false if the condition in the config was not met.</returns>
246246
bool TryDeleteItem(IDictionary<string, DynamoDBEntry> key, DeleteItemOperationConfig config = null);
247247

248+
/// <summary>
249+
/// Delete a document in DynamoDB, identified by a key, using specified configs.
250+
/// </summary>
251+
/// <exception cref="T:Amazon.DynamoDBv2.Model.ConditionalCheckFailedException">If the condition set on the config fails.</exception>
252+
/// <param name="request">The DeleteItemDocumentOperationRequest object containing all parameters for the delete operation.</param>
253+
/// <returns>Null or old attributes, depending on config.</returns>
254+
Document DeleteItem(DeleteItemDocumentOperationRequest request);
255+
256+
/// <summary>
257+
/// Delete a document in DynamoDB, identified by a key, using specified configs.
258+
/// </summary>
259+
/// <param name="request">The DeleteItemDocumentOperationRequest object containing all parameters for the delete operation.</param>
260+
/// <returns>True if deleted or false if the condition in the config was not met.</returns>
261+
bool TryDeleteItem(DeleteItemDocumentOperationRequest request);
262+
248263
#endregion
249264
}
250265

@@ -560,6 +575,34 @@ public bool TryDeleteItem(IDictionary<string, DynamoDBEntry> key, DeleteItemOper
560575
}
561576
}
562577

578+
/// <inheritdoc/>
579+
public Document DeleteItem(DeleteItemDocumentOperationRequest request)
580+
{
581+
var operationName = DynamoDBTelemetry.ExtractOperationName(nameof(Table), nameof(DeleteItem));
582+
using (DynamoDBTelemetry.CreateSpan(TracerProvider, operationName, spanKind: SpanKind.CLIENT))
583+
{
584+
return DeleteHelper(request);
585+
}
586+
}
587+
588+
/// <inheritdoc/>
589+
public bool TryDeleteItem(DeleteItemDocumentOperationRequest request)
590+
{
591+
592+
var operationName = DynamoDBTelemetry.ExtractOperationName(nameof(Table), nameof(TryDeleteItem));
593+
using (DynamoDBTelemetry.CreateSpan(TracerProvider, operationName, spanKind: SpanKind.CLIENT))
594+
{
595+
try
596+
{
597+
DeleteHelper(request);
598+
return true;
599+
}
600+
catch (ConditionalCheckFailedException)
601+
{
602+
return false;
603+
}
604+
}
605+
}
563606
#endregion
564607

565608
}

0 commit comments

Comments
 (0)