Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: strip proto wrappers in '_helpers.decode_{value,dict}' #458

Merged
merged 2 commits into from Sep 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 19 additions & 12 deletions google/cloud/firestore_v1/_helpers.py
Expand Up @@ -349,30 +349,35 @@ def decode_value(
NotImplementedError: If the ``value_type`` is ``reference_value``.
ValueError: If the ``value_type`` is unknown.
"""
value_type = value._pb.WhichOneof("value_type")
value_pb = getattr(value, "_pb", value)
value_type = value_pb.WhichOneof("value_type")

if value_type == "null_value":
return None
elif value_type == "boolean_value":
return value.boolean_value
return value_pb.boolean_value
elif value_type == "integer_value":
return value.integer_value
return value_pb.integer_value
elif value_type == "double_value":
return value.double_value
return value_pb.double_value
elif value_type == "timestamp_value":
return DatetimeWithNanoseconds.from_timestamp_pb(value._pb.timestamp_value)
return DatetimeWithNanoseconds.from_timestamp_pb(value_pb.timestamp_value)
elif value_type == "string_value":
return value.string_value
return value_pb.string_value
elif value_type == "bytes_value":
return value.bytes_value
return value_pb.bytes_value
elif value_type == "reference_value":
return reference_value_to_document(value.reference_value, client)
return reference_value_to_document(value_pb.reference_value, client)
elif value_type == "geo_point_value":
return GeoPoint(value.geo_point_value.latitude, value.geo_point_value.longitude)
return GeoPoint(
value_pb.geo_point_value.latitude, value_pb.geo_point_value.longitude
)
elif value_type == "array_value":
return [decode_value(element, client) for element in value.array_value.values]
return [
decode_value(element, client) for element in value_pb.array_value.values
]
elif value_type == "map_value":
return decode_dict(value.map_value.fields, client)
return decode_dict(value_pb.map_value.fields, client)
else:
raise ValueError("Unknown ``value_type``", value_type)

Expand All @@ -391,7 +396,9 @@ def decode_dict(value_fields, client) -> dict:
str, bytes, dict, ~google.cloud.Firestore.GeoPoint]]: A dictionary
of native Python values converted from the ``value_fields``.
"""
return {key: decode_value(value, client) for key, value in value_fields.items()}
value_fields_pb = getattr(value_fields, "_pb", value_fields)

return {key: decode_value(value, client) for key, value in value_fields_pb.items()}


def get_doc_id(document_pb, expected_prefix) -> str:
Expand Down
3 changes: 0 additions & 3 deletions tests/unit/v1/test__helpers.py
Expand Up @@ -618,9 +618,6 @@ def _call_fut(value_fields, client=mock.sentinel.client):

return decode_dict(value_fields, client)

@unittest.skipIf(
(3,) <= sys.version_info < (3, 4, 4), "known datetime bug (bpo-23517) in Python"
)
def test_many_types(self):
from google.protobuf import struct_pb2
from google.protobuf import timestamp_pb2
Expand Down