Skip to content

Commit

Permalink
directive: customization takes if-set
Browse files Browse the repository at this point in the history
Renamed `defined` to `if-set`.
  • Loading branch information
supakeen committed Apr 19, 2024
1 parent b2626a0 commit c3c6caf
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
10 changes: 6 additions & 4 deletions doc/directives.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,17 +169,19 @@ otk.meta.kiwi:
Customizations are conditional blocks that receive separate input through
`otk compile -Cname=data`, a customization is considered to be active when it
is passed data. If a customization is passed multiple times then the `defined`
block is replaced multiple times, once for each data input.
block is repeated multiple times, once for each input.

Expects a `map` for its value which contains a `defined` key. The `default` key
is optional. The values of `default` and `defined` can be of any type.
Expects a `map` for its value which contains an `if-set` key. The `default` key
is optional. If a `default` key is not passed and the customization is inactive
then the customization block is effectively a no-op and will be removed from the
tree. The values of `default` and `if-set` can be of any type.

```yaml
otk.customization.name:
default:
- type: org.osbuild.stage
options: none
defined:
if-set:
- type: org.osbuild.stage
options: ${this.data} # it's not going to be `this`
- type: org.osbuild.stage
Expand Down
7 changes: 4 additions & 3 deletions src/otk/transform/directive.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,14 @@ def op_map_merge(ctx: Context, tree: dict[str, Any]) -> Any:


@tree.must_be(dict)
@tree.must_pass(tree.has_keys(["default", "scope", "defined"]))
@tree.must_pass(tree.has_keys(["scope", "if-set"]))
def customization(ctx: Context, tree: dict[str, Any], key) -> Any:
"""Apply a customization."""
log.warning("applying customization %r", key)

# TODO take in customizations somewhere and use there here
return tree["default"]
# TODO take in customizations somewhere and use them here, this is
# TODO currently just a placeholder.
return tree.get("default")


@tree.must_be(str)
Expand Down
6 changes: 3 additions & 3 deletions test/test_sugar.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def test_simple_sugar():

omnifest_result = resolve(context, omnifest_under_test)

assert omnifest_result['my_var'] == "foo"
assert omnifest_result["my_var"] == "foo"


def test_simple_sugar_tree():
Expand All @@ -39,7 +39,7 @@ def test_simple_sugar_tree():

omnifest_result = resolve(context, omnifest_under_test)

assert omnifest_result['my_var'] == [1, 2]
assert omnifest_result["my_var"] == [1, 2]


def test_simple_sugar_tree_fail():
Expand All @@ -52,7 +52,7 @@ def test_simple_sugar_tree_fail():
my_var: my_prefix_${variable}
"""
expected_error = 'string sugar resolves to an incorrect type, expected int, float, or str but got %r'
expected_error = "string sugar resolves to an incorrect type, expected int, float, or str but got %r"

context = Context()
omnifest_under_test = Omnifest.from_yaml_bytes(data).to_tree()
Expand Down

0 comments on commit c3c6caf

Please sign in to comment.