diff options
| author | Dan Smith <danms@us.ibm.com> | 2013-06-24 12:54:05 -0700 |
|---|---|---|
| committer | Dan Smith <danms@us.ibm.com> | 2013-06-26 15:55:47 -0700 |
| commit | 68cb4d53385821c3ffdc40c299a77d11a7f98f27 (patch) | |
| tree | 8d9dbe5fedc3cca0ec8c815e4d4421696e89f165 /nova/objects | |
| parent | 73eaa61f468d1c4f6f800862e4eb4d8a70f88bb1 (diff) | |
| download | nova-68cb4d53385821c3ffdc40c299a77d11a7f98f27.tar.gz nova-68cb4d53385821c3ffdc40c299a77d11a7f98f27.tar.xz nova-68cb4d53385821c3ffdc40c299a77d11a7f98f27.zip | |
Add obj_to_primitive() to recursively primitiveize objects
Occasionally we need to recursively process a NovaObject into a
representative dict or list. This patch adds a function to do that
recursively, accounting for nested objects and lists of objects.
Related to blueprint unified-object-model
Change-Id: I6adb84f9e5a0564ec4af14200e26e838b4a5bb6e
Diffstat (limited to 'nova/objects')
| -rw-r--r-- | nova/objects/base.py | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/nova/objects/base.py b/nova/objects/base.py index 147d55c23..4f973de0c 100644 --- a/nova/objects/base.py +++ b/nova/objects/base.py @@ -464,3 +464,20 @@ class NovaObjectSerializer(nova.openstack.common.rpc.serializer.Serializer): entity = self._process_iterable(context, self.deserialize_entity, entity) return entity + + +def obj_to_primitive(obj): + """Recrusively turn an object into a python primitive. + + A NovaObject becomes a dict, and anything that implements ObjectListBase + becomes a list. + """ + if isinstance(obj, ObjectListBase): + return [obj_to_primitive(x) for x in obj] + elif isinstance(obj, NovaObject): + result = {} + for key, value in obj.iteritems(): + result[key] = obj_to_primitive(value) + return result + else: + return obj |
