Skip to content

Commit

Permalink
Upgrading list_value -> array_value for v1beta3.
Browse files Browse the repository at this point in the history
  • Loading branch information
dhermes committed Feb 13, 2016
1 parent 74fdb65 commit e81f643
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 54 deletions.
27 changes: 14 additions & 13 deletions gcloud/datastore/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,20 +96,20 @@ def _get_meaning(value_pb, is_list=False):
if is_list:
# An empty list will have no values, hence no shared meaning
# set among them.
if len(value_pb.list_value) == 0:
if len(value_pb.array_value.values) == 0:
return None

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

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

if exclude_values.pop():
exclude_from_indexes.append(prop_name)
Expand Down Expand Up @@ -224,7 +225,7 @@ def entity_to_protobuf(entity):
if not value_is_list:
value_pb.exclude_from_indexes = True

for sub_value in value_pb.list_value:
for sub_value in value_pb.array_value.values:
sub_value.exclude_from_indexes = True

# Add meaning information to protobuf.
Expand All @@ -235,7 +236,7 @@ def entity_to_protobuf(entity):
if orig_value is value:
# For lists, we set meaning on each sub-element.
if value_is_list:
for sub_value_pb in value_pb.list_value:
for sub_value_pb in value_pb.array_value.values:
sub_value_pb.meaning = meaning
else:
value_pb.meaning = meaning
Expand Down Expand Up @@ -326,7 +327,7 @@ def _pb_attr_value(val):
elif isinstance(val, Entity):
name, value = 'entity', val
elif isinstance(val, list):
name, value = 'list', val
name, value = 'array', val
else:
raise ValueError("Unknown protobuf attr type %s" % type(val))

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

elif value_pb.list_value:
elif value_pb.array_value.values:
result = [_get_value_from_value_pb(value)
for value in value_pb.list_value]
for value in value_pb.array_value.values]

return result

Expand Down Expand Up @@ -410,8 +411,8 @@ def _set_protobuf_value(value_pb, val):
elif attr == 'entity_value':
entity_pb = entity_to_protobuf(val)
value_pb.entity_value.CopyFrom(entity_pb)
elif attr == 'list_value':
l_pb = value_pb.list_value
elif attr == 'array_value':
l_pb = value_pb.array_value.values
for item in val:
i_pb = l_pb.add()
_set_protobuf_value(i_pb, item)
Expand Down
82 changes: 41 additions & 41 deletions gcloud/datastore/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,18 @@ def test_it(self):
unindexed_val_pb.integer_value = 10
unindexed_val_pb.exclude_from_indexes = True

list_val_pb1 = _new_value_pb(entity_pb, 'baz')
list_pb1 = list_val_pb1.list_value
array_val_pb1 = _new_value_pb(entity_pb, 'baz')
array_pb1 = array_val_pb1.array_value.values

unindexed_list_val_pb = list_pb1.add()
unindexed_list_val_pb.integer_value = 11
unindexed_list_val_pb.exclude_from_indexes = True
unindexed_array_val_pb = array_pb1.add()
unindexed_array_val_pb.integer_value = 11
unindexed_array_val_pb.exclude_from_indexes = True

list_val_pb2 = _new_value_pb(entity_pb, 'qux')
list_pb2 = list_val_pb2.list_value
array_val_pb2 = _new_value_pb(entity_pb, 'qux')
array_pb2 = array_val_pb2.array_value.values

indexed_list_val_pb = list_pb2.add()
indexed_list_val_pb.integer_value = 12
indexed_array_val_pb = array_pb2.add()
indexed_array_val_pb.integer_value = 12

entity = self._callFUT(entity_pb)
self.assertEqual(entity.kind, _KIND)
Expand Down Expand Up @@ -119,14 +119,14 @@ def test_mismatched_value_indexed(self):
entity_pb.key.partition_id.project_id = _PROJECT
entity_pb.key.path.add(kind=_KIND, id=_ID)

list_val_pb = _new_value_pb(entity_pb, 'baz')
list_pb = list_val_pb.list_value
array_val_pb = _new_value_pb(entity_pb, 'baz')
array_pb = array_val_pb.array_value.values

unindexed_value_pb1 = list_pb.add()
unindexed_value_pb1 = array_pb.add()
unindexed_value_pb1.integer_value = 10
unindexed_value_pb1.exclude_from_indexes = True

unindexed_value_pb2 = list_pb.add()
unindexed_value_pb2 = array_pb.add()
unindexed_value_pb2.integer_value = 11

with self.assertRaises(ValueError):
Expand Down Expand Up @@ -305,14 +305,14 @@ def test_inverts_to_protobuf(self):

# Add a list property.
val_pb4 = _new_value_pb(original_pb, 'list-quux')
list_val1 = val_pb4.list_value.add()
list_val1.exclude_from_indexes = True
list_val1.meaning = meaning = 22
list_val1.blob_value = b'\xe2\x98\x83'
list_val2 = val_pb4.list_value.add()
list_val2.exclude_from_indexes = True
list_val2.meaning = meaning
list_val2.blob_value = b'\xe2\x98\x85'
array_val1 = val_pb4.array_value.values.add()
array_val1.exclude_from_indexes = False
array_val1.meaning = meaning = 22
array_val1.blob_value = b'\xe2\x98\x83'
array_val2 = val_pb4.array_value.add()
array_val2.exclude_from_indexes = False
array_val2.meaning = meaning
array_val2.blob_value = b'\xe2\x98\x85'

# Convert to the user-space Entity.
entity = entity_from_protobuf(original_pb)
Expand Down Expand Up @@ -491,10 +491,10 @@ def test_entity(self):
self.assertEqual(name, 'entity_value')
self.assertTrue(value is entity)

def test_list(self):
def test_array(self):
values = ['a', 0, 3.14]
name, value = self._callFUT(values)
self.assertEqual(name, 'list_value')
self.assertEqual(name, 'array_value')
self.assertTrue(value is values)

def test_object(self):
Expand Down Expand Up @@ -574,14 +574,14 @@ def test_entity(self):
self.assertTrue(isinstance(entity, Entity))
self.assertEqual(entity['foo'], 'Foo')

def test_list(self):
def test_array(self):
from gcloud.datastore._generated import entity_pb2

pb = entity_pb2.Value()
list_pb = pb.list_value
item_pb = list_pb.add()
array_pb = pb.array_value.values
item_pb = array_pb.add()
item_pb.string_value = 'Foo'
item_pb = list_pb.add()
item_pb = array_pb.add()
item_pb.string_value = 'Bar'
items = self._callFUT(pb)
self.assertEqual(items, ['Foo', 'Bar'])
Expand Down Expand Up @@ -723,11 +723,11 @@ def test_entity_w_key(self):
self.assertEqual(list(prop_dict.keys()), [name])
self.assertEqual(prop_dict[name].string_value, value)

def test_list(self):
def test_array(self):
pb = self._makePB()
values = [u'a', 0, 3.14]
self._callFUT(pb, values)
marshalled = pb.list_value
marshalled = pb.array_value.values
self.assertEqual(len(marshalled), len(values))
self.assertEqual(marshalled[0].string_value, values[0])
self.assertEqual(marshalled[1].integer_value, values[1])
Expand Down Expand Up @@ -836,23 +836,23 @@ def test_single(self):
result = self._callFUT(value_pb)
self.assertEqual(meaning, result)

def test_empty_list_value(self):
def test_empty_array_value(self):
from gcloud.datastore._generated import entity_pb2

value_pb = entity_pb2.Value()
value_pb.list_value.add()
value_pb.list_value.pop()
value_pb.array_value.values.add()
value_pb.array_value.values.pop()

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

def test_list_value(self):
def test_array_value(self):
from gcloud.datastore._generated import entity_pb2

value_pb = entity_pb2.Value()
meaning = 9
sub_value_pb1 = value_pb.list_value.add()
sub_value_pb2 = value_pb.list_value.add()
sub_value_pb1 = value_pb.array_value.values.add()
sub_value_pb2 = value_pb.array_value.values.add()

sub_value_pb1.meaning = sub_value_pb2.meaning = meaning
sub_value_pb1.string_value = u'hi'
Expand All @@ -861,14 +861,14 @@ def test_list_value(self):
result = self._callFUT(value_pb, is_list=True)
self.assertEqual(meaning, result)

def test_list_value_disagreeing(self):
def test_array_value_disagreeing(self):
from gcloud.datastore._generated import entity_pb2

value_pb = entity_pb2.Value()
meaning1 = 9
meaning2 = 10
sub_value_pb1 = value_pb.list_value.add()
sub_value_pb2 = value_pb.list_value.add()
sub_value_pb1 = value_pb.array_value.values.add()
sub_value_pb2 = value_pb.array_value.values.add()

sub_value_pb1.meaning = meaning1
sub_value_pb2.meaning = meaning2
Expand All @@ -878,13 +878,13 @@ def test_list_value_disagreeing(self):
with self.assertRaises(ValueError):
self._callFUT(value_pb, is_list=True)

def test_list_value_partially_unset(self):
def test_array_value_partially_unset(self):
from gcloud.datastore._generated import entity_pb2

value_pb = entity_pb2.Value()
meaning1 = 9
sub_value_pb1 = value_pb.list_value.add()
sub_value_pb2 = value_pb.list_value.add()
sub_value_pb1 = value_pb.array_value.values.add()
sub_value_pb2 = value_pb.array_value.values.add()

sub_value_pb1.meaning = meaning1
sub_value_pb1.string_value = u'hi'
Expand Down

0 comments on commit e81f643

Please sign in to comment.