Skip to content

Commit 0475d5e

Browse files
committed
add support for list_value, entity_value
1 parent 14019fc commit 0475d5e

File tree

1 file changed

+42
-14
lines changed

1 file changed

+42
-14
lines changed

gcloud/datastore/helpers.py

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def get_protobuf_attribute_and_value(val):
6060
return name + '_value', value
6161

6262

63-
def get_value_from_protobuf(pb):
63+
def get_value_from_value_pb(value_pb):
6464
"""Given a protobuf for a Property, get the correct value.
6565
6666
The Cloud Datastore Protobuf API returns a Property Protobuf
@@ -75,27 +75,55 @@ def get_value_from_protobuf(pb):
7575
7676
:returns: The value provided by the Protobuf.
7777
"""
78-
79-
if pb.value.HasField('timestamp_microseconds_value'):
80-
microseconds = pb.value.timestamp_microseconds_value
78+
if value_pb.HasField('timestamp_microseconds_value'):
79+
microseconds = value_pb.timestamp_microseconds_value
8180
return (datetime.utcfromtimestamp(0) +
8281
timedelta(microseconds=microseconds))
8382

84-
elif pb.value.HasField('key_value'):
85-
return Key.from_protobuf(pb.value.key_value)
83+
elif value_pb.HasField('key_value'):
84+
return Key.from_protobuf(value_pb.key_value)
85+
86+
elif value_pb.HasField('boolean_value'):
87+
return value_pb.boolean_value
88+
89+
elif value_pb.HasField('double_value'):
90+
return value_pb.double_value
91+
92+
elif value_pb.HasField('integer_value'):
93+
return value_pb.integer_value
94+
95+
elif value_pb.HasField('string_value'):
96+
return value_pb.string_value
8697

87-
elif pb.value.HasField('boolean_value'):
88-
return pb.value.boolean_value
98+
elif value_pb.HasField('blob_key_value'):
99+
return value_pb.blob_key_value
89100

90-
elif pb.value.HasField('double_value'):
91-
return pb.value.double_value
101+
elif value_pb.HasField('blob_value'):
102+
return value_pb.blob_value
92103

93-
elif pb.value.HasField('integer_value'):
94-
return pb.value.integer_value
104+
elif value_pb.HasField('entity_value'):
105+
return value_pb.entity_value
95106

96-
elif pb.value.HasField('string_value'):
97-
return pb.value.string_value
107+
elif value_pb.list_value:
108+
return [get_value_from_value_pb(k) for k in value_pb.list_value]
98109

99110
else:
100111
# TODO(jjg): Should we raise a ValueError here?
101112
return None
113+
114+
def get_value_from_protobuf(pb):
115+
"""Given a protobuf for a Property, get the correct value.
116+
117+
The Cloud Datastore Protobuf API returns a Property Protobuf
118+
which has one value set and the rest blank.
119+
This method retrieves the the one value provided.
120+
121+
Some work is done to coerce the return value into a more useful type
122+
(particularly in the case of a timestamp value, or a key value).
123+
124+
:type pb: :class:`gcloud.datastore.datastore_v1_pb2.Property`
125+
:param pb: The Property Protobuf.
126+
127+
:returns: The value provided by the Protobuf.
128+
"""
129+
return get_value_from_value_pb(pb.value)

0 commit comments

Comments
 (0)