From fae24eee4af6044c712d799ff09442b61cf6dab2 Mon Sep 17 00:00:00 2001 From: Hironori Shiina Date: Fri, 5 Aug 2016 02:08:36 +0900 Subject: [PATCH] Modify flatten method to display an empty dict When a dict is flattened, if a value of an item is empty, e.g. {"cpu_info": {}}, the item is not displayed. This patch fixes _flatten method to display such an item including an empty dict. Change-Id: I2e4ada0de217c5c4ddea10f3f283c6d8e78083b6 Related-Bug: #1594230 --- novaclient/tests/unit/test_utils.py | 6 ++++-- novaclient/utils.py | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/novaclient/tests/unit/test_utils.py b/novaclient/tests/unit/test_utils.py index 64a947ea5..919d4cb2b 100644 --- a/novaclient/tests/unit/test_utils.py +++ b/novaclient/tests/unit/test_utils.py @@ -340,7 +340,8 @@ class FlattenTestCase(test_utils.TestCase): 'b4': {'c1': ['l', 'l', ['l']], 'c2': 'string'}}, 'a2': ['l'], - 'a3': ('t',)}) + 'a3': ('t',), + 'a4': {}}) self.assertEqual({'a1_b1': 1234, 'a1_b2': 'string', @@ -348,7 +349,8 @@ class FlattenTestCase(test_utils.TestCase): 'a1_b4_c1': ['l', 'l', ['l']], 'a1_b4_c2': 'string', 'a2': ['l'], - 'a3': ('t',)}, + 'a3': ('t',), + 'a4': {}}, squashed) def test_pretty_choice_dict(self): diff --git a/novaclient/utils.py b/novaclient/utils.py index d7573758a..093bc9619 100644 --- a/novaclient/utils.py +++ b/novaclient/utils.py @@ -210,7 +210,7 @@ def _flatten(data, prefix=None): if isinstance(data, dict): for key, value in six.iteritems(data): new_key = '%s_%s' % (prefix, key) if prefix else key - if isinstance(value, (dict, list)): + if isinstance(value, (dict, list)) and value: for item in _flatten(value, new_key): yield item else: