Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -180,19 +180,54 @@ private <T> ProviderEvaluation<T> buildEvaluation(
* @param expectedType the type we expect for this value
* @param <T> the type we want to convert to
* @return A converted object
* @throws TypeMismatchError if the value cannot be converted to the expected type
*/
@SuppressWarnings("unchecked")
private <T> T convertValue(Object value, Class<?> expectedType) {
boolean isPrimitive = expectedType == Boolean.class
|| expectedType == String.class
|| expectedType == Integer.class
|| expectedType == Double.class;
T flagValue = isPrimitive ? (T) value : (T) objectToValue(value);

if (flagValue.getClass() != expectedType) {
throw new TypeMismatchError(
"Flag value had an unexpected type " + flagValue.getClass() + ", expected " + expectedType + ".");
Object flagValue;
String message = "Flag value had an unexpected type ("
+ (value != null ? value.getClass() : "null") + "), expected (" + expectedType + ").";
if (isPrimitive) {
if (expectedType == Double.class) {
if (value instanceof Double) {
flagValue = value;
} else if (value instanceof String) {
try {
flagValue = Double.parseDouble((String) value);
} catch (NumberFormatException e) {
throw new TypeMismatchError("Flag value string could not be parsed as Double: " + value);
}
} else {
throw new TypeMismatchError(message);
}
} else if (expectedType == Integer.class) {
if (value instanceof Integer) {
flagValue = value;
} else if (value instanceof String) {
try {
flagValue = Integer.parseInt((String) value);
} catch (NumberFormatException e) {
throw new TypeMismatchError("Flag value string could not be parsed as Integer: " + value);
}
} else {
throw new TypeMismatchError(message);
}
} else {
flagValue = value;
}
} else {
flagValue = objectToValue(value);
}

if (!expectedType.isInstance(flagValue)) {
throw new TypeMismatchError(message);
}
return flagValue;
return (T) flagValue;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,26 @@ void tearDown() throws IOException {
mockFlagsmithErrorServer.shutdown();
}

@ParameterizedTest
@MethodSource("convertValueArguments")
void testConvertValue(Object input, Class<?> expectedType, Object expected) throws Exception {
var method = flagsmithProvider.getClass().getDeclaredMethod("convertValue", Object.class, Class.class);
method.setAccessible(true);
Object result = method.invoke(flagsmithProvider, input, expectedType);
assertEquals(expected, result);
}

private static Stream<Arguments> convertValueArguments() {
return Stream.of(
Arguments.of(true, Boolean.class, true),
Arguments.of("test", String.class, "test"),
Arguments.of(123, Integer.class, 123),
Arguments.of("123", Integer.class, 123),
Arguments.of(3.14, Double.class, 3.14),
Arguments.of("3.14", Double.class, 3.14)
);
}

@Test
void shouldInitializeProviderWhenAllOptionsSet() {
HashMap<String, String> headers = new HashMap<String, String>() {
Expand Down
Loading