|
23 | 23 | import six |
24 | 24 |
|
25 | 25 | from gcloud._helpers import _datetime_from_microseconds |
| 26 | +from gcloud._helpers import _has_field |
26 | 27 | from gcloud._helpers import _microseconds_from_datetime |
27 | 28 | from gcloud.datastore._generated import entity_pb2 as _entity_pb2 |
28 | 29 | from gcloud.datastore.entity import Entity |
@@ -109,7 +110,7 @@ def _get_meaning(value_pb, is_list=False): |
109 | 110 | if all_meanings: |
110 | 111 | raise ValueError('Different meanings set on values ' |
111 | 112 | 'within a list_value') |
112 | | - elif value_pb.HasField('meaning'): |
| 113 | + elif _has_field(value_pb, 'meaning'): |
113 | 114 | meaning = value_pb.meaning |
114 | 115 |
|
115 | 116 | return meaning |
@@ -159,7 +160,7 @@ def entity_from_protobuf(pb): |
159 | 160 | :returns: The entity derived from the protobuf. |
160 | 161 | """ |
161 | 162 | key = None |
162 | | - if pb.HasField('key'): |
| 163 | + if _has_field(pb, 'key'): |
163 | 164 | key = key_from_protobuf(pb.key) |
164 | 165 |
|
165 | 166 | entity_props = {} |
@@ -259,18 +260,18 @@ def key_from_protobuf(pb): |
259 | 260 | path_args = [] |
260 | 261 | for element in pb.path_element: |
261 | 262 | path_args.append(element.kind) |
262 | | - if element.HasField('id'): |
| 263 | + if _has_field(element, 'id'): |
263 | 264 | path_args.append(element.id) |
264 | 265 | # This is safe: we expect proto objects returned will only have |
265 | 266 | # one of `name` or `id` set. |
266 | | - if element.HasField('name'): |
| 267 | + if _has_field(element, 'name'): |
267 | 268 | path_args.append(element.name) |
268 | 269 |
|
269 | 270 | project = None |
270 | | - if pb.partition_id.HasField('dataset_id'): |
| 271 | + if _has_field(pb.partition_id, 'dataset_id'): |
271 | 272 | project = pb.partition_id.dataset_id |
272 | 273 | namespace = None |
273 | | - if pb.partition_id.HasField('namespace'): |
| 274 | + if _has_field(pb.partition_id, 'namespace'): |
274 | 275 | namespace = pb.partition_id.namespace |
275 | 276 |
|
276 | 277 | return Key(*path_args, namespace=namespace, project=project) |
@@ -350,29 +351,29 @@ def _get_value_from_value_pb(value_pb): |
350 | 351 | :returns: The value provided by the Protobuf. |
351 | 352 | """ |
352 | 353 | result = None |
353 | | - if value_pb.HasField('timestamp_microseconds_value'): |
| 354 | + if _has_field(value_pb, 'timestamp_microseconds_value'): |
354 | 355 | microseconds = value_pb.timestamp_microseconds_value |
355 | 356 | result = _datetime_from_microseconds(microseconds) |
356 | 357 |
|
357 | | - elif value_pb.HasField('key_value'): |
| 358 | + elif _has_field(value_pb, 'key_value'): |
358 | 359 | result = key_from_protobuf(value_pb.key_value) |
359 | 360 |
|
360 | | - elif value_pb.HasField('boolean_value'): |
| 361 | + elif _has_field(value_pb, 'boolean_value'): |
361 | 362 | result = value_pb.boolean_value |
362 | 363 |
|
363 | | - elif value_pb.HasField('double_value'): |
| 364 | + elif _has_field(value_pb, 'double_value'): |
364 | 365 | result = value_pb.double_value |
365 | 366 |
|
366 | | - elif value_pb.HasField('integer_value'): |
| 367 | + elif _has_field(value_pb, 'integer_value'): |
367 | 368 | result = value_pb.integer_value |
368 | 369 |
|
369 | | - elif value_pb.HasField('string_value'): |
| 370 | + elif _has_field(value_pb, 'string_value'): |
370 | 371 | result = value_pb.string_value |
371 | 372 |
|
372 | | - elif value_pb.HasField('blob_value'): |
| 373 | + elif _has_field(value_pb, 'blob_value'): |
373 | 374 | result = value_pb.blob_value |
374 | 375 |
|
375 | | - elif value_pb.HasField('entity_value'): |
| 376 | + elif _has_field(value_pb, 'entity_value'): |
376 | 377 | result = entity_from_protobuf(value_pb.entity_value) |
377 | 378 |
|
378 | 379 | elif value_pb.list_value: |
@@ -428,7 +429,7 @@ def _prepare_key_for_request(key_pb): |
428 | 429 | :returns: A key which will be added to a request. It will be the |
429 | 430 | original if nothing needs to be changed. |
430 | 431 | """ |
431 | | - if key_pb.partition_id.HasField('dataset_id'): |
| 432 | + if _has_field(key_pb.partition_id, 'dataset_id'): |
432 | 433 | # We remove the dataset_id from the protobuf. This is because |
433 | 434 | # the backend fails a request if the key contains un-prefixed |
434 | 435 | # project. The backend fails because requests to |
|
0 commit comments