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>
This commit is contained in:
co-authored by
Sigbjørn Skjæret
parent
8fc17493c3
commit
914eb5ff0c
@@ -667,8 +667,9 @@ value macro_statement::execute_impl(context & ctx) {
|
|||||||
if (is_stmt<identifier>(this->args[i])) {
|
if (is_stmt<identifier>(this->args[i])) {
|
||||||
// normal parameter
|
// normal parameter
|
||||||
std::string param_name = cast_stmt<identifier>(this->args[i])->val;
|
std::string param_name = cast_stmt<identifier>(this->args[i])->val;
|
||||||
JJ_DEBUG(" Binding parameter '%s' to argument of type %s", param_name.c_str(), args.get_pos(i)->type().c_str());
|
value param_value = args.get_kwarg_or_pos(param_name, i);
|
||||||
macro_ctx.set_val(param_name, args.get_pos(i));
|
JJ_DEBUG(" Binding parameter '%s' to argument of type %s", param_name.c_str(), param_value->type().c_str());
|
||||||
|
macro_ctx.set_val(param_name, param_value);
|
||||||
} else if (is_stmt<keyword_argument_expression>(this->args[i])) {
|
} else if (is_stmt<keyword_argument_expression>(this->args[i])) {
|
||||||
// default argument used as normal parameter
|
// default argument used as normal parameter
|
||||||
auto kwarg = cast_stmt<keyword_argument_expression>(this->args[i]);
|
auto kwarg = cast_stmt<keyword_argument_expression>(this->args[i]);
|
||||||
@@ -676,8 +677,9 @@ value macro_statement::execute_impl(context & ctx) {
|
|||||||
throw std::runtime_error("Keyword argument key must be an identifier in macro '" + name + "'");
|
throw std::runtime_error("Keyword argument key must be an identifier in macro '" + name + "'");
|
||||||
}
|
}
|
||||||
std::string param_name = cast_stmt<identifier>(kwarg->key)->val;
|
std::string param_name = cast_stmt<identifier>(kwarg->key)->val;
|
||||||
JJ_DEBUG(" Binding parameter '%s' to argument of type %s", param_name.c_str(), args.get_pos(i)->type().c_str());
|
value param_value = args.get_kwarg_or_pos(param_name, i);
|
||||||
macro_ctx.set_val(param_name, args.get_pos(i));
|
JJ_DEBUG(" Binding parameter '%s' to argument of type %s", param_name.c_str(), param_value->type().c_str());
|
||||||
|
macro_ctx.set_val(param_name, param_value);
|
||||||
} else {
|
} else {
|
||||||
throw std::runtime_error("Invalid parameter type in macro '" + name + "'");
|
throw std::runtime_error("Invalid parameter type in macro '" + name + "'");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -884,6 +884,24 @@ static void test_macros(testing & t) {
|
|||||||
json::object(),
|
json::object(),
|
||||||
"Hi Guest"
|
"Hi Guest"
|
||||||
);
|
);
|
||||||
|
|
||||||
|
test_template(t, "macro kwargs input",
|
||||||
|
"{% macro my_func(a, b=False) %}{% if b %}{{ a }}{% else %}nope{% endif %}{% endmacro %}{{ my_func(1, b=True) }}",
|
||||||
|
json::object(),
|
||||||
|
"1"
|
||||||
|
);
|
||||||
|
|
||||||
|
test_template(t, "macro with multiple args",
|
||||||
|
"{% 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) }}",
|
||||||
|
json::object(),
|
||||||
|
"3,6,11,8"
|
||||||
|
);
|
||||||
|
|
||||||
|
test_template(t, "macro with kwarg out-of-order input",
|
||||||
|
"{% macro greet(first, last, greeting='Hello') %}{{ greeting }}, {{ first }} {{ last }}{% endmacro %}{{ greet(last='Smith', first='John') }},{{ greet(last='Doe', greeting='Hi', first='Jane') }}",
|
||||||
|
json::object(),
|
||||||
|
"Hello, John Smith,Hi, Jane Doe"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void test_namespace(testing & t) {
|
static void test_namespace(testing & t) {
|
||||||
|
|||||||
Reference in New Issue
Block a user