Special case 'entrySet' on Map fetcher.
You can't normally reflectively access methods on Map because it has a special fetcher, but being able to reflectively access 'entrySet' is super useful when you want to iterate over the keys+values of a map, so we're going to hack it in there. Ha!
This commit is contained in:
@@ -71,7 +71,10 @@ public abstract class BasicCollector implements Mustache.Collector
|
|||||||
protected static final Mustache.VariableFetcher MAP_FETCHER = new Mustache.VariableFetcher() {
|
protected static final Mustache.VariableFetcher MAP_FETCHER = new Mustache.VariableFetcher() {
|
||||||
public Object get (Object ctx, String name) throws Exception {
|
public Object get (Object ctx, String name) throws Exception {
|
||||||
Map<?,?> map = (Map<?,?>)ctx;
|
Map<?,?> map = (Map<?,?>)ctx;
|
||||||
return map.containsKey(name) ? map.get(name) : Template.NO_FETCHER_FOUND;
|
if (map.containsKey(name)) return map.get(name);
|
||||||
|
// special case to allow map entry set to be iterated over
|
||||||
|
if (name == "entrySet") return map.entrySet();
|
||||||
|
return Template.NO_FETCHER_FOUND;
|
||||||
}
|
}
|
||||||
@Override public String toString () {
|
@Override public String toString () {
|
||||||
return "MAP_FETCHER";
|
return "MAP_FETCHER";
|
||||||
|
|||||||
@@ -70,6 +70,14 @@ public abstract class SharedTests extends GWTTestCase
|
|||||||
"foo", Arrays.asList(context("bar", "baz"), context("bar", "bif"))));
|
"foo", Arrays.asList(context("bar", "baz"), context("bar", "bif"))));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test public void testMapEntriesSection () {
|
||||||
|
Map<String,String> data = new HashMap<String,String>();
|
||||||
|
data.put("k1", "v1");
|
||||||
|
data.put("k2", "v2");
|
||||||
|
test("k1v1k2v2", "{{#map.entrySet}}{{key}}{{value}}{{/map.entrySet}}",
|
||||||
|
context("map", data));
|
||||||
|
}
|
||||||
|
|
||||||
@Test public void testArraySection () {
|
@Test public void testArraySection () {
|
||||||
test("bazbif", "{{#foo}}{{bar}}{{/foo}}",
|
test("bazbif", "{{#foo}}{{bar}}{{/foo}}",
|
||||||
context("foo", new Object[] { context("bar", "baz"), context("bar", "bif") }));
|
context("foo", new Object[] { context("bar", "baz"), context("bar", "bif") }));
|
||||||
|
|||||||
Reference in New Issue
Block a user