Skip to content

Commit 9809123

Browse files
committed
Support for iterating Maps with non-string keys fix jknack#502
1 parent d9c40e2 commit 9809123

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed

handlebars/src/main/java/com/github/jknack/handlebars/helper/EachHelper.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,14 @@ public Object apply(final Object context, final Options options)
7979
}
8080
return buffer;
8181
} else if (context != null) {
82-
Iterator<Entry<String, Object>> loop = options.propertySet(context).iterator();
82+
Iterator loop = options.propertySet(context).iterator();
8383
Context parent = options.context;
8484
boolean first = true;
8585
Options.Buffer buffer = options.buffer();
8686
Template fn = options.fn;
8787
while (loop.hasNext()) {
88-
Entry<String, Object> entry = loop.next();
89-
String key = entry.getKey();
88+
Entry entry = (Entry) loop.next();
89+
Object key = entry.getKey();
9090
Object value = entry.getValue();
9191
Context itCtx = Context.newBuilder(parent, value)
9292
.combine("@key", key)

handlebars/src/main/java/com/github/jknack/handlebars/internal/Variable.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,14 @@ protected void merge(final Context scope, final Writer writer)
163163
}
164164
}
165165

166+
/**
167+
* Apply the template and return the raw value (not a CharSequence)
168+
*
169+
* @param scope Template scope.
170+
* @param writer Writer.
171+
* @return Resulting value.
172+
* @throws IOException If something goes wrong.
173+
*/
166174
@SuppressWarnings("unchecked")
167175
public Object value(final Context scope, final Writer writer) throws IOException {
168176
boolean blockParam = scope.isBlockParams() && noArg;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.github.jknack.handlebars.issues;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.io.IOException;
6+
import java.util.ArrayList;
7+
import java.util.Collection;
8+
import java.util.HashMap;
9+
import java.util.Map;
10+
import java.util.TreeMap;
11+
12+
import org.junit.Test;
13+
14+
import com.github.jknack.handlebars.Handlebars;
15+
16+
public class Issue502 {
17+
18+
@Test
19+
public void supportForIteratingMapsWithNonStringKeys() throws IOException {
20+
Map<Object, Collection<String>> data = new TreeMap<>();
21+
Collection<String> d1 = new ArrayList<>();
22+
d1.add("1");
23+
d1.add("2");
24+
d1.add("3");
25+
26+
Collection<String> d2 = new ArrayList<>();
27+
d2.add("4");
28+
d2.add("5");
29+
d2.add("6");
30+
31+
data.put(123, d1);
32+
data.put(456, d2);
33+
34+
Map<String, Object> params = new HashMap<>();
35+
params.put("data", data);
36+
37+
String result = new Handlebars()
38+
.compileInline("{{#each data}}{{ @key }} - {{#each . }}Val:{{.}}{{/each}}{{/each}}")
39+
.apply(params);
40+
41+
assertEquals("123 - Val:1Val:2Val:3456 - Val:4Val:5Val:6", result);
42+
}
43+
44+
}

0 commit comments

Comments
 (0)