Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't allow old nested dict style keys anymore #1865

Merged
merged 3 commits into from
Oct 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 2 additions & 15 deletions plugin/core/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
"""
Expand All @@ -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))

Expand Down
50 changes: 44 additions & 6 deletions tests/test_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"})

Expand Down Expand Up @@ -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"
}
}
})