jinja : add count/d/e filter aliases (#24606)

This commit is contained in:
Sigbjørn Skjæret 2026-06-14 15:07:31 +02:00 committed by GitHub
parent 6e14286eda
commit acd79d603c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 18 additions and 10 deletions

View File

@ -316,12 +316,22 @@ value filter_expression::execute_impl(context & ctx) {
JJ_DEBUG("Applying filter to %s", input->type().c_str());
auto set_filter_alias = [](auto & filter_id) {
if (filter_id == "count") {
filter_id = "length";
} else if (filter_id == "d") {
filter_id = "default";
} else if (filter_id == "e") {
filter_id = "escape";
} else if (filter_id == "trim") {
filter_id = "strip";
}
};
if (is_stmt<identifier>(filter)) {
auto filter_id = cast_stmt<identifier>(filter)->val;
if (filter_id == "trim") {
filter_id = "strip"; // alias
}
set_filter_alias(filter_id);
JJ_DEBUG("Applying filter '%s' to %s", filter_id.c_str(), input->type().c_str());
// TODO: Refactor filters so this coercion can be done automatically
if (!input->is_undefined() && !is_val<value_string>(input) && (
@ -345,9 +355,7 @@ value filter_expression::execute_impl(context & ctx) {
}
auto filter_id = cast_stmt<identifier>(call->callee)->val;
if (filter_id == "trim") {
filter_id = "strip"; // alias
}
set_filter_alias(filter_id);
JJ_DEBUG("Applying filter '%s' with arguments to %s", filter_id.c_str(), input->type().c_str());
func_args args(ctx);
for (const auto & arg_expr : call->args) {

View File

@ -601,8 +601,8 @@ static void test_filters(testing & t) {
"hello jinja"
);
test_template(t, "length list",
"{{ items|length }}",
test_template(t, "length (count alias) list",
"{{ items|count }}",
{{"items", json::array({1, 2, 3})}},
"3"
);
@ -711,8 +711,8 @@ static void test_filters(testing & t) {
"fallback"
);
test_template(t, "default with falsy value",
"{{ ''|default('fallback', true) }}",
test_template(t, "default (d alias) with falsy value",
"{{ ''|d('fallback', true) }}",
json::object(),
"fallback"
);