From 68bc329964fa7819c076d7dfcadb76f5d2cf130e Mon Sep 17 00:00:00 2001 From: Johannes Kulik Date: Mon, 20 Dec 2021 11:07:35 +0100 Subject: [PATCH] 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 --- oslo_reports/tests/test_views.py | 20 ++++++++++++++++++++ oslo_reports/views/text/generic.py | 4 ++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/oslo_reports/tests/test_views.py b/oslo_reports/tests/test_views.py index 195390c..76cbd09 100644 --- a/oslo_reports/tests/test_views.py +++ b/oslo_reports/tests/test_views.py @@ -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'] diff --git a/oslo_reports/views/text/generic.py b/oslo_reports/views/text/generic.py index 05928a7..e69f653 100644 --- a/oslo_reports/views/text/generic.py +++ b/oslo_reports/views/text/generic.py @@ -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)):