-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new MapELResolver with type coercion to support accessing enum ke…
…ys (#688) * enum dict test case * Add EnumMapElResolver to handle accessing enum variables by name * Use CollectionMembershipOperator * Add logic to handle custom enum toString Co-authored-by: Matt Coley <[email protected]>
- Loading branch information
1 parent
2e8821a
commit bbf37df
Showing
4 changed files
with
131 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/main/java/com/hubspot/jinjava/el/TypeConvertingMapELResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.hubspot.jinjava.el; | ||
|
||
import java.util.Map; | ||
import javax.el.ELContext; | ||
import javax.el.MapELResolver; | ||
|
||
public class TypeConvertingMapELResolver extends MapELResolver { | ||
private static final TruthyTypeConverter TYPE_CONVERTER = new TruthyTypeConverter(); | ||
|
||
public TypeConvertingMapELResolver(boolean readOnly) { | ||
super(readOnly); | ||
} | ||
|
||
@Override | ||
public Object getValue(ELContext context, Object base, Object property) { | ||
Object value = super.getValue(context, base, property); | ||
|
||
if (value != null) { | ||
return value; | ||
} | ||
|
||
if (base instanceof Map && !((Map) base).isEmpty()) { | ||
Class<?> keyClass = ((Map) base).keySet().iterator().next().getClass(); | ||
value = ((Map) base).get(TYPE_CONVERTER.convert(property, keyClass)); | ||
if (value != null) { | ||
context.setPropertyResolved(true); | ||
} | ||
} | ||
|
||
return value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package com.hubspot.jinjava.el.ext; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import com.hubspot.jinjava.Jinjava; | ||
import com.hubspot.jinjava.interpret.JinjavaInterpreter; | ||
import com.hubspot.jinjava.interpret.TemplateError.ErrorType; | ||
import java.util.Map; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class AstDictTest { | ||
private JinjavaInterpreter interpreter; | ||
|
||
@Before | ||
public void setup() { | ||
interpreter = new Jinjava().newInterpreter(); | ||
} | ||
|
||
@Test | ||
public void itGetsDictValues() { | ||
interpreter.getContext().put("foo", ImmutableMap.of("bar", "test")); | ||
assertThat(interpreter.resolveELExpression("foo.bar", -1)).isEqualTo("test"); | ||
} | ||
|
||
@Test | ||
public void itGetsDictValuesWithEnumKeys() { | ||
interpreter.getContext().put("foo", ImmutableMap.of(ErrorType.FATAL, "test")); | ||
assertThat(interpreter.resolveELExpression("foo.FATAL", -1)).isEqualTo("test"); | ||
} | ||
|
||
@Test | ||
public void itGetsDictValuesWithEnumKeysUsingToString() { | ||
interpreter.getContext().put("foo", ImmutableMap.of(TestEnum.BAR, "test")); | ||
assertThat(interpreter.resolveELExpression("foo.barName", -1)).isEqualTo("test"); | ||
} | ||
|
||
@Test | ||
public void itHandlesEmptyMaps() { | ||
interpreter.getContext().put("foo", ImmutableMap.of()); | ||
assertThat(interpreter.resolveELExpression("foo.FATAL", -1)).isNull(); | ||
assertThat(interpreter.getErrors()).isEmpty(); | ||
} | ||
|
||
@Test | ||
public void itGetsDictValuesWithEnumKeysInObjects() { | ||
interpreter | ||
.getContext() | ||
.put("test", new TestClass(ImmutableMap.of(ErrorType.FATAL, "test"))); | ||
assertThat(interpreter.resolveELExpression("test.my_map.FATAL", -1)) | ||
.isEqualTo("test"); | ||
} | ||
|
||
public class TestClass { | ||
private Map<ErrorType, String> myMap; | ||
|
||
public TestClass(Map<ErrorType, String> myMap) { | ||
this.myMap = myMap; | ||
} | ||
|
||
public Map<ErrorType, String> getMyMap() { | ||
return myMap; | ||
} | ||
} | ||
|
||
public enum TestEnum { | ||
FOO("fooName"), | ||
BAR("barName"); | ||
|
||
private String name; | ||
|
||
TestEnum(String name) { | ||
this.name = name; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return name; | ||
} | ||
} | ||
} |