Skip to content

Commit 92639df

Browse files
Makes data argument optional on transactional delete
Signed-off-by: Elena Kolevska <[email protected]>
1 parent 6e90e84 commit 92639df

File tree

5 files changed

+31
-6
lines changed

5 files changed

+31
-6
lines changed

dapr/clients/grpc/_request.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ class TransactionalStateOperation:
281281
def __init__(
282282
self,
283283
key: str,
284-
data: Union[bytes, str],
284+
data: Optional[Union[bytes, str]] = None,
285285
etag: Optional[str] = None,
286286
operation_type: TransactionOperationType = TransactionOperationType.upsert,
287287
):
@@ -297,7 +297,7 @@ def __init__(
297297
Raises:
298298
ValueError: data is not bytes or str.
299299
"""
300-
if not isinstance(data, (bytes, str)):
300+
if operation_type != TransactionOperationType.delete and not isinstance(data, (bytes, str)):
301301
raise ValueError(f'invalid type for data {type(data)}')
302302

303303
self._key = key

dapr/clients/grpc/client.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -884,6 +884,7 @@ def execute_state_transaction(
884884
transactional_metadata: Optional[Dict[str, str]] = dict(),
885885
metadata: Optional[MetadataTuple] = None,
886886
) -> DaprResponse:
887+
887888
"""Saves or deletes key-value pairs to a statestore as a transaction
888889
889890
This saves or deletes key-values to the statestore as part of a single transaction,
@@ -929,7 +930,7 @@ def execute_state_transaction(
929930
operationType=o.operation_type.value,
930931
request=common_v1.StateItem(
931932
key=o.key,
932-
value=to_bytes(o.data),
933+
value=to_bytes(o.data) if o.data is not None else None,
933934
etag=common_v1.Etag(value=o.etag) if o.etag is not None else None,
934935
),
935936
)

examples/state_store/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ expected_stdout_lines:
4141
- "== APP == Cannot save bulk due to bad etags. ErrorCode=StatusCode.ABORTED"
4242
- "== APP == Got value=b'value_1' eTag=1"
4343
- "== APP == Got items with etags: [(b'value_1_updated', '2'), (b'value_2', '2')]"
44+
- "== APP == Got values after transaction delete: [b'', b'']"
4445
- "== APP == Got value after delete: b''"
4546
timeout_seconds: 5
4647
-->
@@ -67,6 +68,8 @@ The output should be as follows:
6768
6869
== APP == Got items with etags: [(b'value_1_updated', '2'), (b'value_2', '2')]
6970
71+
== APP == Got values after transaction delete: [b'', b'']
72+
7073
== APP == Got value after delete: b''
7174
```
7275

examples/state_store/state_store.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
etag=state.etag,
7979
),
8080
TransactionalStateOperation(key=another_key, data=another_value),
81+
TransactionalStateOperation(key=yet_another_key, data=yet_another_value),
8182
],
8283
)
8384

@@ -87,7 +88,17 @@
8788
).items
8889
print(f'Got items with etags: {[(i.data, i.etag) for i in items]}')
8990

91+
# Transaction delete
92+
d.execute_state_transaction(store_name=storeName, operations=[
93+
TransactionalStateOperation(operation_type=TransactionOperationType.delete, key=key),
94+
TransactionalStateOperation(operation_type=TransactionOperationType.delete, key=another_key),])
95+
96+
# Batch get
97+
items = d.get_bulk_state(store_name=storeName, keys=[key, another_key],
98+
states_metadata={'metakey': 'metavalue'}).items
99+
print(f'Got values after transaction delete: {[data.data for data in items]}')
100+
90101
# Delete one state by key.
91-
d.delete_state(store_name=storeName, key=key, state_metadata={'metakey': 'metavalue'})
92-
data = d.get_state(store_name=storeName, key=key).data
102+
d.delete_state(store_name=storeName, key=yet_another_key, state_metadata={'metakey': 'metavalue'})
103+
data = d.get_state(store_name=storeName, key=yet_another_key).data
93104
print(f'Got value after delete: {data}')

tests/clients/test_dapr_grpc_client.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
from .fake_dapr_server import FakeDaprSidecar
3434
from dapr.conf import settings
3535
from dapr.clients.grpc._helpers import to_bytes
36-
from dapr.clients.grpc._request import TransactionalStateOperation
36+
from dapr.clients.grpc._request import TransactionalStateOperation, TransactionOperationType
3737
from dapr.clients.grpc._state import StateOptions, Consistency, Concurrency, StateItem
3838
from dapr.clients.grpc._crypto import EncryptOptions, DecryptOptions
3939
from dapr.clients.grpc._response import (
@@ -508,6 +508,16 @@ def test_transaction_then_get_states(self):
508508
self.assertEqual(resp.items[1].key, another_key)
509509
self.assertEqual(resp.items[1].data, to_bytes(another_value.upper()))
510510

511+
dapr.execute_state_transaction(store_name='statestore',
512+
operations=[TransactionalStateOperation(key=key, operation_type=TransactionOperationType.delete),
513+
TransactionalStateOperation(key=another_key, operation_type=TransactionOperationType.delete), ],
514+
)
515+
resp = dapr.get_state(store_name='statestore', key=key)
516+
self.assertEqual(resp.data, b'')
517+
518+
resp = dapr.get_state(store_name='statestore', key=another_key)
519+
self.assertEqual(resp.data, b'')
520+
511521
self._fake_dapr_server.raise_exception_on_next_call(
512522
status_pb2.Status(code=code_pb2.INVALID_ARGUMENT, message='my invalid argument message')
513523
)

0 commit comments

Comments
 (0)