diff options
author | Aleš Mrázek <ales.mrazek@nic.cz> | 2023-05-03 11:12:37 +0200 |
---|---|---|
committer | Aleš Mrázek <ales.mrazek@nic.cz> | 2023-06-09 13:54:07 +0200 |
commit | 2a35724a7ffb8727851a443e338624c11bd7315a (patch) | |
tree | 362d6853cd8d8e112e93f3a4e5b2eb66cfe35e98 | |
parent | manager: datamodel: ListOrItem custom generic type (diff) | |
download | knot-resolver-2a35724a7ffb8727851a443e338624c11bd7315a.tar.xz knot-resolver-2a35724a7ffb8727851a443e338624c11bd7315a.zip |
manager: datamodel: templates: macro for boolean values
-rw-r--r-- | manager/knot_resolver_manager/datamodel/templates/macros/common_macros.lua.j2 | 8 | ||||
-rw-r--r-- | manager/tests/unit/datamodel/templates/test_common_macros.py | 18 |
2 files changed, 26 insertions, 0 deletions
diff --git a/manager/knot_resolver_manager/datamodel/templates/macros/common_macros.lua.j2 b/manager/knot_resolver_manager/datamodel/templates/macros/common_macros.lua.j2 index d576e6ba..4c2ba11a 100644 --- a/manager/knot_resolver_manager/datamodel/templates/macros/common_macros.lua.j2 +++ b/manager/knot_resolver_manager/datamodel/templates/macros/common_macros.lua.j2 @@ -2,6 +2,14 @@ '{{ string }}' {%- endmacro %} +{% macro boolean(val, negation=false) -%} +{%- if negation -%} +{{ 'false' if val else 'true' }} +{%- else-%} +{{ 'true' if val else 'false' }} +{%- endif -%} +{%- endmacro %} + {# Return string or table of strings #} {% macro string_table(table) -%} {%- if table is string -%} diff --git a/manager/tests/unit/datamodel/templates/test_common_macros.py b/manager/tests/unit/datamodel/templates/test_common_macros.py index 0dee2881..611cb14d 100644 --- a/manager/tests/unit/datamodel/templates/test_common_macros.py +++ b/manager/tests/unit/datamodel/templates/test_common_macros.py @@ -2,6 +2,24 @@ from knot_resolver_manager.datamodel.config_schema import template_from_str from knot_resolver_manager.datamodel.forward_schema import ForwardServerSchema +def test_boolean(): + tmpl_str = """{% from 'macros/common_macros.lua.j2' import boolean %} +{{ boolean(x) }}""" + + tmpl = template_from_str(tmpl_str) + assert tmpl.render(x=True) == "true" + assert tmpl.render(x=False) == "false" + + +def test_boolean_neg(): + tmpl_str = """{% from 'macros/common_macros.lua.j2' import boolean %} +{{ boolean(x,true) }}""" + + tmpl = template_from_str(tmpl_str) + assert tmpl.render(x=True) == "false" + assert tmpl.render(x=False) == "true" + + def test_string_table(): s = "any string" t = [s, "other string"] |