Skip to content

Commit

Permalink
add support for list_value, entity_value
Browse files Browse the repository at this point in the history
  • Loading branch information
lucemia committed Sep 17, 2014
1 parent 14019fc commit 0475d5e
Showing 1 changed file with 42 additions and 14 deletions.
56 changes: 42 additions & 14 deletions gcloud/datastore/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

0 comments on commit 0475d5e

Please sign in to comment.