|
35 | 35 | import java.util.LinkedHashMap; |
36 | 36 | import java.util.List; |
37 | 37 | import java.util.Map; |
| 38 | +import java.util.Optional; |
38 | 39 | import java.util.concurrent.CompletableFuture; |
39 | 40 | import java.util.concurrent.atomic.AtomicLong; |
40 | 41 | import org.junit.After; |
@@ -93,6 +94,15 @@ private JinjavaInterpreter getInterpreter(boolean evaluateMapKeys) throws Except |
93 | 94 | this.getClass().getDeclaredMethod("sleeper") |
94 | 95 | ) |
95 | 96 | ); |
| 97 | + jinjava |
| 98 | + .getGlobalContext() |
| 99 | + .registerFunction( |
| 100 | + new ELFunctionDefinition( |
| 101 | + "", |
| 102 | + "optionally", |
| 103 | + this.getClass().getDeclaredMethod("optionally", boolean.class) |
| 104 | + ) |
| 105 | + ); |
96 | 106 | interpreter = new JinjavaInterpreter(jinjava.newInterpreter()); |
97 | 107 | context = interpreter.getContext(); |
98 | 108 | context.put("deferred", DeferredValue.instance()); |
@@ -124,6 +134,14 @@ public void itResolvesDeferredBoolean() { |
124 | 134 | assertThat(interpreter.resolveELExpression(partiallyResolved, 1)).isEqualTo(true); |
125 | 135 | } |
126 | 136 |
|
| 137 | + @Test |
| 138 | + public void itSerializesNestedOptional() { |
| 139 | + assertThat(eagerResolveExpression("[optionally(true)]").toString()) |
| 140 | + .isEqualTo("['1']"); |
| 141 | + assertThat(eagerResolveExpression("[optionally(false)]").toString()) |
| 142 | + .isEqualTo("[null]"); |
| 143 | + } |
| 144 | + |
127 | 145 | @Test |
128 | 146 | public void itResolvesDeferredList() { |
129 | 147 | context.put("foo", "foo_val"); |
@@ -877,6 +895,10 @@ public static long sleeper() throws InterruptedException { |
877 | 895 | return sleepTime; |
878 | 896 | } |
879 | 897 |
|
| 898 | + public static Optional<String> optionally(boolean hasValue) { |
| 899 | + return Optional.of(hasValue).filter(Boolean::booleanValue).map(ignored -> "1"); |
| 900 | + } |
| 901 | + |
880 | 902 | private static class Foo { |
881 | 903 |
|
882 | 904 | private final String bar; |
|
0 commit comments