Skip to content

Commit

Permalink
feature() method to get evaluateFeature() method result "as it is" wa…
Browse files Browse the repository at this point in the history
…s added
  • Loading branch information
Bohdan-Kim committed Dec 31, 2024
1 parent a77de00 commit e578aa6
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,39 +173,43 @@ class GrowthBookSDK() : FeaturesFlowDelegate {

/**
* The feature method takes a single string argument,
* which is the unique identifier for the feature and returns a FeatureResult object.
* which is the unique identifier for the feature and
* @returns a [GBFeatureResult] object
*/
fun <V>feature(id: String): GBFeatureResult<V> {
fun feature(id: String): GBFeatureResult<GBValue> {
val evaluator = GBFeatureEvaluator(gbContext, this.forcedFeatures)
val gbFeatureResult = evaluator.evaluateFeature(
return evaluator.evaluateFeature(
featureKey = id,
attributeOverrides = attributeOverrides,
)
}

/**
* The feature method takes a single string argument,
* which is the unique identifier, and the type of the accessed feature
* @returns a feature value typed with specified type
*/
fun <V>feature(id: String): V? {
val gbFeatureResult = feature(id)
val resultValue = gbFeatureResult.value

@Suppress("UNCHECKED_CAST")
return GBFeatureResult(
value = when(gbFeatureResult.value) {
is GBBoolean -> gbFeatureResult.value.value as? V
is GBString -> gbFeatureResult.value.value as? V
is GBNumber -> gbFeatureResult.value.value as? V
is GBJson -> gbFeatureResult.value as? V
is GBValue.Unknown -> null
null -> null
},
on = gbFeatureResult.on,
off = gbFeatureResult.off,
source = gbFeatureResult.source,
experiment = gbFeatureResult.experiment,
experimentResult = gbFeatureResult.experimentResult
)
return when(resultValue) {
is GBBoolean -> resultValue.value as? V
is GBString -> resultValue.value as? V
is GBNumber -> resultValue.value as? V
is GBJson -> resultValue as? V
is GBValue.Unknown -> null
null -> null
}
}

/**
* The isOn method takes a single string argument,
* which is the unique identifier for the feature and returns the feature state on/off
*/
fun isOn(featureId: String): Boolean {
return feature<Any>(id = featureId).on
return feature(id = featureId).on
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ internal class VerifySDKReturnFeatureValues {

// maybe we should think about Generic type of value just like in Java SDK

assertEquals(true, sdkInstance.feature<Boolean>("bool_feature_true").value)
assertEquals(false, sdkInstance.feature<Boolean>("bool_feature_false").value)
assertEquals("Default value", sdkInstance.feature<String>("string_feature").value)
assertEquals(true, sdkInstance.feature<Boolean>("bool_feature_true"))
assertEquals(false, sdkInstance.feature<Boolean>("bool_feature_false"))
assertEquals("Default value", sdkInstance.feature<String>("string_feature"))

assertEquals(888, sdkInstance.feature<Int>("number_feature").value)
assertEquals(-1, sdkInstance.feature<Int>("number_feature_negative").value)
assertEquals(888, sdkInstance.feature<Int>("number_feature"))
assertEquals(-1, sdkInstance.feature<Int>("number_feature_negative"))
}

@Test
Expand Down Expand Up @@ -83,7 +83,7 @@ internal class VerifySDKReturnFeatureValues {
val sdkInstance = buildSDK(json, attributes)
assertEquals(
expected = "Default value for brand:KZ",
actual = sdkInstance.feature<String>("string_feature").value,
actual = sdkInstance.feature<String>("string_feature"),
)
}

Expand Down Expand Up @@ -118,29 +118,29 @@ internal class VerifySDKReturnFeatureValues {
val sdkInstance = buildSDK(json = json, attributes = mapOf("user_id" to 123))
val intAttributeValue = sdkInstance.getGBContext().attributes["user_id"]?.toString() ?: ""
assertEquals("123", intAttributeValue)
val intFeature = sdkInstance.feature<Int>("test_feature")
val intFeature = sdkInstance.feature("test_feature")
assertEquals("experiment", intFeature.source.name)

//Check casting with Boolean value
sdkInstance.setAttributes(attributes = mapOf("user_id" to true))
val boolAttributeValue = sdkInstance.getGBContext().attributes["user_id"]?.toString() ?: ""
assertEquals("true", boolAttributeValue)
val boolFeature = sdkInstance.feature<Boolean>("test_feature")
val boolFeature = sdkInstance.feature("test_feature")
assertEquals("experiment", boolFeature.source.name)

//Check casting with Float value
sdkInstance.setAttributes(attributes = mapOf("user_id" to 1.8f))
val floatAttributeValue = sdkInstance.getGBContext().attributes["user_id"]?.toString() ?: ""
assertEquals("1.8", floatAttributeValue)
val floatFeature = sdkInstance.feature<Float>("test_feature")
val floatFeature = sdkInstance.feature("test_feature")
assertEquals("experiment", floatFeature.source.name)

//Checking with wrong attribute key
sdkInstance.setAttributes(attributes = mapOf("user_id" to 5))
val wrongKeyAttributeValue =
sdkInstance.getGBContext().attributes["user_iiii"]?.toString() ?: "wrongIdDefaultValue"
assertEquals("wrongIdDefaultValue", wrongKeyAttributeValue)
val wrongKeyFeature = sdkInstance.feature<Any>("test_feature")
val wrongKeyFeature = sdkInstance.feature("test_feature")
assertEquals("experiment", wrongKeyFeature.source.name)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ class GrowthBookSDKBuilderTests {
).setRefreshHandler { isRefreshed, gbError ->
}.initialize()

val featureValue = sdkInstance.feature<Any>("fwrfewrfe")
val featureValue = sdkInstance.feature("fwrfewrfe")
assertEquals(featureValue.source, GBFeatureSource.unknownFeature)

val expValue = sdkInstance.run(GBExperiment("fwewrwefw"))
Expand Down

0 comments on commit e578aa6

Please sign in to comment.