Skip to content

Commit 4cfc64b

Browse files
committed
updates
1 parent 28eea59 commit 4cfc64b

File tree

4 files changed

+95
-13
lines changed

4 files changed

+95
-13
lines changed

src/main/java/com/featurevisor/sdk/Featurevisor.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.featurevisor.types.Feature;
55
import com.featurevisor.types.EvaluatedFeature;
66
import com.featurevisor.types.EvaluatedFeatures;
7+
import com.featurevisor.types.VariableType;
78
import com.fasterxml.jackson.core.type.TypeReference;
89
import com.fasterxml.jackson.databind.ObjectMapper;
910
import java.util.Map;
@@ -526,11 +527,9 @@ public Object getVariable(String featureKey, String variableKey, Map<String, Obj
526527
Object value = evaluation.getVariableValue();
527528
if (value instanceof String) {
528529
String strValue = (String) value;
529-
boolean looksLikeJson = (strValue.startsWith("{") && strValue.endsWith("}")) ||
530-
(strValue.startsWith("[") && strValue.endsWith("]"));
531530
boolean isJsonType = evaluation.getVariableSchema() != null &&
532-
"json".equals(evaluation.getVariableSchema().getType());
533-
if (isJsonType || looksLikeJson) {
531+
evaluation.getVariableSchema().getType() == VariableType.JSON;
532+
if (isJsonType) {
534533
try {
535534
ObjectMapper mapper = new ObjectMapper();
536535
return mapper.readValue(strValue, Object.class);

src/main/java/com/featurevisor/sdk/Helpers.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,17 @@ public static <T> T getValueByType(Object value, String fieldType) {
4040
}
4141
return null;
4242
case "boolean":
43-
if (value instanceof Boolean) {
44-
return (T) value;
45-
} else if (value instanceof String) {
46-
return (T) Boolean.valueOf((String) value);
47-
} else if (value instanceof Number) {
48-
return (T) Boolean.valueOf(((Number) value).intValue() != 0);
49-
}
50-
return null;
43+
return (T) Boolean.valueOf(Boolean.TRUE.equals(value));
5144
case "array":
5245
return (T) (value instanceof List ? value : null);
5346
case "object":
54-
return (T) (value instanceof Map ? value : null);
47+
if (value instanceof Map) {
48+
return (T) value;
49+
}
50+
if (value instanceof List) {
51+
return (T) value;
52+
}
53+
return null;
5554
case "json":
5655
// JSON type is handled specially in the calling code
5756
return (T) value;

src/main/java/com/featurevisor/types/VariableType.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.featurevisor.types;
22

3+
import com.fasterxml.jackson.annotation.JsonCreator;
34
import com.fasterxml.jackson.annotation.JsonProperty;
5+
import com.fasterxml.jackson.annotation.JsonValue;
46

57
public enum VariableType {
68
@JsonProperty("boolean")
@@ -24,7 +26,23 @@ public enum VariableType {
2426
this.value = value;
2527
}
2628

29+
@JsonValue
2730
public String getValue() {
2831
return value;
2932
}
33+
34+
@JsonCreator
35+
public static VariableType fromValue(String value) {
36+
if (value == null) {
37+
return null;
38+
}
39+
40+
for (VariableType type : values()) {
41+
if (type.value.equalsIgnoreCase(value)) {
42+
return type;
43+
}
44+
}
45+
46+
return null;
47+
}
3048
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.featurevisor.sdk;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
import static org.junit.jupiter.api.Assertions.assertEquals;
9+
import static org.junit.jupiter.api.Assertions.assertNull;
10+
11+
public class HelpersTest {
12+
13+
@Test
14+
public void testStringMismatchReturnsNull() {
15+
assertNull(Helpers.getValueByType(1, "string"));
16+
}
17+
18+
@Test
19+
public void testStringValueReturnedAsIs() {
20+
assertEquals("1", Helpers.getValueByType("1", "string"));
21+
}
22+
23+
@Test
24+
public void testBooleanValueReturnedAsIs() {
25+
assertEquals(Boolean.TRUE, Helpers.getValueByType(true, "boolean"));
26+
}
27+
28+
@Test
29+
public void testBooleanStrictness() {
30+
assertEquals(Boolean.FALSE, Helpers.getValueByType("true", "boolean"));
31+
assertEquals(Boolean.FALSE, Helpers.getValueByType(1, "boolean"));
32+
}
33+
34+
@Test
35+
public void testObjectValueReturnedAsIs() {
36+
Map<String, Integer> value = Map.of("a", 1, "b", 2);
37+
assertEquals(value, Helpers.getValueByType(value, "object"));
38+
}
39+
40+
@Test
41+
public void testJsonValueReturnedAsIs() {
42+
String json = "{\"a\":1,\"b\":2}";
43+
assertEquals(json, Helpers.getValueByType(json, "json"));
44+
}
45+
46+
@Test
47+
public void testArrayValueReturnedAsIs() {
48+
List<String> arr = List.of("1", "2", "3");
49+
assertEquals(arr, Helpers.getValueByType(arr, "array"));
50+
}
51+
52+
@Test
53+
public void testIntegerParsing() {
54+
assertEquals(Integer.valueOf(1), Helpers.getValueByType("1", "integer"));
55+
}
56+
57+
@Test
58+
public void testDoubleParsing() {
59+
assertEquals(1.1d, Helpers.getValueByType("1.1", "double"));
60+
}
61+
62+
@Test
63+
public void testNullValueReturnsNull() {
64+
assertNull(Helpers.getValueByType(null, "string"));
65+
}
66+
}

0 commit comments

Comments
 (0)