Skip to content

Commit

Permalink
otk: error when a otk.include inside a dict override the dict
Browse files Browse the repository at this point in the history
We allow the following:
```yaml
otk.target.osbuild:
  sub:
    c: d
    otk.include: "foo.yaml"
```
The current semantic is that if the resolved value of `foo.yaml` is in
itself a dict the two are merged and if it's a non-dict value
the `sub` tree is replaced with the scalar value from `foo.yaml`.

However in the above example this can lead to unexpected results,
i.e. depending on how `foo.yaml` resolves `c: d` is removed or
kept.

This commit make it an error if an `otk.include` inside a non
empty dict returns a scalar. We *could* consider also making it
a warning but I fail to see a use-case right now (that is not
contrived) where this would not be unexpected/unwanted behavior.
  • Loading branch information
mvo5 authored and supakeen committed Jul 10, 2024
1 parent 00fea2a commit 152f3cd
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/otk/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ def resolve_dict(ctx: Context, state: State, tree: dict[str, Any]) -> Any:

included = process_include(ctx, state, pathlib.Path(val))
if not isinstance(included, dict):
if len(tree) > 0:
raise ValueError(f"otk.include '{val}' overrides non-empty dict {tree} with '{included}'")
return included

tree.update(included)
Expand Down
1 change: 1 addition & 0 deletions test/data/error/08-include-scalar-siblings.err
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
otk.include '08-include-scalar-siblings/foo.yaml' overrides non-empty dict {'c': 'd'} with 'foo'
6 changes: 6 additions & 0 deletions test/data/error/08-include-scalar-siblings.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
otk.version: 1

otk.target.osbuild:
sub:
c: d
otk.include: "08-include-scalar-siblings/foo.yaml"
1 change: 1 addition & 0 deletions test/data/error/08-include-scalar-siblings/foo.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo

0 comments on commit 152f3cd

Please sign in to comment.