Skip to content

Commit

Permalink
Revert "Don't allow old nested dict style keys anymore (#1865)"
Browse files Browse the repository at this point in the history
This reverts commit cad734c.
  • Loading branch information
rchl committed Oct 5, 2021
1 parent cb66ed7 commit ce3b4c7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 46 deletions.
17 changes: 15 additions & 2 deletions plugin/core/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,13 @@ def update(self, d: Dict[str, Any]) -> None:
"""
Overwrite and/or add new key-value pairs to the collection.
:param d: The overriding dictionary. Keys must be in the new-style dotted format.
:param d: The overriding dictionary. Can contain nested dictionaries.
"""
for key, value in d.items():
self.set(key, value)
if isinstance(value, dict):
self._update_recursive(value, key)
else:
self.set(key, value)

def get_resolved(self, variables: Dict[str, str]) -> Dict[str, Any]:
"""
Expand All @@ -150,6 +153,16 @@ 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))

Expand Down
50 changes: 6 additions & 44 deletions tests/test_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,12 @@ 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",
"foo.bar.b": "y"
"foo": {
"bar": {
"a": "x",
"b": "y"
}
}
})
self.verify(d, "foo.bar", {"a": "x", "b": "y", "c": "c"})

Expand Down Expand Up @@ -133,45 +137,3 @@ 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"
}
}
})

0 comments on commit ce3b4c7

Please sign in to comment.