Merge "Add ListField class to resource base"

This commit is contained in:
Zuul 2018-01-02 17:22:01 +00:00 committed by Gerrit Code Review
commit f46ed9b53c
2 changed files with 54 additions and 1 deletions

View File

@ -153,6 +153,40 @@ class CompositeField(collections.Mapping, Field):
return iter(self._subfields)
class ListField(Field):
"""Base class for fields consisting of a list of several sub-fields."""
def __init__(self, *args, **kwargs):
super(ListField, self).__init__(*args, **kwargs)
self._subfields = dict(_collect_fields(self))
def _load(self, body, resource, nested_in=None):
"""Load the field list.
:param body: parent JSON body.
:param resource: parent resource.
:param nested_in: parent resource name (for error reporting only).
:returns: a new list object containing subfields.
"""
nested_in = (nested_in or []) + self._path
values = super(ListField, self)._load(body, resource)
if values is None:
return None
# Initialize the list that will contain each field instance
instances = []
for value in values:
instance = copy.copy(self)
for attr, field in self._subfields.items():
# Hide the Field object behind the real value
setattr(instance, attr, field._load(value,
resource,
nested_in))
instances.append(instance)
return instances
class MappedField(Field):
"""Field taking real value from a mapping."""

View File

@ -155,7 +155,17 @@ TEST_JSON = {
'Field': 'field value'
},
'Mapped': 'raw'
}
},
'ListField': [
{
'String': 'a third string',
'Integer': 1
},
{
'String': 'a fourth string',
'Integer': 2
}
]
}
@ -172,10 +182,16 @@ class NestedTestField(resource_base.CompositeField):
non_existing = resource_base.Field('NonExisting', default=3.14)
class TestListField(resource_base.ListField):
string = resource_base.Field('String', required=True)
integer = resource_base.Field('Integer', adapter=int)
class ComplexResource(resource_base.ResourceBase):
string = resource_base.Field('String', required=True)
integer = resource_base.Field('Integer', adapter=int)
nested = NestedTestField('Nested')
field_list = TestListField('ListField')
non_existing_nested = NestedTestField('NonExistingNested')
non_existing_mapped = resource_base.MappedField('NonExistingMapped',
MAPPING)
@ -198,6 +214,9 @@ class FieldTestCase(base.TestCase):
self.assertEqual('field value', self.test_resource.nested.nested_field)
self.assertEqual('real', self.test_resource.nested.mapped)
self.assertEqual(3.14, self.test_resource.nested.non_existing)
self.assertEqual('a third string',
self.test_resource.field_list[0].string)
self.assertEqual(2, self.test_resource.field_list[1].integer)
self.assertIsNone(self.test_resource.non_existing_nested)
self.assertIsNone(self.test_resource.non_existing_mapped)