mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-08 14:22:46 +00:00
Avoid default serialization of classes in dump_attrs
For playbook base objects, when dumping attributes via dump_attrs() an attribute like loop_control is a class. Using the default serialization for these is slow and consumes a lot of memory. Since LoopControl is also based on the Base class, we can use serialize() instead and save a lot of resources. This also adds a from_attrs() complimentary method to nicely turn the dumped attrs back into proper field attributes. Fixes #23579
This commit is contained in:
@@ -506,10 +506,28 @@ class Base(with_metaclass(BaseMeta, object)):
|
||||
Dumps all attributes to a dictionary
|
||||
'''
|
||||
attrs = dict()
|
||||
for name in self._valid_attrs.keys():
|
||||
attrs[name] = getattr(self, name)
|
||||
for (name, attribute) in iteritems(self._valid_attrs):
|
||||
attr = getattr(self, name)
|
||||
if attribute.isa == 'class' and attr is not None and hasattr(attr, 'serialize'):
|
||||
attrs[name] = attr.serialize()
|
||||
else:
|
||||
attrs[name] = attr
|
||||
return attrs
|
||||
|
||||
def from_attrs(self, attrs):
|
||||
'''
|
||||
Loads attributes from a dictionary
|
||||
'''
|
||||
for (attr, value) in iteritems(attrs):
|
||||
if attr in self._valid_attrs:
|
||||
attribute = self._valid_attrs[attr]
|
||||
if attribute.isa == 'class' and isinstance(value, dict):
|
||||
obj = attribute.class_type()
|
||||
obj.deserialize(value)
|
||||
setattr(self, attr, obj)
|
||||
else:
|
||||
setattr(self, attr, value)
|
||||
|
||||
def serialize(self):
|
||||
'''
|
||||
Serializes the object derived from the base object into
|
||||
|
||||
Reference in New Issue
Block a user