Merge "Remove ObjectListBase from sysinv"
This commit is contained in:
commit
44f35b8eb8
|
@ -485,63 +485,6 @@ class SysinvObject(object):
|
|||
return cls._from_db_object(cls(), db_obj)
|
||||
|
||||
|
||||
class ObjectListBase(object):
|
||||
"""Mixin class for lists of objects.
|
||||
|
||||
This mixin class can be added as a base class for an object that
|
||||
is implementing a list of objects. It adds a single field of 'objects',
|
||||
which is the list store, and behaves like a list itself. It supports
|
||||
serialization of the list of objects automatically.
|
||||
"""
|
||||
fields = {
|
||||
'objects': list,
|
||||
}
|
||||
|
||||
def __iter__(self):
|
||||
"""List iterator interface."""
|
||||
return iter(self.objects)
|
||||
|
||||
def __len__(self):
|
||||
"""List length."""
|
||||
return len(self.objects)
|
||||
|
||||
def __getitem__(self, index):
|
||||
"""List index access."""
|
||||
if isinstance(index, slice):
|
||||
new_obj = self.__class__()
|
||||
new_obj.objects = self.objects[index]
|
||||
# NOTE(danms): We must be mixed in with an SysinvObject!
|
||||
new_obj.obj_reset_changes() # pylint: disable=no-member
|
||||
new_obj._context = self._context
|
||||
return new_obj
|
||||
return self.objects[index]
|
||||
|
||||
def __contains__(self, value):
|
||||
"""List membership test."""
|
||||
return value in self.objects
|
||||
|
||||
def count(self, value):
|
||||
"""List count of value occurrences."""
|
||||
return self.objects.count(value)
|
||||
|
||||
def index(self, value):
|
||||
"""List index of value."""
|
||||
return self.objects.index(value)
|
||||
|
||||
def _attr_objects_to_primitive(self):
|
||||
"""Serialization of object list."""
|
||||
return [x.obj_to_primitive() for x in self.objects]
|
||||
|
||||
def _attr_objects_from_primitive(self, value):
|
||||
"""Deserialization of object list."""
|
||||
objects = []
|
||||
for entity in value:
|
||||
obj = SysinvObject.obj_from_primitive(entity,
|
||||
context=self._context)
|
||||
objects.append(obj)
|
||||
return objects
|
||||
|
||||
|
||||
class SysinvObjectSerializer(rpc_serializer.Serializer):
|
||||
"""A SysinvObject-aware Serializer.
|
||||
|
||||
|
@ -588,12 +531,9 @@ class SysinvObjectSerializer(rpc_serializer.Serializer):
|
|||
def obj_to_primitive(obj):
|
||||
"""Recursively turn an object into a python primitive.
|
||||
|
||||
An SysinvObject becomes a dict, and anything that implements ObjectListBase
|
||||
becomes a list.
|
||||
A SysinvObject becomes a dict
|
||||
"""
|
||||
if isinstance(obj, ObjectListBase):
|
||||
return [obj_to_primitive(x) for x in obj]
|
||||
elif isinstance(obj, SysinvObject):
|
||||
if isinstance(obj, SysinvObject):
|
||||
result = {}
|
||||
for key, value in obj.items():
|
||||
result[key] = obj_to_primitive(value)
|
||||
|
|
|
@ -1,3 +1,7 @@
|
|||
# Copyright (c) 2023 Wind River Systems, Inc.
|
||||
#
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
#
|
||||
# Copyright 2013 IBM Corp.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
|
@ -11,7 +15,7 @@
|
|||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
#
|
||||
import contextlib
|
||||
import datetime
|
||||
import gettext
|
||||
|
@ -178,13 +182,6 @@ class TestUtils(test_base.TestCase):
|
|||
self.assertEqual(utils.dt_deserializer(None, None), None)
|
||||
self.assertRaises(ValueError, utils.dt_deserializer, None, 'foo')
|
||||
|
||||
def test_obj_to_primitive_list(self):
|
||||
class MyList(base.ObjectListBase, base.SysinvObject):
|
||||
pass
|
||||
mylist = MyList()
|
||||
mylist.objects = [1, 2, 3]
|
||||
self.assertEqual([1, 2, 3], base.obj_to_primitive(mylist))
|
||||
|
||||
def test_obj_to_primitive_dict(self):
|
||||
myobj = MyObj()
|
||||
myobj.foo = 1
|
||||
|
@ -192,17 +189,6 @@ class TestUtils(test_base.TestCase):
|
|||
self.assertEqual({'foo': 1, 'bar': 'foo'},
|
||||
base.obj_to_primitive(myobj))
|
||||
|
||||
def test_obj_to_primitive_recursive(self):
|
||||
class MyList(base.ObjectListBase, base.SysinvObject):
|
||||
pass
|
||||
|
||||
mylist = MyList()
|
||||
mylist.objects = [MyObj(), MyObj()]
|
||||
for i, value in enumerate(mylist):
|
||||
value.foo = i
|
||||
self.assertEqual([{'foo': 0}, {'foo': 1}],
|
||||
base.obj_to_primitive(mylist))
|
||||
|
||||
|
||||
class _BaseTestCase(test_base.TestCase):
|
||||
def setUp(self):
|
||||
|
@ -447,43 +433,6 @@ class TestObject(_LocalTest, _TestObjectMixin):
|
|||
pass
|
||||
|
||||
|
||||
class TestObjectListBase(test_base.TestCase):
|
||||
def test_list_like_operations(self):
|
||||
class Foo(base.ObjectListBase, base.SysinvObject):
|
||||
pass
|
||||
|
||||
objlist = Foo()
|
||||
objlist._context = 'foo'
|
||||
objlist.objects = [1, 2, 3]
|
||||
self.assertEqual(list(objlist), objlist.objects)
|
||||
self.assertEqual(len(objlist), 3)
|
||||
self.assertIn(2, objlist)
|
||||
self.assertEqual(list(objlist[:1]), [1])
|
||||
self.assertEqual(objlist[:1]._context, 'foo')
|
||||
self.assertEqual(objlist[2], 3)
|
||||
self.assertEqual(objlist.count(1), 1)
|
||||
self.assertEqual(objlist.index(2), 1)
|
||||
|
||||
def test_serialization(self):
|
||||
class Foo(base.ObjectListBase, base.SysinvObject):
|
||||
pass
|
||||
|
||||
class Bar(base.SysinvObject):
|
||||
fields = {'foo': str}
|
||||
|
||||
obj = Foo()
|
||||
obj.objects = []
|
||||
for i in 'abc':
|
||||
bar = Bar()
|
||||
bar.foo = i
|
||||
obj.objects.append(bar)
|
||||
|
||||
obj2 = base.SysinvObject.obj_from_primitive(obj.obj_to_primitive())
|
||||
self.assertFalse(obj is obj2)
|
||||
self.assertEqual([x.foo for x in obj],
|
||||
[y.foo for y in obj2])
|
||||
|
||||
|
||||
class TestObjectSerializer(test_base.TestCase):
|
||||
def test_serialize_entity_primitive(self):
|
||||
ser = base.SysinvObjectSerializer()
|
||||
|
|
Loading…
Reference in New Issue