Skip to content

Commit

Permalink
fix check superclasses for flag class
Browse files Browse the repository at this point in the history
  • Loading branch information
v.lantratov committed May 27, 2022
1 parent d6aafd9 commit 7ceec7e
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ class DebugDataSource(private val isEnabled: Boolean = true,
registry: FeatureFlagRegistry
): Map<String, Any> {
val flagClass = registry.getFeatureFlagsMap()[key]
return if (flagClass != null && flagClass.superclass == SimpleFeatureFlag::class.java) {
val hasSimpleFeatureFlagAsSuperclass = flagClass?.superclasses
?.any { it == SimpleFeatureFlag::class.java }
?: false
return if (flagClass != null && hasSimpleFeatureFlagAsSuperclass) {
val featureFlag = flagClass.constructors.first().newInstance() as SimpleFeatureFlag
featureFlag.isEnabled = isEnabled
mapOf(key to featureFlag)
Expand Down Expand Up @@ -159,4 +162,15 @@ class DebugDataSource(private val isEnabled: Boolean = true,
data class UpdateSimpleFlag(val key: String, val isEnabled: Boolean): FeatureFlagsUpdateEvent()
data class UpdateFromJsonString(val content: String): FeatureFlagsUpdateEvent()
}

private val Class<*>.superclasses: List<Class<*>>
get() {
val classesList = mutableListOf<Class<*>>()
var currentSuperclass = superclass
while (currentSuperclass != null) {
classesList.add(currentSuperclass)
currentSuperclass = currentSuperclass.superclass
}
return classesList
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import app.cash.turbine.test
import com.qiwi.featuretoggle.converter.JacksonFeatureFlagConverter
import com.qiwi.featuretoggle.test.flag.BOOLEAN_FEATURE_KEY
import com.qiwi.featuretoggle.test.flag.BooleanFeatureFlag
import com.qiwi.featuretoggle.test.flag.COMPLEX_BOOLEAN_FEATURE_KEY
import com.qiwi.featuretoggle.test.flag.ComplexBooleanFeatureFlag
import com.qiwi.featuretoggle.test.logger.TestLogger
import com.qiwi.featuretoggle.test.registry.TestFlagRegistry
import com.qiwi.featuretoggle.test.util.TestResources
Expand Down Expand Up @@ -59,6 +61,14 @@ class DebugDataSourceTest {
assertEquals(mapOf(BOOLEAN_FEATURE_KEY to BooleanFeatureFlag().apply {
isEnabled = true
}), awaitItem())
dataSource.updateSimpleFeatureFlag(COMPLEX_BOOLEAN_FEATURE_KEY, false)
assertEquals(mapOf(COMPLEX_BOOLEAN_FEATURE_KEY to ComplexBooleanFeatureFlag().apply {
isEnabled = false
}), awaitItem())
dataSource.updateSimpleFeatureFlag(COMPLEX_BOOLEAN_FEATURE_KEY, true)
assertEquals(mapOf(COMPLEX_BOOLEAN_FEATURE_KEY to ComplexBooleanFeatureFlag().apply {
isEnabled = true
}), awaitItem())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ import com.qiwi.featuretoggle.annotation.FeatureFlag
import com.qiwi.featuretoggle.flag.SimpleFeatureFlag

@FeatureFlag(BOOLEAN_FEATURE_KEY)
class BooleanFeatureFlag: SimpleFeatureFlag()
open class BooleanFeatureFlag: SimpleFeatureFlag()

const val BOOLEAN_FEATURE_KEY = "boolean_feature"
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Copyright (c) 2021 QIWI
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.qiwi.featuretoggle.test.flag

import com.qiwi.featuretoggle.annotation.FeatureFlag

@FeatureFlag(COMPLEX_BOOLEAN_FEATURE_KEY)
class ComplexBooleanFeatureFlag: BooleanFeatureFlag()

const val COMPLEX_BOOLEAN_FEATURE_KEY = "boolean_feature_complex"
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import com.qiwi.featuretoggle.test.flag.*
class TestFlagRegistry(
var flagsMap: Map<String, Class<*>> = mapOf(
BOOLEAN_FEATURE_KEY to BooleanFeatureFlag::class.java,
COMPLEX_BOOLEAN_FEATURE_KEY to ComplexBooleanFeatureFlag::class.java,
STRING_FEATURE_KEY to StringFeatureFlag::class.java,
DOUBLE_FEATURE_KEY to DoubleFeatureFlag::class.java,
COMPLEX_FEATURE_KEY to ComplexFeatureFlag::class.java
Expand Down

0 comments on commit 7ceec7e

Please sign in to comment.