jinja : add count/d/e filter aliases (#24606)
This commit is contained in:
@@ -316,12 +316,22 @@ value filter_expression::execute_impl(context & ctx) {
|
|||||||
|
|
||||||
JJ_DEBUG("Applying filter to %s", input->type().c_str());
|
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)) {
|
if (is_stmt<identifier>(filter)) {
|
||||||
auto filter_id = cast_stmt<identifier>(filter)->val;
|
auto filter_id = cast_stmt<identifier>(filter)->val;
|
||||||
|
|
||||||
if (filter_id == "trim") {
|
set_filter_alias(filter_id);
|
||||||
filter_id = "strip"; // alias
|
|
||||||
}
|
|
||||||
JJ_DEBUG("Applying filter '%s' to %s", filter_id.c_str(), input->type().c_str());
|
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
|
// TODO: Refactor filters so this coercion can be done automatically
|
||||||
if (!input->is_undefined() && !is_val<value_string>(input) && (
|
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;
|
auto filter_id = cast_stmt<identifier>(call->callee)->val;
|
||||||
|
|
||||||
if (filter_id == "trim") {
|
set_filter_alias(filter_id);
|
||||||
filter_id = "strip"; // alias
|
|
||||||
}
|
|
||||||
JJ_DEBUG("Applying filter '%s' with arguments to %s", filter_id.c_str(), input->type().c_str());
|
JJ_DEBUG("Applying filter '%s' with arguments to %s", filter_id.c_str(), input->type().c_str());
|
||||||
func_args args(ctx);
|
func_args args(ctx);
|
||||||
for (const auto & arg_expr : call->args) {
|
for (const auto & arg_expr : call->args) {
|
||||||
|
|||||||
@@ -601,8 +601,8 @@ static void test_filters(testing & t) {
|
|||||||
"hello jinja"
|
"hello jinja"
|
||||||
);
|
);
|
||||||
|
|
||||||
test_template(t, "length list",
|
test_template(t, "length (count alias) list",
|
||||||
"{{ items|length }}",
|
"{{ items|count }}",
|
||||||
{{"items", json::array({1, 2, 3})}},
|
{{"items", json::array({1, 2, 3})}},
|
||||||
"3"
|
"3"
|
||||||
);
|
);
|
||||||
@@ -711,8 +711,8 @@ static void test_filters(testing & t) {
|
|||||||
"fallback"
|
"fallback"
|
||||||
);
|
);
|
||||||
|
|
||||||
test_template(t, "default with falsy value",
|
test_template(t, "default (d alias) with falsy value",
|
||||||
"{{ ''|default('fallback', true) }}",
|
"{{ ''|d('fallback', true) }}",
|
||||||
json::object(),
|
json::object(),
|
||||||
"fallback"
|
"fallback"
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user