Skip to content

Commit

Permalink
Adding a findAll(KClass) function to DestinationPlugins. This matches…
Browse files Browse the repository at this point in the history
… the API already present in Analytics. (#199)
  • Loading branch information
didiergarcia authored Nov 21, 2023
1 parent 3a65ff2 commit 4c8b3b7
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 <T: Plugin> findAll(pluginClass: KClass<T>): List<T> {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

0 comments on commit 4c8b3b7

Please sign in to comment.