From 0475d5ee2ac53ecd9a711c747916fade6e2d9c02 Mon Sep 17 00:00:00 2001 From: lucemia Date: Wed, 17 Sep 2014 12:13:26 +0800 Subject: [PATCH] add support for list_value, entity_value --- gcloud/datastore/helpers.py | 56 +++++++++++++++++++++++++++---------- 1 file changed, 42 insertions(+), 14 deletions(-) diff --git a/gcloud/datastore/helpers.py b/gcloud/datastore/helpers.py index 23075381ff69..3ae1556594b2 100644 --- a/gcloud/datastore/helpers.py +++ b/gcloud/datastore/helpers.py @@ -60,7 +60,7 @@ def get_protobuf_attribute_and_value(val): return name + '_value', value -def get_value_from_protobuf(pb): +def get_value_from_value_pb(value_pb): """Given a protobuf for a Property, get the correct value. The Cloud Datastore Protobuf API returns a Property Protobuf @@ -75,27 +75,55 @@ def get_value_from_protobuf(pb): :returns: The value provided by the Protobuf. """ - - if pb.value.HasField('timestamp_microseconds_value'): - microseconds = pb.value.timestamp_microseconds_value + if value_pb.HasField('timestamp_microseconds_value'): + microseconds = value_pb.timestamp_microseconds_value return (datetime.utcfromtimestamp(0) + timedelta(microseconds=microseconds)) - elif pb.value.HasField('key_value'): - return Key.from_protobuf(pb.value.key_value) + elif value_pb.HasField('key_value'): + return Key.from_protobuf(value_pb.key_value) + + elif value_pb.HasField('boolean_value'): + return value_pb.boolean_value + + elif value_pb.HasField('double_value'): + return value_pb.double_value + + elif value_pb.HasField('integer_value'): + return value_pb.integer_value + + elif value_pb.HasField('string_value'): + return value_pb.string_value - elif pb.value.HasField('boolean_value'): - return pb.value.boolean_value + elif value_pb.HasField('blob_key_value'): + return value_pb.blob_key_value - elif pb.value.HasField('double_value'): - return pb.value.double_value + elif value_pb.HasField('blob_value'): + return value_pb.blob_value - elif pb.value.HasField('integer_value'): - return pb.value.integer_value + elif value_pb.HasField('entity_value'): + return value_pb.entity_value - elif pb.value.HasField('string_value'): - return pb.value.string_value + elif value_pb.list_value: + return [get_value_from_value_pb(k) for k in value_pb.list_value] else: # TODO(jjg): Should we raise a ValueError here? return None + +def get_value_from_protobuf(pb): + """Given a protobuf for a Property, get the correct value. + + The Cloud Datastore Protobuf API returns a Property Protobuf + which has one value set and the rest blank. + This method retrieves the the one value provided. + + Some work is done to coerce the return value into a more useful type + (particularly in the case of a timestamp value, or a key value). + + :type pb: :class:`gcloud.datastore.datastore_v1_pb2.Property` + :param pb: The Property Protobuf. + + :returns: The value provided by the Protobuf. + """ + return get_value_from_value_pb(pb.value)