3737from google .cloud .firestore_v1beta1 .document import DocumentSnapshot
3838from google .cloud .firestore_v1beta1 .gapic import firestore_client
3939from google .cloud .firestore_v1beta1 .transaction import Transaction
40+ from google .cloud .firestore_v1beta1 .proto import common_pb2
41+ from google .cloud .firestore_v1beta1 .proto import document_pb2
42+ from google .cloud .firestore_v1beta1 .proto import write_pb2
4043
4144
4245DEFAULT_DATABASE = '(default)'
@@ -251,15 +254,18 @@ def write_option(**kwargs):
251254 :meth:`~.DocumentReference.update` and
252255 :meth:`~.DocumentReference.delete`.
253256
254- One of the following two keyword arguments must be provided:
257+ One of the following keyword arguments must be provided:
255258
256259 * ``last_update_time`` (:class:`google.protobuf.timestamp_pb2.\
257- Timestamp`): A timestamp. When set, the target document must exist
258- and have been last updated at that time. Protobuf ``update_time``
259- timestamps are typically returned from methods that perform write
260- operations as part of a "write result" protobuf or directly.
260+ Timestamp`): A timestamp. When set, the target document must exist
261+ and have been last updated at that time. Protobuf ``update_time``
262+ timestamps are typically returned from methods that perform write
263+ operations as part of a "write result" protobuf or directly.
261264 * ``exists`` (:class:`bool`): Indicates if the document being modified
262- should already exist.
265+ should already exist.
266+ * ``merge`` (Any):
267+ Indicates if the document should be merged.
268+ **Note**: argument is ignored
263269
264270 Providing no argument would make the option have no effect (so
265271 it is not allowed). Providing multiple would be an apparent
@@ -283,6 +289,8 @@ def write_option(**kwargs):
283289 return LastUpdateOption (value )
284290 elif name == 'exists' :
285291 return ExistsOption (value )
292+ elif name == 'merge' :
293+ return MergeOption ()
286294 else :
287295 extra = '{!r} was provided' .format (name )
288296 raise TypeError (_BAD_OPTION_ERR , extra )
@@ -418,6 +426,39 @@ def modify_write(self, write_pb, **unused_kwargs):
418426 write_pb .current_document .CopyFrom (current_doc )
419427
420428
429+ class MergeOption (WriteOption ):
430+ """Option used to merge on a write operation.
431+
432+ This will typically be created by
433+ :meth:`~.firestore_v1beta1.client.Client.write_option`.
434+ """
435+ def modify_write (self , write_pb , actual_data = None , path = None , ** unused_kwargs ):
436+ """Modify a ``Write`` protobuf based on the state of this write option.
437+
438+ Args:
439+ write_pb (google.cloud.firestore_v1beta1.types.Write): A
440+ ``Write`` protobuf instance to be modified with a precondition
441+ determined by the state of this option.
442+ actual_data (dict):
443+ The actual field names and values to use for replacing a
444+ document.
445+ path (str): A fully-qualified document_path
446+ unused_kwargs (Dict[str, Any]): Keyword arguments accepted by
447+ other subclasses that are unused here.
448+ """
449+ actual_data , field_paths = _helpers .FieldPathHelper .to_field_paths (actual_data )
450+ doc = document_pb2 .Document (
451+ name = path ,
452+ fields = _helpers .encode_dict (actual_data )
453+ )
454+ write = write_pb2 .Write (
455+ update = doc ,
456+ )
457+ write_pb .CopyFrom (write )
458+ mask = common_pb2 .DocumentMask (field_paths = sorted (field_paths ))
459+ write_pb .update_mask .CopyFrom (mask )
460+
461+
421462class ExistsOption (WriteOption ):
422463 """Option used to assert existence on a write operation.
423464
0 commit comments