From 12695c2aa79ecf44a6c85ed12ed41cb618884a0c Mon Sep 17 00:00:00 2001 From: Danny Hermes Date: Thu, 23 Oct 2014 16:42:21 -0700 Subject: [PATCH] Only using explicit namespace when deserializing key pb. This issue surfaced during review for #282. --- gcloud/datastore/entity.py | 7 ++++++- gcloud/datastore/helpers.py | 8 ++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/gcloud/datastore/entity.py b/gcloud/datastore/entity.py index 5c939493c9d6..0d75a5c9b413 100644 --- a/gcloud/datastore/entity.py +++ b/gcloud/datastore/entity.py @@ -222,13 +222,18 @@ def save(self): transaction.add_auto_id_entity(self) if isinstance(key_pb, datastore_pb.Key): + # Update the path (which may have been altered). + # NOTE: The underlying namespace can't have changed in a save(). + # The value of the dataset ID may have changed from implicit + # (i.e. None, with the ID implied from the dataset.Dataset + # object associated with the Entity/Key), but if it was + # implicit before the save() we leave it as implicit. path = [] for element in key_pb.path_element: key_part = {} for descriptor, value in element._fields.items(): key_part[descriptor.name] = value path.append(key_part) - # Update the path (which may have been altered). self._key = key.path(path) return self diff --git a/gcloud/datastore/helpers.py b/gcloud/datastore/helpers.py index c8927bf2beea..c5673a8d3c04 100644 --- a/gcloud/datastore/helpers.py +++ b/gcloud/datastore/helpers.py @@ -64,8 +64,12 @@ def key_from_protobuf(pb): path.append(element_dict) - dataset_id = pb.partition_id.dataset_id or None - namespace = pb.partition_id.namespace + dataset_id = None + if pb.partition_id.HasField('dataset_id'): + dataset_id = pb.partition_id.dataset_id + namespace = None + if pb.partition_id.HasField('namespace'): + namespace = pb.partition_id.namespace return Key(path, namespace, dataset_id)