-
Notifications
You must be signed in to change notification settings - Fork 173
Add more lenient coercing #1172
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
Open
ruslanhubspot
wants to merge
3
commits into
HubSpot:master
Choose a base branch
from
ruslanhubspot:add-more-lenient-coercing
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
File filter
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
commit 63be60dc99559c478d7735014cd3339bd5f44920
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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); | ||
| } | ||
|
|
||
|
|
@@ -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 = trimDecimal(trimCommas((String) value)); | ||
| return super.coerceToFloat(sanitizedValue); | ||
| } | ||
|
|
||
| return super.coerceToFloat(value); | ||
| } | ||
|
|
||
|
|
@@ -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); | ||
| } | ||
|
|
||
|
|
@@ -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); | ||
| } | ||
|
|
||
|
|
@@ -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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
IntFilterandFloatFilter. Make use ofNumberFormat#parse