Handle nested dicts in format_dict()
Some dicts have dicts in them. Handle them. Story: 2006480 Task: 36423 Change-Id: Id57fc34c676059d8b13a490bfb74d1ee1ff52891
This commit is contained in:
parent
17179a72b0
commit
6db6492a7d
|
@ -689,6 +689,50 @@ class TestFindResource(test_utils.TestCase):
|
|||
utils.format_dict({'e': 'f', 'c': 'd', 'a': 'b'}))
|
||||
self.assertIsNone(utils.format_dict(None))
|
||||
|
||||
def test_format_dict_recursive(self):
|
||||
expected = "a='b', c.1='d', c.2='e'"
|
||||
self.assertEqual(
|
||||
expected,
|
||||
utils.format_dict({'a': 'b', 'c': {'1': 'd', '2': 'e'}})
|
||||
)
|
||||
self.assertEqual(
|
||||
expected,
|
||||
utils.format_dict({'c': {'1': 'd', '2': 'e'}, 'a': 'b'})
|
||||
)
|
||||
self.assertIsNone(utils.format_dict(None))
|
||||
|
||||
expected = "a1='A', a2.b1.c1='B', a2.b1.c2='C', a2.b2='D'"
|
||||
self.assertEqual(
|
||||
expected,
|
||||
utils.format_dict(
|
||||
{
|
||||
'a1': 'A',
|
||||
'a2': {
|
||||
'b1': {
|
||||
'c1': 'B',
|
||||
'c2': 'C',
|
||||
},
|
||||
'b2': 'D',
|
||||
},
|
||||
}
|
||||
)
|
||||
)
|
||||
self.assertEqual(
|
||||
expected,
|
||||
utils.format_dict(
|
||||
{
|
||||
'a2': {
|
||||
'b1': {
|
||||
'c2': 'C',
|
||||
'c1': 'B',
|
||||
},
|
||||
'b2': 'D',
|
||||
},
|
||||
'a1': 'A',
|
||||
}
|
||||
)
|
||||
)
|
||||
|
||||
def test_format_dict_of_list(self):
|
||||
expected = "a=a1, a2; b=b1, b2; c=c1, c2; e="
|
||||
self.assertEqual(expected,
|
||||
|
|
|
@ -294,10 +294,11 @@ def find_resource(manager, name_or_id, **kwargs):
|
|||
raise exceptions.CommandError(msg % name_or_id)
|
||||
|
||||
|
||||
def format_dict(data):
|
||||
def format_dict(data, prefix=None):
|
||||
"""Return a formatted string of key value pairs
|
||||
|
||||
:param data: a dict
|
||||
:param prefix: the current parent keys in a recursive call
|
||||
:rtype: a string formatted to key='value'
|
||||
"""
|
||||
|
||||
|
@ -306,7 +307,16 @@ def format_dict(data):
|
|||
|
||||
output = ""
|
||||
for s in sorted(data):
|
||||
output = output + s + "='" + six.text_type(data[s]) + "', "
|
||||
if prefix:
|
||||
key_str = ".".join([prefix, s])
|
||||
else:
|
||||
key_str = s
|
||||
if isinstance(data[s], dict):
|
||||
# NOTE(dtroyer): Only append the separator chars here, quoting
|
||||
# is completely handled in the terminal case.
|
||||
output = output + format_dict(data[s], prefix=key_str) + ", "
|
||||
else:
|
||||
output = output + key_str + "='" + six.text_type(data[s]) + "', "
|
||||
return output[:-2]
|
||||
|
||||
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
---
|
||||
fixes:
|
||||
- Fix format_columns.format_dict() to properly re-format nested dicts.
|
||||
[Bug `2006480 <https://storyboard.openstack.org/#!/story/2006480>`_]
|
Loading…
Reference in New Issue