diff --git a/osc_lib/cli/format_columns.py b/osc_lib/cli/format_columns.py index ef2bcc5..7968592 100644 --- a/osc_lib/cli/format_columns.py +++ b/osc_lib/cli/format_columns.py @@ -26,6 +26,9 @@ class DictColumn(columns.FormattableColumn): def human_readable(self): return utils.format_dict(self._value) + def machine_readable(self): + return dict(self._value or {}) + class DictListColumn(columns.FormattableColumn): """Format column for dict, key is string, value is list""" @@ -33,6 +36,9 @@ class DictListColumn(columns.FormattableColumn): def human_readable(self): return utils.format_dict_of_list(self._value) + def machine_readable(self): + return dict(self._value or {}) + class ListColumn(columns.FormattableColumn): """Format column for list content""" diff --git a/osc_lib/tests/cli/test_format_columns.py b/osc_lib/tests/cli/test_format_columns.py index df44500..9df47dc 100644 --- a/osc_lib/tests/cli/test_format_columns.py +++ b/osc_lib/tests/cli/test_format_columns.py @@ -13,6 +13,8 @@ # under the License. # +import collections + from osc_lib.cli import format_columns from osc_lib.tests import utils @@ -28,6 +30,13 @@ class TestDictColumn(utils.TestCase): self.assertEqual(dict_content, col.machine_readable()) self.assertEqual("key1='value1', key2='value2'", col.human_readable()) + def test_complex_object(self): + """Non-primitive objects should be converted to a dict.""" + dict_content = collections.OrderedDict( + [('key1', 'value1'), ('key2', 'value2')]) + col = format_columns.DictColumn(dict_content) + self.assertIsInstance(col._value, dict) + class TestDictListColumn(utils.TestCase): @@ -40,6 +49,13 @@ class TestDictListColumn(utils.TestCase): 'public=172.24.4.6, 2001:db8::8', col.human_readable()) + def test_complex_object(self): + """Non-primitive objects should be converted to a dict.""" + dict_content = collections.OrderedDict( + [('key1', ['value1']), ('key2', ['value2'])]) + col = format_columns.DictListColumn(dict_content) + self.assertIsInstance(col._value, dict) + class TestListColumn(utils.TestCase):