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

fix: handling of double and integer #316

Merged
merged 1 commit into from
Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 6 additions & 5 deletions src/main/java/dev/openfeature/sdk/Structure.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,13 @@ default Object convertValue(Value value) {
return value.asBoolean();
}

if (value.isNumber()) {
Double valueAsDouble = value.asDouble();
if (valueAsDouble == Math.floor(valueAsDouble) && !Double.isInfinite(valueAsDouble)) {
return value.asInteger();
if (value.isNumber() && !value.isNull()) {
Number numberValue = (Number) value.asObject();
if (numberValue instanceof Double) {
return numberValue.doubleValue();
} else if (numberValue instanceof Integer) {
return numberValue.intValue();
}
return valueAsDouble;
}

if (value.isString()) {
Expand Down
15 changes: 7 additions & 8 deletions src/main/java/dev/openfeature/sdk/Value.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ public Value() {
* (boolean, string, int, double, list, structure, instant)
*/
public Value(Object value) throws InstantiationException {
// integer is a special case, convert those.
this.innerObject = value instanceof Integer ? ((Integer)value).doubleValue() : value;
this.innerObject = value;
if (!this.isNull()
&& !this.isBoolean()
&& !this.isString()
Expand All @@ -61,7 +60,7 @@ public Value(String value) {
}

public Value(Integer value) {
this.innerObject = value.doubleValue();
this.innerObject = value;
}

public Value(Double value) {
Expand Down Expand Up @@ -113,7 +112,7 @@ public boolean isString() {
* @return boolean
*/
public boolean isNumber() {
return this.innerObject instanceof Double;
return this.innerObject instanceof Number;
}

/**
Expand Down Expand Up @@ -187,8 +186,8 @@ public String asString() {
* @return Integer
*/
public Integer asInteger() {
if (this.isNumber()) {
return (int)Math.round((Double)this.innerObject);
if (this.isNumber() && !this.isNull()) {
return ((Number)this.innerObject).intValue();
}
return null;
}
Expand All @@ -199,8 +198,8 @@ public Integer asInteger() {
* @return Double
*/
public Double asDouble() {
if (this.isNumber()) {
return (Double)this.innerObject;
if (this.isNumber() && !isNull()) {
return ((Number)this.innerObject).doubleValue();
}
return null;
}
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/dev/openfeature/sdk/ValueTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ class Something {}
}

@Test public void numericArgShouldReturnDoubleOrInt() {
double innerDoubleValue = .75;
double innerDoubleValue = 1.75;
Value doubleValue = new Value(innerDoubleValue);
assertTrue(doubleValue.isNumber());
assertEquals(1, doubleValue.asInteger()); // should be rounded
assertEquals(.75, doubleValue.asDouble());
assertEquals(1, doubleValue.asInteger()); // the double value represented by this object converted to type int
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for linking the spec related to this rounding "toward zero". I was not aware of this nuance.

assertEquals(1.75, doubleValue.asDouble());

int innerIntValue = 100;
Value intValue = new Value(innerIntValue);
Expand Down