load_yaml: Add jw.inplace-link with sibling merge
jw.inplace-link behaves like jw.link but can have sibling keys that
are deep-merged into the resolved target dict (siblings override target
on conflict).
Usage:
entry:
jw.inplace-link: path.to.target
sibling_key: value
Expands to:
entry:
<merged result of path.to.target + sibling_key: value>
This solves the common use case of pulling in a dict from elsewhere
while overriding specific keys inline.
Added 12 unit tests covering: no siblings, dict siblings, scalar
siblings, non-dict targets, relative paths, cycle detection, chains,
list siblings, list items, deep nesting, sibling override, and
integration with jw.include.
Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
parent
57a75399a2
commit
ab77b24a50
2 changed files with 207 additions and 9 deletions
|
|
@ -162,14 +162,47 @@ def _expand_links_recursive(
|
|||
resolved: dict,
|
||||
resolving: set,
|
||||
) -> Any:
|
||||
"""Recursively walk *node* and expand all ``jw.link`` markers.
|
||||
"""Recursively walk *node* and expand all link markers.
|
||||
|
||||
``resolved`` is a memoisation cache mapping ``(path, link_spec)`` →
|
||||
expanded value. ``resolving`` tracks links currently being expanded
|
||||
to detect cycles.
|
||||
"""
|
||||
if isinstance(node, dict):
|
||||
# Link node: dict with only jw.link as key
|
||||
# --- jw.inplace-link: can have siblings, merges into resolved target ---
|
||||
if 'jw.inplace-link' in node:
|
||||
link_spec = node['jw.inplace-link']
|
||||
link_key = (current_path, link_spec)
|
||||
|
||||
if link_key in resolving:
|
||||
raise ValueError(f"Cyclic link detected: {link_spec!r}")
|
||||
if link_key in resolved:
|
||||
base_value: dict | Any = resolved[link_key]
|
||||
else:
|
||||
resolving.add(link_key)
|
||||
_, _, path = _parse_link(link_spec)
|
||||
_, target_value = _resolve_path(path, current_path, root)
|
||||
base_value = _expand_links_recursive(
|
||||
target_value, root, current_path, resolved, resolving
|
||||
)
|
||||
resolved[link_key] = base_value
|
||||
resolving.discard(link_key)
|
||||
|
||||
# Expand sibling keys
|
||||
expanded_remaining: dict[str, Any] = {}
|
||||
for key, value in node.items():
|
||||
if key == 'jw.inplace-link':
|
||||
continue
|
||||
expanded_remaining[key] = _expand_links_recursive(
|
||||
value, root, current_path + (key, ), resolved, resolving
|
||||
)
|
||||
|
||||
# Deep-merge siblings into resolved dict (siblings win on conflict)
|
||||
if isinstance(base_value, dict) and expanded_remaining:
|
||||
return _deep_merge(base_value, expanded_remaining)
|
||||
return base_value
|
||||
|
||||
# --- jw.link (sole key): inplace or named replacement ---
|
||||
if 'jw.link' in node and len(node) == 1:
|
||||
link_spec = node['jw.link']
|
||||
link_key = (current_path, link_spec)
|
||||
|
|
@ -195,7 +228,7 @@ def _expand_links_recursive(
|
|||
resolved[link_key] = result
|
||||
return result
|
||||
|
||||
# Normal dict — expand values recursively
|
||||
# --- Normal dict — expand values recursively ---
|
||||
expanded: dict[str, Any] = {}
|
||||
for key, value in node.items():
|
||||
expanded[key] = _expand_links_recursive(
|
||||
|
|
@ -213,19 +246,22 @@ def _expand_links_recursive(
|
|||
return node # scalar or None
|
||||
|
||||
def _expand_links(root: dict[str, Any]) -> dict[str, Any]:
|
||||
r"""Expand all ``jw.link`` markers in *root* and return the result.
|
||||
r"""Expand all link markers in *root* and return the result.
|
||||
|
||||
Two link forms are supported:
|
||||
Three link forms are supported:
|
||||
|
||||
- **Inplace** ``jw.link: "target.path"`` — the ``jw.link`` key
|
||||
- **Inplace** ``jw.link: "target.path"`` — the ``jw.link`` key
|
||||
disappears and is replaced by the resolved value, inline.
|
||||
- **Named** ``jw.link: "name|target.path"`` — the ``jw.link`` key
|
||||
- **Named** ``jw.link: "name|target.path"`` — the ``jw.link`` key
|
||||
is renamed to ``name`` and its value is the resolved target.
|
||||
- **Inplace** ``jw.inplace-link: "target.path"`` — the ``jw.inplace-link``
|
||||
key disappears and its value is deep-merged into the resolved target
|
||||
dict. Sibling keys (if any) override the target on conflict.
|
||||
|
||||
Paths use dot-separated segments. A relative path starts with ``.``
|
||||
(Python-import style): ``.`` stays at the current level, ``..`` goes
|
||||
up one level, ``...`` two, and so on. The first ``/`` in the link
|
||||
value (only when the prefix matches ``[a-zA-Z0-9_-]+/``) separates
|
||||
up one level, ``...`` two, and so on. The first ``|`` in the link
|
||||
value (only when the prefix matches ``[a-zA-Z0-9_-]+|``) separates
|
||||
the link name from the target path for named links.
|
||||
|
||||
Cyclic links raise ``ValueError``; missing targets also raise.
|
||||
|
|
|
|||
|
|
@ -625,6 +625,168 @@ class TestEdgeCases:
|
|||
data = load_yaml(yaml_file('target: ok\nlink:\n jw.link: 123target|target\n'))
|
||||
assert data['link'] == {'123target': 'ok'}
|
||||
|
||||
# -- jw.inplace-link expansion ------------------------------------------------
|
||||
|
||||
class TestInplaceLink:
|
||||
"""Tests for jw.inplace-link expansion with sibling merging."""
|
||||
|
||||
def test_inplace_link_no_siblings(self, yaml_file: Callable) -> None:
|
||||
"""jw.inplace-link alone replaces the value (like jw.link)."""
|
||||
data = load_yaml(yaml_file('target: val\nlink:\n jw.inplace-link: target\n'))
|
||||
assert data['link'] == 'val'
|
||||
|
||||
def test_inplace_link_with_dict_siblings(self, yaml_file: Callable) -> None:
|
||||
"""jw.inplace-link with sibling keys deep-merges into resolved dict."""
|
||||
data = load_yaml(
|
||||
yaml_file(
|
||||
'target:\n'
|
||||
' a: 1\n'
|
||||
' b: 2\n'
|
||||
'entry:\n'
|
||||
' jw.inplace-link: target\n'
|
||||
' c: 3\n'
|
||||
' b: overridden\n'
|
||||
)
|
||||
)
|
||||
# a and b from target, c from siblings, b overridden by sibling
|
||||
assert data['entry']['a'] == 1
|
||||
assert data['entry']['b'] == 'overridden'
|
||||
assert data['entry']['c'] == 3
|
||||
|
||||
def test_inplace_link_with_scalar_sibling(self, yaml_file: Callable) -> None:
|
||||
"""Scalar siblings merge into resolved dict."""
|
||||
data = load_yaml(
|
||||
yaml_file(
|
||||
'target:\n'
|
||||
' nested: dict\n'
|
||||
'entry:\n'
|
||||
' jw.inplace-link: target\n'
|
||||
' flag: true\n'
|
||||
)
|
||||
)
|
||||
assert data['entry']['nested'] == 'dict'
|
||||
assert data['entry']['flag'] is True
|
||||
|
||||
def test_inplace_link_non_dict_target(self, yaml_file: Callable) -> None:
|
||||
"""Non-dict target replaces entirely; siblings dropped."""
|
||||
data = load_yaml(yaml_file('target: val\nentry:\n jw.inplace-link: target\n'))
|
||||
assert data['entry'] == 'val'
|
||||
|
||||
def test_inplace_link_relative_path(self, yaml_file: Callable) -> None:
|
||||
"""jw.inplace-link supports relative paths."""
|
||||
data = load_yaml(
|
||||
yaml_file(
|
||||
'sibling:\n'
|
||||
' x: resolved\n'
|
||||
'root:\n'
|
||||
' entry:\n'
|
||||
' jw.inplace-link: ..sibling\n'
|
||||
' y: extra\n'
|
||||
)
|
||||
)
|
||||
assert data['root']['entry']['x'] == 'resolved'
|
||||
assert data['root']['entry']['y'] == 'extra'
|
||||
|
||||
def test_inplace_link_cyclic(self, yaml_file: Callable) -> None:
|
||||
"""jw.inplace-link detects cycles."""
|
||||
with pytest.raises(ValueError, match = 'Cyclic link'):
|
||||
load_yaml(yaml_file('a:\n jw.inplace-link: b\nb:\n jw.inplace-link: a\n'))
|
||||
|
||||
def test_inplace_link_chain(self, yaml_file: Callable) -> None:
|
||||
"""jw.inplace-link resolves through transitive chains."""
|
||||
data = load_yaml(
|
||||
yaml_file(
|
||||
'target: final\n'
|
||||
'a:\n'
|
||||
' jw.inplace-link: b\n'
|
||||
'b:\n'
|
||||
' jw.inplace-link: target\n'
|
||||
)
|
||||
)
|
||||
assert data['a'] == 'final'
|
||||
|
||||
def test_inplace_link_with_list_sibling(self, yaml_file: Callable) -> None:
|
||||
"""List sibling values merge into resolved dict."""
|
||||
data = load_yaml(
|
||||
yaml_file(
|
||||
'target:\n'
|
||||
' key: val\n'
|
||||
'entry:\n'
|
||||
' jw.inplace-link: target\n'
|
||||
' items:\n'
|
||||
' - a\n'
|
||||
' - b\n'
|
||||
)
|
||||
)
|
||||
assert data['entry']['key'] == 'val'
|
||||
assert data['entry']['items'] == ['a', 'b']
|
||||
|
||||
def test_inplace_link_in_list(self, yaml_file: Callable) -> None:
|
||||
"""jw.inplace-link works inside list items."""
|
||||
data = load_yaml(
|
||||
yaml_file(
|
||||
'target:\n'
|
||||
' x: 1\n'
|
||||
'items:\n'
|
||||
' - jw.inplace-link: target\n'
|
||||
' y: 2\n'
|
||||
' - plain\n'
|
||||
)
|
||||
)
|
||||
assert data['items'][0] == {'x': 1, 'y': 2}
|
||||
assert data['items'][1] == 'plain'
|
||||
|
||||
def test_inplace_link_deep_nesting(self, yaml_file: Callable) -> None:
|
||||
"""jw.inplace-link merges deeply nested dicts."""
|
||||
data = load_yaml(
|
||||
yaml_file(
|
||||
'target:\n'
|
||||
' a:\n'
|
||||
' b:\n'
|
||||
' c: original\n'
|
||||
'entry:\n'
|
||||
' jw.inplace-link: target\n'
|
||||
' a:\n'
|
||||
' b:\n'
|
||||
' d: added\n'
|
||||
' e: new\n'
|
||||
)
|
||||
)
|
||||
assert data['entry']['a']['b']['c'] == 'original'
|
||||
assert data['entry']['a']['b']['d'] == 'added'
|
||||
assert data['entry']['a']['e'] == 'new'
|
||||
|
||||
def test_inplace_link_override_deep(self, yaml_file: Callable) -> None:
|
||||
"""Sibling values override target values in deep merge."""
|
||||
data = load_yaml(
|
||||
yaml_file(
|
||||
'target:\n'
|
||||
' a:\n'
|
||||
' b: original\n'
|
||||
'entry:\n'
|
||||
' jw.inplace-link: target\n'
|
||||
' a:\n'
|
||||
' b: overridden\n'
|
||||
)
|
||||
)
|
||||
assert data['entry']['a']['b'] == 'overridden'
|
||||
|
||||
def test_inplace_link_with_include(self, temp_dir: Callable) -> None:
|
||||
"""jw.inplace-link works with included files."""
|
||||
base = temp_dir(
|
||||
[
|
||||
('base.yaml', 'shared:\n from: include\n'),
|
||||
(
|
||||
'main.yaml',
|
||||
'jw.include: base.yaml\nentry:\n'
|
||||
' jw.inplace-link: shared\n local: here\n'
|
||||
),
|
||||
]
|
||||
)
|
||||
data = load_yaml(pathlib.Path(base) / 'main.yaml')
|
||||
assert data['entry']['from'] == 'include'
|
||||
assert data['entry']['local'] == 'here'
|
||||
|
||||
# -- jw.link expansion - Path resolution --------------------------------------
|
||||
|
||||
class TestPathResolution:
|
||||
|
|
|
|||
Loading…
Reference in a new issue