From 4c8b3b7d505b271fa6b1d5fcec13fedef9f8eb02 Mon Sep 17 00:00:00 2001 From: Didier Garcia Date: Mon, 20 Nov 2023 22:37:46 -0500 Subject: [PATCH] Adding a findAll(KClass) function to DestinationPlugins. This matches the API already present in Analytics. (#199) --- .../analytics/kotlin/core/platform/Plugin.kt | 8 +++ .../plugins/DestinationPluginTests.kt | 50 +++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/core/src/main/java/com/segment/analytics/kotlin/core/platform/Plugin.kt b/core/src/main/java/com/segment/analytics/kotlin/core/platform/Plugin.kt index 8df20b84..bdfa7cdd 100644 --- a/core/src/main/java/com/segment/analytics/kotlin/core/platform/Plugin.kt +++ b/core/src/main/java/com/segment/analytics/kotlin/core/platform/Plugin.kt @@ -9,6 +9,7 @@ import com.segment.analytics.kotlin.core.ScreenEvent import com.segment.analytics.kotlin.core.Settings import com.segment.analytics.kotlin.core.TrackEvent import com.segment.analytics.kotlin.core.utilities.getBoolean +import kotlin.reflect.KClass // Most simple interface for an plugin interface Plugin { @@ -105,6 +106,13 @@ abstract class DestinationPlugin : EventPlugin { timeline.remove(plugin) } + /** + * Find all Plugins matching the given class that have been added to this Destination Plugin. + */ + fun findAll(pluginClass: KClass): List { + return timeline.findAll(pluginClass) + } + /** * Update `enabled` state of destination and apply settings update to destination timeline * We recommend calling `super.update(..., ...) in case this function is overridden diff --git a/core/src/test/kotlin/com/segment/analytics/kotlin/core/platform/plugins/DestinationPluginTests.kt b/core/src/test/kotlin/com/segment/analytics/kotlin/core/platform/plugins/DestinationPluginTests.kt index c1e8e3b2..133b3255 100644 --- a/core/src/test/kotlin/com/segment/analytics/kotlin/core/platform/plugins/DestinationPluginTests.kt +++ b/core/src/test/kotlin/com/segment/analytics/kotlin/core/platform/plugins/DestinationPluginTests.kt @@ -180,4 +180,54 @@ class DestinationPluginTests { val result = destinationPlugin.process(trackEvent) assertEquals(null, result) } + + @Test + fun `findAll will find single added plugin`() { + val destinationPlugin = object: DestinationPlugin() { + override val key: String = "TestDestination" + } + + timeline.add(destinationPlugin) + var simplePlugins = destinationPlugin.findAll(SimplePlugin::class) + assertTrue(simplePlugins.isEmpty()) + + val testPlugin = SimplePlugin("Test1") + destinationPlugin.add(testPlugin) + simplePlugins = destinationPlugin.findAll(SimplePlugin::class) + + assertTrue(simplePlugins.isNotEmpty()) + assertEquals(1, simplePlugins.size) + assertEquals(testPlugin, simplePlugins.get(0)) + } + + @Test + fun `findAll will find multiple added plugin`() { + val destinationPlugin = object: DestinationPlugin() { + override val key: String = "TestDestination" + } + + timeline.add(destinationPlugin) + var simplePlugins = destinationPlugin.findAll(SimplePlugin::class) + assertTrue(simplePlugins.isEmpty()) + + val testPlugin1 = SimplePlugin("Test1") + val testPlugin2 = SimplePlugin("Test2") + val testPlugin3 = SimplePlugin("Test3") + + destinationPlugin.add(testPlugin1) + destinationPlugin.add(testPlugin2) + destinationPlugin.add(testPlugin3) + simplePlugins = destinationPlugin.findAll(SimplePlugin::class) + + assertTrue(simplePlugins.isNotEmpty()) + assertEquals(3, simplePlugins.size) + assertEquals(testPlugin1, simplePlugins.get(0)) + assertEquals(testPlugin2, simplePlugins.get(1)) + assertEquals(testPlugin3, simplePlugins.get(2)) + } + + class SimplePlugin(var name: String) : Plugin { + override val type: Plugin.Type = Plugin.Type.Enrichment + override lateinit var analytics: Analytics + } } \ No newline at end of file