From 3a74badf5d7b9c28d4852d0ec35b902f6be27c32 Mon Sep 17 00:00:00 2001 From: Danny Hermes Date: Fri, 28 Oct 2016 14:57:16 -0700 Subject: [PATCH] Make type_url optional when registering types. Also renaming register_type_url to register_type. --- bigtable/google/cloud/bigtable/cluster.py | 8 +--- bigtable/google/cloud/bigtable/instance.py | 11 ++--- core/google/cloud/operation.py | 13 +++--- core/unit_tests/test_operation.py | 47 ++++++++++++++-------- 4 files changed, 44 insertions(+), 35 deletions(-) diff --git a/bigtable/google/cloud/bigtable/cluster.py b/bigtable/google/cloud/bigtable/cluster.py index 48b335c5196e0..c2418576dde95 100644 --- a/bigtable/google/cloud/bigtable/cluster.py +++ b/bigtable/google/cloud/bigtable/cluster.py @@ -22,8 +22,7 @@ from google.cloud.bigtable._generated import ( bigtable_instance_admin_pb2 as messages_v2_pb2) from google.cloud.operation import Operation -from google.cloud.operation import _compute_type_url -from google.cloud.operation import register_type_url +from google.cloud.operation import register_type _CLUSTER_NAME_RE = re.compile(r'^projects/(?P[^/]+)/' @@ -34,10 +33,7 @@ """Default number of nodes to use when creating a cluster.""" -_UPDATE_CLUSTER_METADATA_URL = _compute_type_url( - messages_v2_pb2.UpdateClusterMetadata) -register_type_url( - _UPDATE_CLUSTER_METADATA_URL, messages_v2_pb2.UpdateClusterMetadata) +register_type(messages_v2_pb2.UpdateClusterMetadata) def _prepare_create_request(cluster): diff --git a/bigtable/google/cloud/bigtable/instance.py b/bigtable/google/cloud/bigtable/instance.py index afa4066a75b0a..41dda563c8437 100644 --- a/bigtable/google/cloud/bigtable/instance.py +++ b/bigtable/google/cloud/bigtable/instance.py @@ -27,8 +27,7 @@ from google.cloud.bigtable.cluster import DEFAULT_SERVE_NODES from google.cloud.bigtable.table import Table from google.cloud.operation import Operation -from google.cloud.operation import _compute_type_url -from google.cloud.operation import register_type_url +from google.cloud.operation import register_type _EXISTING_INSTANCE_LOCATION_ID = 'see-existing-cluster' @@ -36,12 +35,8 @@ r'instances/(?P[a-z][-a-z0-9]*)$') -_CREATE_INSTANCE_METADATA_URL = _compute_type_url( - messages_v2_pb2.CreateInstanceMetadata) -register_type_url( - _CREATE_INSTANCE_METADATA_URL, messages_v2_pb2.CreateInstanceMetadata) -_INSTANCE_METADATA_URL = _compute_type_url(data_v2_pb2.Instance) -register_type_url(_INSTANCE_METADATA_URL, data_v2_pb2.Instance) +register_type(messages_v2_pb2.CreateInstanceMetadata) +register_type(data_v2_pb2.Instance) def _prepare_create_request(instance): diff --git a/core/google/cloud/operation.py b/core/google/cloud/operation.py index 1be1e434d367a..018eacaad68e1 100644 --- a/core/google/cloud/operation.py +++ b/core/google/cloud/operation.py @@ -39,17 +39,20 @@ def _compute_type_url(klass, prefix=_GOOGLE_APIS_PREFIX): return '%s/%s' % (prefix, name) -def register_type_url(type_url, klass): +def register_type(klass, type_url=None): """Register a klass as the factory for a given type URL. - :type type_url: str - :param type_url: URL naming the type - :type klass: type :param klass: class to be used as a factory for the given type + :type type_url: str + :param type_url: (Optional) URL naming the type. If not provided, + infers the URL from the type descriptor. + :raises: ValueError if a registration already exists for the URL. """ + if type_url is None: + type_url = _compute_type_url(klass) if type_url in _TYPE_URL_MAP: if _TYPE_URL_MAP[type_url] is not klass: raise ValueError("Conflict: %s" % (_TYPE_URL_MAP[type_url],)) @@ -123,7 +126,7 @@ class Operation(object): """Metadata about the current operation (as a protobuf). Code that uses operations must register the metadata types (via - :func:`register_type_url`) to ensure that the metadata fields can be + :func:`register_type`) to ensure that the metadata fields can be converted into the correct types. """ diff --git a/core/unit_tests/test_operation.py b/core/unit_tests/test_operation.py index e67bf21f6f23b..a30ec97f19aec 100644 --- a/core/unit_tests/test_operation.py +++ b/core/unit_tests/test_operation.py @@ -44,48 +44,63 @@ def test_w_prefix(self): '%s/%s' % (PREFIX, Struct.DESCRIPTOR.full_name)) -class Test_register_type_url(unittest.TestCase): +class Test_register_type(unittest.TestCase): - def _callFUT(self, type_url, klass): - from google.cloud.operation import register_type_url - register_type_url(type_url, klass) + def _callFUT(self, klass, type_url=None): + from google.cloud.operation import register_type + register_type(klass, type_url=type_url) - def test_simple(self): + def test_explicit(self): from google.cloud import operation as MUT from google.cloud._testing import _Monkey - TYPE_URI = 'testing.google-cloud-python.com/testing' + + type_url = 'testing.google-cloud-python.com/testing' klass = object() type_url_map = {} with _Monkey(MUT, _TYPE_URL_MAP=type_url_map): - self._callFUT(TYPE_URI, klass) + self._callFUT(klass, type_url) + + self.assertEqual(type_url_map, {type_url: klass}) - self.assertEqual(type_url_map, {TYPE_URI: klass}) + def test_default(self): + from google.protobuf.struct_pb2 import Struct + from google.cloud._testing import _Monkey + from google.cloud import operation as MUT + + type_url_map = {} + with _Monkey(MUT, _TYPE_URL_MAP=type_url_map): + self._callFUT(Struct) + + type_url = MUT._compute_type_url(Struct) + self.assertEqual(type_url_map, {type_url: Struct}) def test_w_same_class(self): from google.cloud import operation as MUT from google.cloud._testing import _Monkey - TYPE_URI = 'testing.google-cloud-python.com/testing' + + type_url = 'testing.google-cloud-python.com/testing' klass = object() - type_url_map = {TYPE_URI: klass} + type_url_map = {type_url: klass} with _Monkey(MUT, _TYPE_URL_MAP=type_url_map): - self._callFUT(TYPE_URI, klass) + self._callFUT(klass, type_url) - self.assertEqual(type_url_map, {TYPE_URI: klass}) + self.assertEqual(type_url_map, {type_url: klass}) def test_w_conflict(self): from google.cloud import operation as MUT from google.cloud._testing import _Monkey - TYPE_URI = 'testing.google-cloud-python.com/testing' + + type_url = 'testing.google-cloud-python.com/testing' klass, other = object(), object() - type_url_map = {TYPE_URI: other} + type_url_map = {type_url: other} with _Monkey(MUT, _TYPE_URL_MAP=type_url_map): with self.assertRaises(ValueError): - self._callFUT(TYPE_URI, klass) + self._callFUT(klass, type_url) - self.assertEqual(type_url_map, {TYPE_URI: other}) + self.assertEqual(type_url_map, {type_url: other}) class OperationTests(unittest.TestCase):