diff --git a/src/main/java/com/hubspot/jinjava/el/TruthyTypeConverter.java b/src/main/java/com/hubspot/jinjava/el/TruthyTypeConverter.java index 25babab14..14ad09082 100644 --- a/src/main/java/com/hubspot/jinjava/el/TruthyTypeConverter.java +++ b/src/main/java/com/hubspot/jinjava/el/TruthyTypeConverter.java @@ -33,6 +33,12 @@ protected BigDecimal coerceToBigDecimal(Object value) { if (value instanceof DummyObject) { return BigDecimal.ZERO; } + + if (value instanceof String) { + String sanitizedValue = trimCommas((String) value); + return super.coerceToBigDecimal(sanitizedValue); + } + return super.coerceToBigDecimal(value); } @@ -41,6 +47,12 @@ protected BigInteger coerceToBigInteger(Object value) { if (value instanceof DummyObject) { return BigInteger.ZERO; } + + if (value instanceof String) { + String sanitizedValue = trimDecimal(trimCommas((String) value)); + return super.coerceToBigInteger(sanitizedValue); + } + return super.coerceToBigInteger(value); } @@ -49,6 +61,12 @@ protected Double coerceToDouble(Object value) { if (value instanceof DummyObject) { return 0d; } + + if (value instanceof String) { + String sanitizedValue = trimCommas((String) value); + return super.coerceToDouble(sanitizedValue); + } + return super.coerceToDouble(value); } @@ -57,6 +75,12 @@ protected Float coerceToFloat(Object value) { if (value instanceof DummyObject) { return 0f; } + + if (value instanceof String) { + String sanitizedValue = trimCommas((String) value); + return super.coerceToFloat(sanitizedValue); + } + return super.coerceToFloat(value); } @@ -65,6 +89,11 @@ protected Long coerceToLong(Object value) { if (value instanceof DummyObject) { return 0L; } + + if (value instanceof String) { + String sanitizedValue = trimDecimal(trimCommas((String) value)); + return super.coerceToLong(sanitizedValue); + } return super.coerceToLong(value); } @@ -73,6 +102,11 @@ protected Integer coerceToInteger(Object value) { if (value instanceof DummyObject) { return 0; } + + if (value instanceof String) { + String sanitizedValue = trimDecimal(trimCommas((String) value)); + return super.coerceToInteger(sanitizedValue); + } return super.coerceToInteger(value); } @@ -81,6 +115,11 @@ protected Short coerceToShort(Object value) { if (value instanceof DummyObject) { return 0; } + + if (value instanceof String) { + String sanitizedValue = trimDecimal(trimCommas((String) value)); + return super.coerceToShort(sanitizedValue); + } return super.coerceToShort(value); } @@ -89,9 +128,36 @@ protected Byte coerceToByte(Object value) { if (value instanceof DummyObject) { return 0; } + if (value instanceof String) { + String sanitizedValue = trimDecimal(trimCommas((String) value)); + return super.coerceToByte(sanitizedValue); + } + return super.coerceToByte(value); } + private String trimCommas(String value) { + if (!value.contains(",")) { + return value; + } + + if (value.matches(".*\\..*,.*")) { + return value; + } + return value.replaceAll(",", ""); + } + + private String trimDecimal(String value) { + if (!value.contains(".")) { + return value; + } + + if (!value.matches("\\d*\\.\\d*")) { + return value; + } + return value.substring(0, value.indexOf(".")); + } + @Override protected String coerceToString(Object value) { if (value instanceof DummyObject) { diff --git a/src/test/java/com/hubspot/jinjava/lib/exptest/ComparisonExpTestsTest.java b/src/test/java/com/hubspot/jinjava/lib/exptest/ComparisonExpTestsTest.java index f2dd38e64..6992b57fa 100644 --- a/src/test/java/com/hubspot/jinjava/lib/exptest/ComparisonExpTestsTest.java +++ b/src/test/java/com/hubspot/jinjava/lib/exptest/ComparisonExpTestsTest.java @@ -1,13 +1,17 @@ package com.hubspot.jinjava.lib.exptest; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import com.google.common.collect.ImmutableMap; import com.hubspot.jinjava.BaseJinjavaTest; +import com.hubspot.jinjava.interpret.FatalTemplateErrorsException; import com.hubspot.jinjava.objects.date.PyishDate; import java.time.Instant; import java.util.HashMap; import java.util.Map; +import javax.el.ELException; +import javax.el.MethodNotFoundException; import org.junit.Test; public class ComparisonExpTestsTest extends BaseJinjavaTest { @@ -66,4 +70,27 @@ public void testAliases() { assertThat(jinjava.render("{{ 4 is >= 5 }}", new HashMap<>())).isEqualTo("false"); assertThat(jinjava.render("{{ 4 is != 5 }}", new HashMap<>())).isEqualTo("true"); } + + @Test + public void itParsesFormattedNumbers() { + assertThat(jinjava.render("{{ \"1,050.25\" is ge 4 }}", new HashMap<>())) + .isEqualTo("true"); + assertThat(jinjava.render("{{ \"4.1\" is gt 4 }}", new HashMap<>())) + .isEqualTo("false"); + assertThat(jinjava.render("{{ 4.0 is le 5.00 }}", new HashMap<>())).isEqualTo("true"); + assertThat(jinjava.render("{{ \"4,500.75\" is le 10000.00 }}", new HashMap<>())) + .isEqualTo("true"); + } + + @Test + public void itDoesNotAllowCommasAfterDecimalPointInNumbers() { + assertThat( + jinjava.renderForResult("{{ \"150.25,0\" is ge 4 }}", new HashMap<>()).getErrors() + ) + .isNotEmpty(); + assertThat( + jinjava.renderForResult("{{ \"150.25,0\" is ge 4.0 }}", new HashMap<>()).getErrors() + ) + .isNotEmpty(); + } }