diff --git a/plugin/core/collections.py b/plugin/core/collections.py index 881b6498d..98a57b56d 100644 --- a/plugin/core/collections.py +++ b/plugin/core/collections.py @@ -135,13 +135,10 @@ def update(self, d: Dict[str, Any]) -> None: """ Overwrite and/or add new key-value pairs to the collection. - :param d: The overriding dictionary. Can contain nested dictionaries. + :param d: The overriding dictionary. Keys must be in the new-style dotted format. """ for key, value in d.items(): - if isinstance(value, dict): - self._update_recursive(value, key) - else: - self.set(key, value) + self.set(key, value) def get_resolved(self, variables: Dict[str, str]) -> Dict[str, Any]: """ @@ -153,16 +150,6 @@ def get_resolved(self, variables: Dict[str, str]) -> Dict[str, Any]: """ return sublime.expand_variables(self._d, variables) - def _update_recursive(self, current: Dict[str, Any], prefix: str) -> None: - if not current: - return self.set(prefix, current) - for key, value in current.items(): - path = "{}.{}".format(prefix, key) - if isinstance(value, dict): - self._update_recursive(value, path) - else: - self.set(path, value) - def __repr__(self) -> str: return "{}({})".format(self.__class__.__name__, repr(self._d)) diff --git a/tests/test_collections.py b/tests/test_collections.py index 3750d7a5c..e0d4f4ecc 100644 --- a/tests/test_collections.py +++ b/tests/test_collections.py @@ -74,12 +74,8 @@ def test_update(self) -> None: d.set("foo.bar.c", "c") self.verify(d, "foo.bar", {"a": "a", "b": "b", "c": "c"}) d.update({ - "foo": { - "bar": { - "a": "x", - "b": "y" - } - } + "foo.bar.a": "x", + "foo.bar.b": "y" }) self.verify(d, "foo.bar", {"a": "x", "b": "y", "c": "c"}) @@ -137,3 +133,45 @@ def test_update_empty_dict(self) -> None: self.assertEqual(d.get(), {"a": {}}) d.update({"a": {"b": {}}}) self.assertEqual(d.get(), {"a": {"b": {}}}) + + def test_from_base_and_override(self) -> None: + base = DottedDict({ + "yaml.schemas": {} + }) + override = { + "yaml.schemas": { + "http://foo.com/bar.json": "**/*.json" + } + } + result = DottedDict.from_base_and_override(base, override) + self.assertEqual( + result.get(None), + { + "yaml": { + "schemas": { + "http://foo.com/bar.json": "**/*.json" + } + } + } + ) + + def test_update_with_dicts(self) -> None: + base = { + "settings": { + "yaml.schemas": {} + } + } + overrides = { + "yaml.schemas": { + "http://foo.com/bar.json": "**/*.json" + } + } + settings = DottedDict(base.get("settings", {})) + settings.update(overrides) + self.assertEqual(settings.get(), { + "yaml": { + "schemas": { + "http://foo.com/bar.json": "**/*.json" + } + } + })