Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
add more lenient string parsing
  • Loading branch information
ruslanhubspot committed Mar 29, 2024
commit 63be60dc99559c478d7735014cd3339bd5f44920
58 changes: 58 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 = trimDecimal(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
Copy Markdown
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 = trimDecimal(trimCommas((String) value));
return super.coerceToFloat(sanitizedValue);
}

return super.coerceToFloat(value);
}

Expand All @@ -65,14 +89,38 @@ 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);
}

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

private String trimDecimal(String value) {
if (!value.contains(".")) {
return value;
}
return value.substring(0, value.indexOf("."));
}

@Override
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 +129,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,6 +142,11 @@ 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
Copy Markdown
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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,15 @@ 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 testFormattedStringParsing() {
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");
}
}