Skip to content

Commit

Permalink
Unchecked cast was removed
Browse files Browse the repository at this point in the history
  • Loading branch information
Bohdan-Kim committed Jan 3, 2025
1 parent e578aa6 commit 3911b5b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -185,16 +185,27 @@ class GrowthBookSDK() : FeaturesFlowDelegate {
}

/**
* The feature method takes a single string argument,
* which is the unique identifier, and the type of the accessed feature
* The feature method takes a string argument,
* which is the unique identifier, and the type of the accessed feature.
* The supported types of accessed features are:
* [Boolean], [String], [Number], [Short],
* [Int], [Long], [Float], [Double], [GBJson]
*
* @returns a feature value typed with specified type
*/
fun <V>feature(id: String): V? {
val gbFeatureResult = feature(id)
val resultValue = gbFeatureResult.value
inline fun <reified V>feature(id: String): V? {
val listOfSupportedTypes = listOf(
Boolean::class, String::class,
Number::class, Short::class, Int::class,
Long::class, Float::class, Double::class,
GBJson::class,
)
if (V::class !in listOfSupportedTypes) {
return null
}

@Suppress("UNCHECKED_CAST")
return when(resultValue) {
val gbFeatureResult = feature(id)
return when(val resultValue = gbFeatureResult.value) {
is GBBoolean -> resultValue.value as? V
is GBString -> resultValue.value as? V
is GBNumber -> resultValue.value as? V
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class GBFeatureValueTests {

for (item in evalConditions) {
if (item is JsonArray) {
sdk.feature<Any>(id = item[2].jsonPrimitive.content)
sdk.feature(id = item[2].jsonPrimitive.content)
break
}
}
Expand Down

0 comments on commit 3911b5b

Please sign in to comment.