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

Conversation

ruslanhubspot
Copy link

strings can be implicitly coerced to numeric values, so comparisons like:

{% if "1000" > 5%}

works. However evaluating:

{% if "1,000" > 5%}

or:

{% if "1000.25" > 5%}

throws an error. This adds some additional cleaning to allow formatted number strings to be parsed.
Notably it does not remove decimals when comparing to floats, since those are valuable and:

{% if "1000.25" > 50.0 %}

already works.

It also only removes characters after decimals if its a properly formatted decimal number (so containing no non-digit characters and only one decimal point)

@ruslanhubspot
Copy link
Author

Comment on lines +144 to +146
if (value.matches(".*\\..*,.*")) {
return value;
}
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

Comment on lines +155 to +157
if (!value.matches("\\d*\\.\\d*")) {
return value;
}
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

Comment on lines +131 to +134
if (value instanceof String) {
String sanitizedValue = trimDecimal(trimCommas((String) value));
return super.coerceToByte(sanitizedValue);
}
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

Comment on lines +87 to +90
assertThat(
jinjava.renderForResult("{{ \"150.25,0\" is ge 4 }}", new HashMap<>()).getErrors()
)
.isNotEmpty();
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

Comment on lines +65 to +68
if (value instanceof String) {
String sanitizedValue = trimCommas((String) value);
return super.coerceToDouble(sanitizedValue);
}
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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants