@@ -180,19 +180,42 @@ private <T> ProviderEvaluation<T> buildEvaluation(
180180 * @param expectedType the type we expect for this value
181181 * @param <T> the type we want to convert to
182182 * @return A converted object
183+ * @throws TypeMismatchError if the value cannot be converted to the expected type
183184 */
185+ @ SuppressWarnings ("unchecked" )
184186 private <T > T convertValue (Object value , Class <?> expectedType ) {
185187 boolean isPrimitive = expectedType == Boolean .class
186188 || expectedType == String .class
187189 || expectedType == Integer .class
188190 || expectedType == Double .class ;
189- T flagValue = isPrimitive ? (T ) value : (T ) objectToValue (value );
190191
191- if (flagValue .getClass () != expectedType ) {
192- throw new TypeMismatchError (
193- "Flag value had an unexpected type " + flagValue .getClass () + ", expected " + expectedType + "." );
192+ Object flagValue ;
193+ if (isPrimitive ) {
194+ if (expectedType == Double .class ) {
195+ if (value instanceof Double ) {
196+ flagValue = value ;
197+ } else if (value instanceof String ) {
198+ try {
199+ flagValue = Double .parseDouble ((String ) value );
200+ } catch (NumberFormatException e ) {
201+ throw new TypeMismatchError ("Flag value string could not be parsed as Double: " + value );
202+ }
203+ } else {
204+ throw new TypeMismatchError ("Flag value had an unexpected type "
205+ + (value != null ? value .getClass () : "null" ) + ", expected " + expectedType + "." );
206+ }
207+ } else {
208+ flagValue = value ;
209+ }
210+ } else {
211+ flagValue = objectToValue (value );
212+ }
213+
214+ if (!expectedType .isInstance (flagValue )) {
215+ throw new TypeMismatchError ("Flag value had an unexpected type "
216+ + (flagValue != null ? flagValue .getClass () : "null" + ", expected " + expectedType + "." ));
194217 }
195- return flagValue ;
218+ return ( T ) flagValue ;
196219 }
197220
198221 /**
0 commit comments