forked from googleapis/google-cloud-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Addresses seventh part of googleapis#451. Also requires removing the dataset_id from generated protobufs due to googleapis/google-cloud-datastore#59. This occurs also in __key__ filters and ancestor queries.
- Loading branch information
Showing
14 changed files
with
251 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
from itertools import izip | ||
import six | ||
|
||
from gcloud.datastore import _implicit_environ | ||
from gcloud.datastore import datastore_v1_pb2 as datastore_pb | ||
|
||
|
||
|
@@ -56,24 +57,30 @@ def __init__(self, *path_args, **kwargs): | |
passed as a keyword argument. | ||
:type dataset_id: string | ||
:param dataset_id: The dataset ID associated with the key. Can only be | ||
passed as a keyword argument. | ||
# This note will be obsolete by the end of #451. | ||
.. note:: | ||
The key's ``_dataset_id`` field must be None for keys created | ||
by application code. The | ||
:func:`gcloud.datastore.helpers.key_from_protobuf` factory | ||
will be set the field to an appropriate value for keys | ||
returned from the datastore backend. The application | ||
**must** treat any value set by the back-end as opaque. | ||
:param dataset_id: The dataset ID associated with the key. This is | ||
required. Can only be passed as a keyword argument. | ||
This comment has been minimized.
Sorry, something went wrong.
tseaver
|
||
""" | ||
self._path = self._parse_path(path_args) | ||
self._flat_path = path_args | ||
self._parent = None | ||
self._namespace = kwargs.get('namespace') | ||
self._dataset_id = kwargs.get('dataset_id') | ||
self._validate_dataset_id() | ||
|
||
def _validate_dataset_id(self): | ||
"""Ensures the dataset ID is set. | ||
If unset, attempts to imply the ID from the environment. | ||
:raises: `ValueError` if there is no `dataset_id` and none | ||
can be implied. | ||
""" | ||
if self._dataset_id is None: | ||
if _implicit_environ.DATASET is not None: | ||
# This assumes DATASET.id() is not None. | ||
self._dataset_id = _implicit_environ.DATASET.id() | ||
else: | ||
raise ValueError('A Key must have a dataset ID set.') | ||
|
||
@staticmethod | ||
def _parse_path(path_args): | ||
|
@@ -185,10 +192,6 @@ def compare_to_proto(self, protobuf): | |
If the current key is partial, returns a new key that has been | ||
completed otherwise returns the current key. | ||
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, we leave it as implicit. | ||
:type protobuf: :class:`gcloud.datastore.datastore_v1_pb2.Key` | ||
:param protobuf: A protobuf representation of the key. Expected to be | ||
returned after a datastore operation. | ||
|
@@ -206,9 +209,8 @@ def compare_to_proto(self, protobuf): | |
raise ValueError('Namespace on protobuf does not match.', | ||
protobuf.partition_id.namespace, self.namespace) | ||
|
||
# Check that dataset IDs match if not implicit. | ||
if self.dataset_id is not None: | ||
self._validate_protobuf_dataset_id(protobuf) | ||
# Check that dataset IDs match. | ||
self._validate_protobuf_dataset_id(protobuf) | ||
|
||
path = [] | ||
for element in protobuf.path_element: | ||
|
@@ -245,9 +247,7 @@ def to_protobuf(self): | |
:returns: The Protobuf representing the key. | ||
""" | ||
key = datastore_pb.Key() | ||
|
||
if self.dataset_id is not None: | ||
key.partition_id.dataset_id = self.dataset_id | ||
key.partition_id.dataset_id = self.dataset_id | ||
|
||
if self.namespace: | ||
key.partition_id.namespace = self.namespace | ||
|
@@ -382,4 +382,4 @@ def parent(self): | |
return self._parent | ||
|
||
def __repr__(self): | ||
return '<Key%s>' % self.path | ||
return '<Key%s, dataset=%s>' % (self.path, self.dataset_id) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Why not just mutate the key_pb (and drop the return value)? Key protobufs are always being created on-the-fly anyway, right?