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
1 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.
|
||||
|
|
|
|||
Loading…
Reference in a new issue