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:
Jan Lindemann 2026-07-08 12:55:05 +02:00 • committed by janware DevOps
commit ab77b24a50
Signed by: DevOps
SSH key fingerprint: SHA256:cZiw7ExG5q3KAVm7Jse3rGITowu0VjgUgNMPbifmY8g

View file

@ -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: