@@ -517,31 +517,6 @@ def encode_dict(values_dict):
517517 }
518518
519519
520- def extract_field_paths (document_data ):
521- """Extract field paths from document data
522-
523- Args:
524- document_data (dict): The dictionary of the actual set data.
525-
526- Returns:
527- List[~.firestore_v1beta1._helpers.FieldPath]:
528- A list of `FieldPath` instances from the actual data.
529- """
530- field_paths = []
531- for field_name , value in six .iteritems (document_data ):
532- if isinstance (value , dict ):
533- sub_field_paths = extract_field_paths (value )
534- for sub_path in sub_field_paths :
535- paths = [field_name ]
536- paths .extend (sub_path .parts )
537- field_path = FieldPath (* paths )
538- field_paths .append (field_path )
539- else :
540- path = FieldPath (field_name )
541- field_paths .append (path )
542- return field_paths
543-
544-
545520def reference_value_to_document (reference_value , client ):
546521 """Convert a reference value string to a document.
547522
@@ -844,28 +819,36 @@ def process_server_timestamp(document_data, split_on_dots=True):
844819 * The remaining keys in ``document_data`` after removing the
845820 server timestamp sentinels
846821 """
822+ field_paths = []
847823 transform_paths = []
848824 actual_data = {}
849825 for field_name , value in six .iteritems (document_data ):
850826 if isinstance (value , dict ):
851- sub_field_paths , sub_data = process_server_timestamp (value , False )
852- for sub_path in sub_field_paths :
827+ sub_transform_paths , sub_data , sub_field_paths = (
828+ process_server_timestamp (value , False ))
829+ for sub_path in sub_transform_paths :
853830 field_path = FieldPath .from_string (field_name )
854831 field_path .parts = field_path .parts + sub_path .parts
855832 transform_paths .extend ([field_path ])
856833 if sub_data :
857834 # Only add a key to ``actual_data`` if there is data.
835+
858836 actual_data [field_name ] = sub_data
837+ for sub_field_path in sub_field_paths :
838+ field_path = FieldPath .from_string (field_name )
839+ field_path .parts = field_path .parts + sub_field_path .parts
840+ field_paths .append (field_path )
859841 elif value is constants .SERVER_TIMESTAMP :
860842 if split_on_dots :
861843 transform_paths .append (FieldPath (* field_name .split ("." )))
862844 else :
863845 transform_paths .append (FieldPath .from_string (field_name ))
864846 else :
865847 actual_data [field_name ] = value
848+ field_paths .append (FieldPath (field_name ))
866849 if not transform_paths :
867850 actual_data = document_data
868- return transform_paths , actual_data
851+ return transform_paths , actual_data , field_paths
869852
870853
871854def get_transform_pb (document_path , transform_paths ):
@@ -912,7 +895,7 @@ def pbs_for_set(document_path, document_data, merge=False, exists=None):
912895 List[google.cloud.firestore_v1beta1.types.Write]: One
913896 or two ``Write`` protobuf instances for ``set()``.
914897 """
915- transform_paths , actual_data = process_server_timestamp (
898+ transform_paths , actual_data , field_paths = process_server_timestamp (
916899 document_data , False )
917900 update_pb = write_pb2 .Write (
918901 update = document_pb2 .Document (
@@ -925,7 +908,6 @@ def pbs_for_set(document_path, document_data, merge=False, exists=None):
925908 common_pb2 .Precondition (exists = exists ))
926909
927910 if merge :
928- field_paths = extract_field_paths (document_data )
929911 field_paths = canonicalize_field_paths (field_paths )
930912 mask = common_pb2 .DocumentMask (field_paths = sorted (field_paths ))
931913 update_pb .update_mask .CopyFrom (mask )
@@ -984,7 +966,8 @@ def pbs_for_update(client, document_path, field_updates, option):
984966 # Default uses ``exists=True``.
985967 option = client .write_option (exists = True )
986968
987- transform_paths , actual_updates = process_server_timestamp (field_updates )
969+ transform_paths , actual_updates , field_paths = (
970+ process_server_timestamp (field_updates ))
988971 if not (transform_paths or actual_updates ):
989972 raise ValueError ('There are only ServerTimeStamp objects or is empty.' )
990973 update_values , field_paths = FieldPathHelper .to_field_paths (actual_updates )
0 commit comments