Skip to content

Commit 4987bac

Browse files
committed
Proposal for enhancement
#4
1 parent a9aad5b commit 4987bac

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

Releasenotes.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,5 +151,5 @@ Release 3.2.0
151151
* Transformation of model value to iterable for for each now done in ModelAdaptor
152152
* ModelAdapter can be configured to treat everything as a list when looping over it, prevents map to be iterated over as entries
153153
* ModelAdapter now manages special iterator alias, DefaultModelAdapter provides "_it" for backward compatibility
154-
154+
* ModelAdapter now has a (slow) fallback to access maps with keys that are not strings (iterating through all keys, turning them to strings and compare), can be deactivated
155155

src/com/floreysoft/jmte/DefaultModelAdaptor.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,20 @@ public enum LoopMode {
4343
protected final Map<Class<?>, Map<String, Member>> cache = new HashMap<Class<?>, Map<String, Member>>();
4444
private final LoopMode loopMode;
4545
private final String specialIteratorVariable;
46+
private final boolean enableSlowMapAccess;
4647

4748
public DefaultModelAdaptor() {
4849
this(LoopMode.DEFAULT);
4950
}
5051

5152
public DefaultModelAdaptor(LoopMode loopMode) {
52-
this(loopMode, DEFAULT_SPECIAL_ITERATOR_VARIABLE);
53+
this(loopMode, DEFAULT_SPECIAL_ITERATOR_VARIABLE, true);
5354
}
5455

55-
public DefaultModelAdaptor(LoopMode loopMode, String specialIteratorVariable) {
56+
public DefaultModelAdaptor(LoopMode loopMode, String specialIteratorVariable, boolean enableSlowMapAccess) {
5657
this.loopMode = loopMode;
5758
this.specialIteratorVariable = specialIteratorVariable;
59+
this.enableSlowMapAccess = enableSlowMapAccess;
5860
}
5961

6062
public Object getValue(Map<String, Object> model, String expression) {
@@ -149,8 +151,7 @@ protected Object nextStep(Object o, String attributeName,
149151
"receiver", o.toString()).build());
150152
return o;
151153
} else if (o instanceof Map) {
152-
Map map = (Map) o;
153-
result = map.get(attributeName);
154+
result = accessMap((Map) o, attributeName);
154155
} else {
155156
try {
156157
result = getPropertyValue(o, attributeName);
@@ -164,6 +165,21 @@ protected Object nextStep(Object o, String attributeName,
164165
return result;
165166
}
166167

168+
protected Object accessMap(Map map, String key) {
169+
Object result;
170+
result = map.get(key);
171+
if (result == null && enableSlowMapAccess) {
172+
final Set<Map.Entry<?, ?>> entries = map.entrySet();
173+
for (Map.Entry entry: entries) {
174+
if (entry.getKey().toString().equals(key)) {
175+
result = entry.getValue();
176+
break;
177+
}
178+
}
179+
}
180+
return result;
181+
}
182+
167183
@SuppressWarnings("rawtypes")
168184
protected Object getPropertyValue(Object o, String propertyName) {
169185
try {

test/com/floreysoft/jmte/AbstractEngineTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,12 @@ public Iterator<Object> iterator() {
152152
}
153153
}
154154

155+
public static enum MyType {
156+
TYPE_A, TYPE_B;
157+
}
158+
155159
public static class MyBean {
160+
public Map<MyType, String> mapEnumAsKey = new HashMap<>();
156161

157162
private Object property1 = "propertyValue1";
158163
public Object property2 = "propertyValue2";
@@ -165,6 +170,8 @@ public MyBean(Object property1, Object property2) {
165170
}
166171

167172
public MyBean() {
173+
mapEnumAsKey.put(MyType.TYPE_A, "A");
174+
mapEnumAsKey.put(MyType.TYPE_B, "B");
168175
}
169176

170177
public List getList() {
@@ -1421,4 +1428,11 @@ public void xmlEncoderRawNamedRenderer() throws Exception {
14211428
assertEquals("&<>'\"", actual);
14221429
}
14231430

1431+
@Test
1432+
public void enumAsKey() throws Exception {
1433+
String output = newEngine().transform(
1434+
"${bean.mapEnumAsKey.TYPE_A}", DEFAULT_MODEL);
1435+
assertEquals("A", output);
1436+
}
1437+
14241438
}

0 commit comments

Comments
 (0)