mirror of
https://github.com/ggml-org/llama.cpp.git
synced 2026-09-14 18:02:52 +02:00
jinja: treat a null left operand of in as a plain lookup (#28620)
Templates that default an optional variable to none and then test its membership in a map hit an error, while the same expression is a normal lookup returning false in Jinja. The undefined counterpart of this case was already handled just above.
This commit is contained in:
@@ -167,6 +167,12 @@ value binary_expression::execute_impl(context & ctx) {
|
||||
}
|
||||
throw std::runtime_error("Cannot perform operation " + op.value + " on undefined values");
|
||||
} else if (is_val<value_none>(left_val) || is_val<value_none>(right_val)) {
|
||||
if (!is_val<value_none>(right_val) && (op.value == "in" || op.value == "not in")) {
|
||||
// case: none in {'low': 1}
|
||||
// A null left operand is looked up like any other value.
|
||||
bool member = test_is_in();
|
||||
return mk_val<value_bool>(op.value == "in" ? member : !member);
|
||||
}
|
||||
if (op.value == "+" || op.value == "~") {
|
||||
value res = mk_val<value_undefined>();
|
||||
if (workaround_concat_null_with_str(res)) {
|
||||
|
||||
@@ -374,6 +374,24 @@ static void test_expressions(testing & t) {
|
||||
"42"
|
||||
);
|
||||
|
||||
test_template(t, "none in object",
|
||||
"{{ x in {'low': 1, 'high': 2} }}",
|
||||
{{"x", nullptr}},
|
||||
"False"
|
||||
);
|
||||
|
||||
test_template(t, "none not in object",
|
||||
"{{ x not in {'low': 1, 'high': 2} }}",
|
||||
{{"x", nullptr}},
|
||||
"True"
|
||||
);
|
||||
|
||||
test_template(t, "none in array",
|
||||
"{{ x in [1, none, 3] }}",
|
||||
{{"x", nullptr}},
|
||||
"True"
|
||||
);
|
||||
|
||||
test_template(t, "dot notation",
|
||||
"{{ user.name }}",
|
||||
{{"user", {{"name", "Bob"}}}},
|
||||
|
||||
Reference in New Issue
Block a user