Skip to content

Commit e138e30

Browse files
committed
enhance addition operator to concat lists, dicts
1 parent 03322fd commit e138e30

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
### Version 2.0.1 ([Maven Central](http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22com.hubspot.jinjava%22%20AND%20v%3A%222.0.1%22)) ###
44

55
* Adding self-documenting feature to jinjava core
6+
* Updating addition expression operator ('+') to work with lists and dicts
67

78
### Version 2.0.0 ([Maven Central](http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22com.hubspot.jinjava%22%20AND%20v%3A%222.0.0%22)) ###
89

src/main/java/com/hubspot/jinjava/el/ext/AdditionOperator.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package com.hubspot.jinjava.el.ext;
22

3+
import java.util.ArrayList;
4+
import java.util.Collection;
5+
import java.util.HashMap;
6+
import java.util.List;
7+
import java.util.Map;
38
import java.util.Objects;
49

510
import de.odysseus.el.misc.NumberOperations;
@@ -8,8 +13,28 @@
813

914
public class AdditionOperator extends AstBinary.SimpleOperator {
1015

16+
@SuppressWarnings("unchecked")
1117
@Override
1218
protected Object apply(TypeConverter converter, Object o1, Object o2) {
19+
if(o1 instanceof Collection) {
20+
List<Object> result = new ArrayList<>((Collection<Object>) o1);
21+
22+
if(o2 instanceof Collection) {
23+
result.addAll((Collection<Object>) o2);
24+
}
25+
else {
26+
result.add(o2);
27+
}
28+
29+
return result;
30+
}
31+
else if(o1 instanceof Map && o2 instanceof Map) {
32+
Map<Object, Object> result = new HashMap<>((Map<Object, Object>) o1);
33+
result.putAll((Map<Object, Object>) o2);
34+
35+
return result;
36+
}
37+
1338
if(o1 instanceof String || o2 instanceof String) {
1439
return Objects.toString(o1).concat(Objects.toString(o2));
1540
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.hubspot.jinjava.el.ext;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.assertj.core.api.Assertions.entry;
5+
6+
import java.util.Collection;
7+
import java.util.Map;
8+
9+
import org.junit.Before;
10+
import org.junit.Test;
11+
12+
import com.hubspot.jinjava.Jinjava;
13+
import com.hubspot.jinjava.interpret.Context;
14+
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
15+
16+
17+
@SuppressWarnings("unchecked")
18+
public class AdditionOperatorTest {
19+
20+
21+
private JinjavaInterpreter interpreter;
22+
private Context context;
23+
24+
@Before
25+
public void setup() {
26+
Jinjava jinjava = new Jinjava();
27+
context = new Context();
28+
interpreter = new JinjavaInterpreter(jinjava, context, jinjava.getGlobalConfig());
29+
}
30+
31+
@Test
32+
public void itConcatsStrings() {
33+
assertThat(interpreter.resolveELExpression("'foo' + 'bar'", -1)).isEqualTo("foobar");
34+
}
35+
36+
@Test
37+
public void itAddsNumbers() {
38+
assertThat(interpreter.resolveELExpression("1 + 2", -1)).isEqualTo(3L);
39+
}
40+
41+
@Test
42+
public void itConcatsNumberWithString() {
43+
assertThat(interpreter.resolveELExpression("'1' + 2", -1)).isEqualTo("12");
44+
assertThat(interpreter.resolveELExpression("1 + '2'", -1)).isEqualTo("12");
45+
}
46+
47+
@Test
48+
public void itCombinesTwoLists() {
49+
assertThat((Collection<Object>) interpreter.resolveELExpression("['foo', 'bar'] + ['other', 'one']", -1))
50+
.containsExactly("foo", "bar", "other", "one");
51+
}
52+
53+
@Test
54+
public void itAddsToList() {
55+
assertThat((Collection<Object>) interpreter.resolveELExpression("['foo'] + 'bar'", -1)).containsExactly("foo", "bar");
56+
}
57+
58+
@Test
59+
public void itCombinesTwoDicts() {
60+
assertThat((Map<Object, Object>) interpreter.resolveELExpression("{'k1':'v1'} + {'k2':'v2'}", -1))
61+
.containsOnly(entry("k1", "v1"), entry("k2", "v2"));
62+
}
63+
64+
}

0 commit comments

Comments
 (0)