-
Notifications
You must be signed in to change notification settings - Fork 4k
core: support Integer and Long in service configuration parsing #12827
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
Closed
kannanjgithub
wants to merge
2
commits into
grpc:master
from
kannanjgithub:service-config-parser-integer
+152
−19
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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 |
|---|---|---|
|
|
@@ -18,6 +18,8 @@ | |
|
|
||
| import static com.google.common.math.LongMath.checkedAdd; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.math.BigInteger; | ||
| import java.text.ParseException; | ||
| import java.util.List; | ||
| import java.util.Locale; | ||
|
|
@@ -106,8 +108,8 @@ public static Double getNumberAsDouble(Map<String, ?> obj, String key) { | |
| return null; | ||
| } | ||
| Object value = obj.get(key); | ||
| if (value instanceof Double) { | ||
| return (Double) value; | ||
| if (value instanceof Number) { | ||
| return ((Number) value).doubleValue(); | ||
| } | ||
| if (value instanceof String) { | ||
| try { | ||
|
|
@@ -132,8 +134,8 @@ public static Float getNumberAsFloat(Map<String, ?> obj, String key) { | |
| return null; | ||
| } | ||
| Object value = obj.get(key); | ||
| if (value instanceof Float) { | ||
| return (Float) value; | ||
| if (value instanceof Number) { | ||
| return ((Number) value).floatValue(); | ||
| } | ||
| if (value instanceof String) { | ||
| try { | ||
|
|
@@ -159,13 +161,29 @@ public static Integer getNumberAsInteger(Map<String, ?> obj, String key) { | |
| return null; | ||
| } | ||
| Object value = obj.get(key); | ||
| if (value instanceof Double) { | ||
| Double d = (Double) value; | ||
| int i = d.intValue(); | ||
| if (i != d) { | ||
| throw new ClassCastException("Number expected to be integer: " + d); | ||
| if (value instanceof Number) { | ||
| Number n = (Number) value; | ||
| if (n instanceof Integer || n instanceof Short || n instanceof Byte) { | ||
| return n.intValue(); | ||
| } | ||
| return i; | ||
| try { | ||
| if (n instanceof Long) { | ||
| return Math.toIntExact(n.longValue()); | ||
| } | ||
| if (n instanceof BigInteger) { | ||
| return ((BigInteger) n).intValueExact(); | ||
| } | ||
| if (n instanceof BigDecimal) { | ||
| return ((BigDecimal) n).intValueExact(); | ||
| } | ||
| } catch (ArithmeticException e) { | ||
| throw new ClassCastException("Number expected to be integer: " + n); | ||
| } | ||
| double d = n.doubleValue(); | ||
| if (Math.rint(d) == d && d >= Integer.MIN_VALUE && d <= Integer.MAX_VALUE) { | ||
| return n.intValue(); | ||
| } | ||
| throw new ClassCastException("Number expected to be integer: " + n); | ||
| } | ||
| if (value instanceof String) { | ||
| try { | ||
|
|
@@ -190,13 +208,26 @@ public static Long getNumberAsLong(Map<String, ?> obj, String key) { | |
| return null; | ||
| } | ||
| Object value = obj.get(key); | ||
| if (value instanceof Double) { | ||
| Double d = (Double) value; | ||
| long l = d.longValue(); | ||
| if (l != d) { | ||
| throw new ClassCastException("Number expected to be long: " + d); | ||
| if (value instanceof Number) { | ||
| Number n = (Number) value; | ||
| if (n instanceof Long || n instanceof Integer || n instanceof Short || n instanceof Byte) { | ||
| return n.longValue(); | ||
| } | ||
| try { | ||
| if (n instanceof BigInteger) { | ||
| return ((BigInteger) n).longValueExact(); | ||
| } | ||
| if (n instanceof BigDecimal) { | ||
| return ((BigDecimal) n).longValueExact(); | ||
| } | ||
| } catch (ArithmeticException e) { | ||
| throw new ClassCastException("Number expected to be long: " + n); | ||
| } | ||
|
Comment on lines
+211
to
225
Contributor
Author
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. Done. |
||
| double d = n.doubleValue(); | ||
| if (Math.rint(d) == d && d >= (double) Long.MIN_VALUE && d < Math.scalb(1.0, 63)) { | ||
| return n.longValue(); | ||
| } | ||
| return l; | ||
| throw new ClassCastException("Number expected to be long: " + n); | ||
| } | ||
| if (value instanceof String) { | ||
| try { | ||
|
|
||
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
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.