-
Notifications
You must be signed in to change notification settings - Fork 169
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
base: master
Are you sure you want to change the base?
Add more lenient coercing #1172
Conversation
if (value.matches(".*\\..*,.*")) { | ||
return value; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prefer precompiled regex expressions
if (!value.matches("\\d*\\.\\d*")) { | ||
return value; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prefer precompiled regex expressions
if (value instanceof String) { | ||
String sanitizedValue = trimDecimal(trimCommas((String) value)); | ||
return super.coerceToByte(sanitizedValue); | ||
} |
There was a problem hiding this comment.
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
assertThat( | ||
jinjava.renderForResult("{{ \"150.25,0\" is ge 4 }}", new HashMap<>()).getErrors() | ||
) | ||
.isNotEmpty(); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
if (value instanceof String) { | ||
String sanitizedValue = trimCommas((String) value); | ||
return super.coerceToDouble(sanitizedValue); | ||
} |
There was a problem hiding this comment.
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
strings can be implicitly coerced to numeric values, so comparisons like:
works. However evaluating:
or:
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:
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)