diff --git a/pyhocon/config_tree.py b/pyhocon/config_tree.py index 0fab56ef..568cd0a1 100644 --- a/pyhocon/config_tree.py +++ b/pyhocon/config_tree.py @@ -317,6 +317,19 @@ def get_list(self, key, default=UndefinedKey): value = self.get(key, default) if isinstance(value, list): return value + elif isinstance(value, ConfigTree): + try: + lst = [] + sorted_tree = sorted(value.items()) + for k, v in sorted_tree: + if re.match('^[1-9][0-9]*$|0', k): + lst.append(v) + else: + raise ValueError + return lst + except ValueError: + raise ConfigException( + u"{key} does not translate to a list".format(key=key)) elif value is None: return None else: diff --git a/tests/test_config_tree.py b/tests/test_config_tree.py index e2f3901f..fbdee7b6 100644 --- a/tests/test_config_tree.py +++ b/tests/test_config_tree.py @@ -33,6 +33,17 @@ def test_config_list(self): config_tree.put("a.b.c", [8, 9], True) assert config_tree.get("a.b.c") == [6, 7, 8, 9] + def test_numerically_index_objects_to_arrays(self): + config_tree = ConfigTree() + config_tree.put("list.2", "b") + config_tree.put("list.0", "a") + assert config_tree.get_list("list") == ["a", "b"] + + config_tree.put("invalid-list.a", "c") + config_tree.put("invalid-list.b", "d") + with pytest.raises(ConfigException): + config_tree.get_list("invalid-list") + def test_config_tree_number(self): config_tree = ConfigTree() config_tree.put("a.b.c", 5)