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