Support integer keys of dicts in text serialization

When the config contains a DictOpt that accepts integer keys, the Guru
Meditation Report fails to serialize. Even if current OpenStack code
might not contain such dicts, it's possible that downstream add such
options. Therefore, we change the code to support both dicts with only
integer keys and - just in case - mixed-key dicts.

Change-Id: I44343a8c306c96fc8dc078a76e744cf8b897d8d8
This commit is contained in:
Johannes Kulik 2021-12-20 11:07:35 +01:00
parent 6da7341050
commit 68bc329964
2 changed files with 22 additions and 2 deletions

View File

@ -316,6 +316,26 @@ class TestGenericTextViews(base.BaseTestCase):
'string = value')
self.assertEqual(target_str, str(self.model))
def test_dict_serialization_integer_keys(self):
self.model['dt'] = {3: 4, 5: 6}
target_str = ('dt = \n'
' 3 = 4\n'
' 5 = 6\n'
'int = 1\n'
'string = value')
self.assertEqual(target_str, str(self.model))
def test_dict_serialization_mixed_keys(self):
self.model['dt'] = {'3': 4, 5: 6}
target_str = ('dt = \n'
' 3 = 4\n'
' 5 = 6\n'
'int = 1\n'
'string = value')
self.assertEqual(target_str, str(self.model))
def test_list_serialization(self):
self.model['lt'] = ['a', 'b']

View File

@ -111,7 +111,7 @@ class KeyValueView(object):
def serialize(root, rootkey, indent):
res = []
if rootkey is not None:
res.append((self.indent_str * indent) + rootkey)
res.append((self.indent_str * indent) + str(rootkey))
if isinstance(root, abc.Mapping):
if rootkey is None and indent > 0:
@ -121,7 +121,7 @@ class KeyValueView(object):
if self.before_dict is not None:
res.insert(0, self.before_dict)
for key in sorted(root):
for key in sorted(root, key=str):
res.extend(serialize(root[key], key, indent + 1))
elif (isinstance(root, abc.Sequence) and
not isinstance(root, str)):