Merge "Support integer keys of dicts in text serialization"

This commit is contained in:
Zuul 2022-03-22 13:43:21 +00:00 committed by Gerrit Code Review
commit 3a1157f1e5
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)):