Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new MapELResolver with type coercion to support accessing enum keys #688

Merged
merged 4 commits into from
Jun 17, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
import javax.el.CompositeELResolver;
import javax.el.ELContext;
import javax.el.ELResolver;
import javax.el.MapELResolver;
import javax.el.PropertyNotFoundException;
import javax.el.ResourceBundleELResolver;
import org.apache.commons.lang3.LocaleUtils;
Expand All @@ -57,7 +56,7 @@ public class JinjavaInterpreterResolver extends SimpleResolver {
{
add(new ArrayELResolver(true));
add(new JinjavaListELResolver(true));
add(new MapELResolver(true));
add(new TypeConvertingMapELResolver(true));
add(new ResourceBundleELResolver());
add(new JinjavaBeanELResolver(true));
}
Expand All @@ -68,7 +67,7 @@ public class JinjavaInterpreterResolver extends SimpleResolver {
{
add(new ArrayELResolver(false));
add(new JinjavaListELResolver(false));
add(new MapELResolver(false));
add(new TypeConvertingMapELResolver(false));
add(new ResourceBundleELResolver());
add(new JinjavaBeanELResolver(false));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.hubspot.jinjava.el;

import com.hubspot.jinjava.el.ext.CollectionMembershipOperator;
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 &&
(Boolean) CollectionMembershipOperator.OP.apply(TYPE_CONVERTER, property, base)
liamrharwood marked this conversation as resolved.
Show resolved Hide resolved
) {
Class<?> keyClass = ((Map) base).keySet().iterator().next().getClass();
liamrharwood marked this conversation as resolved.
Show resolved Hide resolved
value = ((Map) base).get(TYPE_CONVERTER.convert(property, keyClass));
if (value != null) {
context.setPropertyResolved(true);
}
}

return value;
}
}
53 changes: 53 additions & 0 deletions src/test/java/com/hubspot/jinjava/el/ext/AstDictTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
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 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;
}
}
}