Empty dicts and lists are now emitted as leaf defaults.
This commit is contained in:
parent
1e545cca87
commit
d9bf2966c6
2 changed files with 25 additions and 0 deletions
|
|
@ -19,9 +19,15 @@ class DictLikeHandler(BaseHandler):
|
||||||
|
|
||||||
def _walk(obj: Any, path: tuple[str, ...] = ()) -> None:
|
def _walk(obj: Any, path: tuple[str, ...] = ()) -> None:
|
||||||
if isinstance(obj, dict):
|
if isinstance(obj, dict):
|
||||||
|
if not obj:
|
||||||
|
items.append((path, obj))
|
||||||
|
return
|
||||||
for k, v in obj.items():
|
for k, v in obj.items():
|
||||||
_walk(v, path + (str(k),))
|
_walk(v, path + (str(k),))
|
||||||
elif isinstance(obj, list) and self.flatten_lists:
|
elif isinstance(obj, list) and self.flatten_lists:
|
||||||
|
if not obj:
|
||||||
|
items.append((path, obj))
|
||||||
|
return
|
||||||
for i, v in enumerate(obj):
|
for i, v in enumerate(obj):
|
||||||
_walk(v, path + (str(i),))
|
_walk(v, path + (str(i),))
|
||||||
else:
|
else:
|
||||||
|
|
|
||||||
|
|
@ -100,3 +100,22 @@ def test_generate_jinja2_template_yaml_structural_fallback():
|
||||||
# We don't care about exact formatting, just that the expected variable
|
# We don't care about exact formatting, just that the expected variable
|
||||||
# name shows up, proving we went through the structural path.
|
# name shows up, proving we went through the structural path.
|
||||||
assert "role_outer_inner" in tmpl
|
assert "role_outer_inner" in tmpl
|
||||||
|
|
||||||
|
|
||||||
|
def test_yaml_empty_collection_defaults_match_template_vars(tmp_path: Path):
|
||||||
|
yaml_path = tmp_path / "pdk.yaml"
|
||||||
|
yaml_path.write_text("ignore: []\nsettings: {}\n", encoding="utf-8")
|
||||||
|
|
||||||
|
fmt, parsed = parse_config(yaml_path)
|
||||||
|
flat_items = flatten_config(fmt, parsed)
|
||||||
|
ansible_yaml = generate_ansible_yaml("role", flat_items)
|
||||||
|
defaults = yaml.safe_load(ansible_yaml)
|
||||||
|
|
||||||
|
assert defaults["role_ignore"] == []
|
||||||
|
assert defaults["role_settings"] == {}
|
||||||
|
|
||||||
|
template = generate_jinja2_template(
|
||||||
|
fmt, parsed, "role", original_text=yaml_path.read_text(encoding="utf-8")
|
||||||
|
)
|
||||||
|
assert "{{ role_ignore }}" in template
|
||||||
|
assert "{{ role_settings }}" in template
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue