@@ -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,35 @@ 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 = process_server_timestamp (value , False )
828+ for sub_path in sub_transform_paths :
853829 field_path = FieldPath .from_string (field_name )
854830 field_path .parts = field_path .parts + sub_path .parts
855831 transform_paths .extend ([field_path ])
856832 if sub_data :
857833 # Only add a key to ``actual_data`` if there is data.
834+
858835 actual_data [field_name ] = sub_data
836+ for sub_field_path in sub_field_paths :
837+ field_path = FieldPath .from_string (field_name )
838+ field_path .parts = field_path .parts + sub_field_path .parts
839+ field_paths .append (field_path )
859840 elif value is constants .SERVER_TIMESTAMP :
860841 if split_on_dots :
861842 transform_paths .append (FieldPath (* field_name .split ("." )))
862843 else :
863844 transform_paths .append (FieldPath .from_string (field_name ))
864845 else :
865846 actual_data [field_name ] = value
847+ field_paths .append (FieldPath (field_name ))
866848 if not transform_paths :
867849 actual_data = document_data
868- return transform_paths , actual_data
850+ return transform_paths , actual_data , field_paths
869851
870852
871853def get_transform_pb (document_path , transform_paths ):
@@ -912,7 +894,7 @@ def pbs_for_set(document_path, document_data, merge=False, exists=None):
912894 List[google.cloud.firestore_v1beta1.types.Write]: One
913895 or two ``Write`` protobuf instances for ``set()``.
914896 """
915- transform_paths , actual_data = process_server_timestamp (
897+ transform_paths , actual_data , field_paths = process_server_timestamp (
916898 document_data , False )
917899 update_pb = write_pb2 .Write (
918900 update = document_pb2 .Document (
@@ -925,7 +907,6 @@ def pbs_for_set(document_path, document_data, merge=False, exists=None):
925907 common_pb2 .Precondition (exists = exists ))
926908
927909 if merge :
928- field_paths = extract_field_paths (document_data )
929910 field_paths = canonicalize_field_paths (field_paths )
930911 mask = common_pb2 .DocumentMask (field_paths = sorted (field_paths ))
931912 update_pb .update_mask .CopyFrom (mask )
@@ -984,7 +965,7 @@ def pbs_for_update(client, document_path, field_updates, option):
984965 # Default uses ``exists=True``.
985966 option = client .write_option (exists = True )
986967
987- transform_paths , actual_updates = process_server_timestamp (field_updates )
968+ transform_paths , actual_updates , field_paths = process_server_timestamp (field_updates )
988969 if not (transform_paths or actual_updates ):
989970 raise ValueError ('There are only ServerTimeStamp objects or is empty.' )
990971 update_values , field_paths = FieldPathHelper .to_field_paths (actual_updates )
0 commit comments