Skip to content

Commit 8f3fe6d

Browse files
committed
Merge pull request #1460 from dhermes/list-to-array-val-1288
Upgrading list_value -> array_value for v1beta3.
2 parents 74fdb65 + e81f643 commit 8f3fe6d

File tree

2 files changed

+55
-54
lines changed

2 files changed

+55
-54
lines changed

gcloud/datastore/helpers.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -96,20 +96,20 @@ def _get_meaning(value_pb, is_list=False):
9696
if is_list:
9797
# An empty list will have no values, hence no shared meaning
9898
# set among them.
99-
if len(value_pb.list_value) == 0:
99+
if len(value_pb.array_value.values) == 0:
100100
return None
101101

102102
# We check among all the meanings, some of which may be None,
103103
# the rest which may be enum/int values.
104104
all_meanings = set(_get_meaning(sub_value_pb)
105-
for sub_value_pb in value_pb.list_value)
105+
for sub_value_pb in value_pb.array_value.values)
106106
meaning = all_meanings.pop()
107107
# The value we popped off should have been unique. If not
108108
# then we can't handle a list with values that have more
109109
# than one meaning.
110110
if all_meanings:
111111
raise ValueError('Different meanings set on values '
112-
'within a list_value')
112+
'within an array_value')
113113
elif value_pb.meaning: # Simple field (int32)
114114
meaning = value_pb.meaning
115115

@@ -179,10 +179,11 @@ def entity_from_protobuf(pb):
179179
# in a list agree.
180180
if is_list:
181181
exclude_values = set(value_pb.exclude_from_indexes
182-
for value_pb in value_pb.list_value)
182+
for value_pb in value_pb.array_value.values)
183183
if len(exclude_values) != 1:
184-
raise ValueError('For a list_value, subvalues must either all '
185-
'be indexed or all excluded from indexes.')
184+
raise ValueError('For an array_value, subvalues must either '
185+
'all be indexed or all excluded from '
186+
'indexes.')
186187

187188
if exclude_values.pop():
188189
exclude_from_indexes.append(prop_name)
@@ -224,7 +225,7 @@ def entity_to_protobuf(entity):
224225
if not value_is_list:
225226
value_pb.exclude_from_indexes = True
226227

227-
for sub_value in value_pb.list_value:
228+
for sub_value in value_pb.array_value.values:
228229
sub_value.exclude_from_indexes = True
229230

230231
# Add meaning information to protobuf.
@@ -235,7 +236,7 @@ def entity_to_protobuf(entity):
235236
if orig_value is value:
236237
# For lists, we set meaning on each sub-element.
237238
if value_is_list:
238-
for sub_value_pb in value_pb.list_value:
239+
for sub_value_pb in value_pb.array_value.values:
239240
sub_value_pb.meaning = meaning
240241
else:
241242
value_pb.meaning = meaning
@@ -326,7 +327,7 @@ def _pb_attr_value(val):
326327
elif isinstance(val, Entity):
327328
name, value = 'entity', val
328329
elif isinstance(val, list):
329-
name, value = 'list', val
330+
name, value = 'array', val
330331
else:
331332
raise ValueError("Unknown protobuf attr type %s" % type(val))
332333

@@ -374,9 +375,9 @@ def _get_value_from_value_pb(value_pb):
374375
elif value_pb.HasField('entity_value'): # Message field (Entity)
375376
result = entity_from_protobuf(value_pb.entity_value)
376377

377-
elif value_pb.list_value:
378+
elif value_pb.array_value.values:
378379
result = [_get_value_from_value_pb(value)
379-
for value in value_pb.list_value]
380+
for value in value_pb.array_value.values]
380381

381382
return result
382383

@@ -410,8 +411,8 @@ def _set_protobuf_value(value_pb, val):
410411
elif attr == 'entity_value':
411412
entity_pb = entity_to_protobuf(val)
412413
value_pb.entity_value.CopyFrom(entity_pb)
413-
elif attr == 'list_value':
414-
l_pb = value_pb.list_value
414+
elif attr == 'array_value':
415+
l_pb = value_pb.array_value.values
415416
for item in val:
416417
i_pb = l_pb.add()
417418
_set_protobuf_value(i_pb, item)

gcloud/datastore/test_helpers.py

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -80,18 +80,18 @@ def test_it(self):
8080
unindexed_val_pb.integer_value = 10
8181
unindexed_val_pb.exclude_from_indexes = True
8282

83-
list_val_pb1 = _new_value_pb(entity_pb, 'baz')
84-
list_pb1 = list_val_pb1.list_value
83+
array_val_pb1 = _new_value_pb(entity_pb, 'baz')
84+
array_pb1 = array_val_pb1.array_value.values
8585

86-
unindexed_list_val_pb = list_pb1.add()
87-
unindexed_list_val_pb.integer_value = 11
88-
unindexed_list_val_pb.exclude_from_indexes = True
86+
unindexed_array_val_pb = array_pb1.add()
87+
unindexed_array_val_pb.integer_value = 11
88+
unindexed_array_val_pb.exclude_from_indexes = True
8989

90-
list_val_pb2 = _new_value_pb(entity_pb, 'qux')
91-
list_pb2 = list_val_pb2.list_value
90+
array_val_pb2 = _new_value_pb(entity_pb, 'qux')
91+
array_pb2 = array_val_pb2.array_value.values
9292

93-
indexed_list_val_pb = list_pb2.add()
94-
indexed_list_val_pb.integer_value = 12
93+
indexed_array_val_pb = array_pb2.add()
94+
indexed_array_val_pb.integer_value = 12
9595

9696
entity = self._callFUT(entity_pb)
9797
self.assertEqual(entity.kind, _KIND)
@@ -119,14 +119,14 @@ def test_mismatched_value_indexed(self):
119119
entity_pb.key.partition_id.project_id = _PROJECT
120120
entity_pb.key.path.add(kind=_KIND, id=_ID)
121121

122-
list_val_pb = _new_value_pb(entity_pb, 'baz')
123-
list_pb = list_val_pb.list_value
122+
array_val_pb = _new_value_pb(entity_pb, 'baz')
123+
array_pb = array_val_pb.array_value.values
124124

125-
unindexed_value_pb1 = list_pb.add()
125+
unindexed_value_pb1 = array_pb.add()
126126
unindexed_value_pb1.integer_value = 10
127127
unindexed_value_pb1.exclude_from_indexes = True
128128

129-
unindexed_value_pb2 = list_pb.add()
129+
unindexed_value_pb2 = array_pb.add()
130130
unindexed_value_pb2.integer_value = 11
131131

132132
with self.assertRaises(ValueError):
@@ -305,14 +305,14 @@ def test_inverts_to_protobuf(self):
305305

306306
# Add a list property.
307307
val_pb4 = _new_value_pb(original_pb, 'list-quux')
308-
list_val1 = val_pb4.list_value.add()
309-
list_val1.exclude_from_indexes = True
310-
list_val1.meaning = meaning = 22
311-
list_val1.blob_value = b'\xe2\x98\x83'
312-
list_val2 = val_pb4.list_value.add()
313-
list_val2.exclude_from_indexes = True
314-
list_val2.meaning = meaning
315-
list_val2.blob_value = b'\xe2\x98\x85'
308+
array_val1 = val_pb4.array_value.values.add()
309+
array_val1.exclude_from_indexes = False
310+
array_val1.meaning = meaning = 22
311+
array_val1.blob_value = b'\xe2\x98\x83'
312+
array_val2 = val_pb4.array_value.add()
313+
array_val2.exclude_from_indexes = False
314+
array_val2.meaning = meaning
315+
array_val2.blob_value = b'\xe2\x98\x85'
316316

317317
# Convert to the user-space Entity.
318318
entity = entity_from_protobuf(original_pb)
@@ -491,10 +491,10 @@ def test_entity(self):
491491
self.assertEqual(name, 'entity_value')
492492
self.assertTrue(value is entity)
493493

494-
def test_list(self):
494+
def test_array(self):
495495
values = ['a', 0, 3.14]
496496
name, value = self._callFUT(values)
497-
self.assertEqual(name, 'list_value')
497+
self.assertEqual(name, 'array_value')
498498
self.assertTrue(value is values)
499499

500500
def test_object(self):
@@ -574,14 +574,14 @@ def test_entity(self):
574574
self.assertTrue(isinstance(entity, Entity))
575575
self.assertEqual(entity['foo'], 'Foo')
576576

577-
def test_list(self):
577+
def test_array(self):
578578
from gcloud.datastore._generated import entity_pb2
579579

580580
pb = entity_pb2.Value()
581-
list_pb = pb.list_value
582-
item_pb = list_pb.add()
581+
array_pb = pb.array_value.values
582+
item_pb = array_pb.add()
583583
item_pb.string_value = 'Foo'
584-
item_pb = list_pb.add()
584+
item_pb = array_pb.add()
585585
item_pb.string_value = 'Bar'
586586
items = self._callFUT(pb)
587587
self.assertEqual(items, ['Foo', 'Bar'])
@@ -723,11 +723,11 @@ def test_entity_w_key(self):
723723
self.assertEqual(list(prop_dict.keys()), [name])
724724
self.assertEqual(prop_dict[name].string_value, value)
725725

726-
def test_list(self):
726+
def test_array(self):
727727
pb = self._makePB()
728728
values = [u'a', 0, 3.14]
729729
self._callFUT(pb, values)
730-
marshalled = pb.list_value
730+
marshalled = pb.array_value.values
731731
self.assertEqual(len(marshalled), len(values))
732732
self.assertEqual(marshalled[0].string_value, values[0])
733733
self.assertEqual(marshalled[1].integer_value, values[1])
@@ -836,23 +836,23 @@ def test_single(self):
836836
result = self._callFUT(value_pb)
837837
self.assertEqual(meaning, result)
838838

839-
def test_empty_list_value(self):
839+
def test_empty_array_value(self):
840840
from gcloud.datastore._generated import entity_pb2
841841

842842
value_pb = entity_pb2.Value()
843-
value_pb.list_value.add()
844-
value_pb.list_value.pop()
843+
value_pb.array_value.values.add()
844+
value_pb.array_value.values.pop()
845845

846846
result = self._callFUT(value_pb, is_list=True)
847847
self.assertEqual(None, result)
848848

849-
def test_list_value(self):
849+
def test_array_value(self):
850850
from gcloud.datastore._generated import entity_pb2
851851

852852
value_pb = entity_pb2.Value()
853853
meaning = 9
854-
sub_value_pb1 = value_pb.list_value.add()
855-
sub_value_pb2 = value_pb.list_value.add()
854+
sub_value_pb1 = value_pb.array_value.values.add()
855+
sub_value_pb2 = value_pb.array_value.values.add()
856856

857857
sub_value_pb1.meaning = sub_value_pb2.meaning = meaning
858858
sub_value_pb1.string_value = u'hi'
@@ -861,14 +861,14 @@ def test_list_value(self):
861861
result = self._callFUT(value_pb, is_list=True)
862862
self.assertEqual(meaning, result)
863863

864-
def test_list_value_disagreeing(self):
864+
def test_array_value_disagreeing(self):
865865
from gcloud.datastore._generated import entity_pb2
866866

867867
value_pb = entity_pb2.Value()
868868
meaning1 = 9
869869
meaning2 = 10
870-
sub_value_pb1 = value_pb.list_value.add()
871-
sub_value_pb2 = value_pb.list_value.add()
870+
sub_value_pb1 = value_pb.array_value.values.add()
871+
sub_value_pb2 = value_pb.array_value.values.add()
872872

873873
sub_value_pb1.meaning = meaning1
874874
sub_value_pb2.meaning = meaning2
@@ -878,13 +878,13 @@ def test_list_value_disagreeing(self):
878878
with self.assertRaises(ValueError):
879879
self._callFUT(value_pb, is_list=True)
880880

881-
def test_list_value_partially_unset(self):
881+
def test_array_value_partially_unset(self):
882882
from gcloud.datastore._generated import entity_pb2
883883

884884
value_pb = entity_pb2.Value()
885885
meaning1 = 9
886-
sub_value_pb1 = value_pb.list_value.add()
887-
sub_value_pb2 = value_pb.list_value.add()
886+
sub_value_pb1 = value_pb.array_value.values.add()
887+
sub_value_pb2 = value_pb.array_value.values.add()
888888

889889
sub_value_pb1.meaning = meaning1
890890
sub_value_pb1.string_value = u'hi'

0 commit comments

Comments
 (0)