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
This commit is contained in:
Hironori Shiina 2016-08-05 02:08:36 +09:00
parent 22beb3b4ea
commit fae24eee4a
2 changed files with 5 additions and 3 deletions

View File

@ -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):

View File

@ -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: