summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAleš Mrázek <ales.mrazek@nic.cz>2023-05-03 11:12:37 +0200
committerAleš Mrázek <ales.mrazek@nic.cz>2023-06-09 13:54:07 +0200
commit2a35724a7ffb8727851a443e338624c11bd7315a (patch)
tree362d6853cd8d8e112e93f3a4e5b2eb66cfe35e98
parentmanager: datamodel: ListOrItem custom generic type (diff)
downloadknot-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.j28
-rw-r--r--manager/tests/unit/datamodel/templates/test_common_macros.py18
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"]