Skip to content

Commit

Permalink
Added support for sequence concatination with + (#491)
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuhiko committed Apr 10, 2024
1 parent 1b0dd3a commit 2650706
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ All notable changes to MiniJinja are documented here.
## 1.0.20

- Added support for implicit string concatenation for Jinja2 compatibility. #489
- Added support for sequence concatenation with the plus operator for Jinja2 compatibility. #491

## 1.0.19

Expand Down
5 changes: 5 additions & 0 deletions minijinja/src/value/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,11 @@ macro_rules! math_binop {
}

pub fn add(lhs: &Value, rhs: &Value) -> Result<Value, Error> {
if lhs.kind() == ValueKind::Seq && rhs.kind() == ValueKind::Seq {
return Ok(Value::from_iter(
lhs.try_iter_owned()?.chain(rhs.try_iter_owned()?),
));
}
match coerce(lhs, rhs) {
Some(CoerceResult::I128(a, b)) => a
.checked_add(b)
Expand Down
5 changes: 4 additions & 1 deletion minijinja/tests/inputs/adding.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ Strings:
{{ 'to' + name + 'you' }}!
{{ 'to' + 'you' }}!

Sequences:
{{ [1] + [2] }}

Adding:
{{ 1 + 2 }}

Minus:
{{ 2 + 1 }}
{{ 2 - 1 }}

Divide:
{{ 100 / 2 }}
8 changes: 5 additions & 3 deletions minijinja/tests/snapshots/test_templates__vm@adding.txt.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: minijinja/tests/test_templates.rs
description: "Strings:\n{{ 'to' + name + 'you' }}!\n{{ 'to' + 'you' }}!\n\nAdding:\n{{ 1 + 2 }}\n\nMinus:\n{{ 2 + 1 }}\n\nDivide:\n{{ 100 / 2 }}"
description: "Strings:\n{{ 'to' + name + 'you' }}!\n{{ 'to' + 'you' }}!\n\nSequences:\n{{ [1] + [2] }}\n\nAdding:\n{{ 1 + 2 }}\n\nMinus:\n{{ 2 - 1 }}\n\nDivide:\n{{ 100 / 2 }}"
info:
name: World
input_file: minijinja/tests/inputs/adding.txt
Expand All @@ -9,12 +9,14 @@ Strings:
toWorldyou!
toyou!

Sequences:
[1, 2]

Adding:
3

Minus:
3
1

Divide:
50.0

0 comments on commit 2650706

Please sign in to comment.