Skip to content

Commit

Permalink
fix: handling of double and integer
Browse files Browse the repository at this point in the history
Signed-off-by: apulbere <[email protected]>
  • Loading branch information
apulbere authored and toddbaert committed Mar 8, 2023
1 parent 09824e7 commit f4841b1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
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
assertEquals(1.75, doubleValue.asDouble());

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

0 comments on commit f4841b1

Please sign in to comment.