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 more lenient coercing #1172

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
66 changes: 66 additions & 0 deletions src/main/java/com/hubspot/jinjava/el/TruthyTypeConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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);
}
Comment on lines +65 to +68
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer these to use similar logic as in IntFilter and FloatFilter. Make use of NumberFormat#parse


return super.coerceToDouble(value);
}

Expand All @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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);
}
Comment on lines +131 to +134
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't think we should do this when coercing to byte


return super.coerceToByte(value);
}

private String trimCommas(String value) {
if (!value.contains(",")) {
return value;
}

if (value.matches(".*\\..*,.*")) {
return value;
}
Comment on lines +144 to +146
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer precompiled regex expressions

return value.replaceAll(",", "");
}

private String trimDecimal(String value) {
if (!value.contains(".")) {
return value;
}

if (!value.matches("\\d*\\.\\d*")) {
return value;
}
Comment on lines +155 to +157
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer precompiled regex expressions

return value.substring(0, value.indexOf("."));
}

@Override
protected String coerceToString(Object value) {
if (value instanceof DummyObject) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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();
Comment on lines +87 to +90
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some languages have decimals and commas flipped and should be handled in the opposite manner

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, would your suggested approach here be to make the TruthyTypeConverter constructor take in a JinjavaContext param in order to access in the current language?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or perhaps just a Locale

assertThat(
jinjava.renderForResult("{{ \"150.25,0\" is ge 4.0 }}", new HashMap<>()).getErrors()
)
.isNotEmpty();
}
}