Skip to content

Commit 914eb5f

Browse files
ngxsonCISC
andauthored
jinja: fix macro with kwargs (#20960)
* jinja: fix macro with kwargs * Apply suggestions from code review Co-authored-by: Sigbjørn Skjæret <sigbjorn.skjaeret@scala.com> * fix newline problem --------- Co-authored-by: Sigbjørn Skjæret <sigbjorn.skjaeret@scala.com>
1 parent 8fc1749 commit 914eb5f

2 files changed

Lines changed: 24 additions & 4 deletions

File tree

common/jinja/runtime.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -667,17 +667,19 @@ value macro_statement::execute_impl(context & ctx) {
667667
if (is_stmt<identifier>(this->args[i])) {
668668
// normal parameter
669669
std::string param_name = cast_stmt<identifier>(this->args[i])->val;
670-
JJ_DEBUG(" Binding parameter '%s' to argument of type %s", param_name.c_str(), args.get_pos(i)->type().c_str());
671-
macro_ctx.set_val(param_name, args.get_pos(i));
670+
value param_value = args.get_kwarg_or_pos(param_name, i);
671+
JJ_DEBUG(" Binding parameter '%s' to argument of type %s", param_name.c_str(), param_value->type().c_str());
672+
macro_ctx.set_val(param_name, param_value);
672673
} else if (is_stmt<keyword_argument_expression>(this->args[i])) {
673674
// default argument used as normal parameter
674675
auto kwarg = cast_stmt<keyword_argument_expression>(this->args[i]);
675676
if (!is_stmt<identifier>(kwarg->key)) {
676677
throw std::runtime_error("Keyword argument key must be an identifier in macro '" + name + "'");
677678
}
678679
std::string param_name = cast_stmt<identifier>(kwarg->key)->val;
679-
JJ_DEBUG(" Binding parameter '%s' to argument of type %s", param_name.c_str(), args.get_pos(i)->type().c_str());
680-
macro_ctx.set_val(param_name, args.get_pos(i));
680+
value param_value = args.get_kwarg_or_pos(param_name, i);
681+
JJ_DEBUG(" Binding parameter '%s' to argument of type %s", param_name.c_str(), param_value->type().c_str());
682+
macro_ctx.set_val(param_name, param_value);
681683
} else {
682684
throw std::runtime_error("Invalid parameter type in macro '" + name + "'");
683685
}

tests/test-jinja.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -884,6 +884,24 @@ static void test_macros(testing & t) {
884884
json::object(),
885885
"Hi Guest"
886886
);
887+
888+
test_template(t, "macro kwargs input",
889+
"{% macro my_func(a, b=False) %}{% if b %}{{ a }}{% else %}nope{% endif %}{% endmacro %}{{ my_func(1, b=True) }}",
890+
json::object(),
891+
"1"
892+
);
893+
894+
test_template(t, "macro with multiple args",
895+
"{% macro add(a, b, c=0) %}{{ a + b + c }}{% endmacro %}{{ add(1, 2) }},{{ add(1, 2, 3) }},{{ add(1, b=10) }},{{ add(1, 2, c=5) }}",
896+
json::object(),
897+
"3,6,11,8"
898+
);
899+
900+
test_template(t, "macro with kwarg out-of-order input",
901+
"{% macro greet(first, last, greeting='Hello') %}{{ greeting }}, {{ first }} {{ last }}{% endmacro %}{{ greet(last='Smith', first='John') }},{{ greet(last='Doe', greeting='Hi', first='Jane') }}",
902+
json::object(),
903+
"Hello, John Smith,Hi, Jane Doe"
904+
);
887905
}
888906

889907
static void test_namespace(testing & t) {

0 commit comments

Comments
 (0)