File tree Expand file tree Collapse file tree 4 files changed +43
-0
lines changed
main/java/com/segment/analytics/kotlin/core
test/kotlin/com/segment/analytics/kotlin/core Expand file tree Collapse file tree 4 files changed +43
-0
lines changed Original file line number Diff line number Diff line change 11package com.segment.analytics.kotlin.core
22
3+ import com.segment.analytics.kotlin.core.platform.DestinationPlugin
34import com.segment.analytics.kotlin.core.platform.EventPlugin
45import com.segment.analytics.kotlin.core.platform.Plugin
56import com.segment.analytics.kotlin.core.platform.Timeline
@@ -405,6 +406,12 @@ open class Analytics protected constructor(
405406 */
406407 fun <T : Plugin > find (plugin : KClass <T >): T ? = this .timeline.find(plugin)
407408
409+ /* *
410+ * Retrieve the first match of registered destination plugin by key. It finds
411+ * @param destinationKey [String]
412+ */
413+ fun find (destinationKey : String ): DestinationPlugin ? = this .timeline.find(destinationKey)
414+
408415 /* *
409416 * Retrieve the first match of registered plugin. It finds
410417 * 1. all instances of the given class/interface
Original file line number Diff line number Diff line change @@ -7,6 +7,7 @@ import com.segment.analytics.kotlin.core.Properties
77import com.segment.analytics.kotlin.core.Storage
88import com.segment.analytics.kotlin.core.Traits
99import com.segment.analytics.kotlin.core.emptyJsonObject
10+ import com.segment.analytics.kotlin.core.platform.DestinationPlugin
1011import com.segment.analytics.kotlin.core.platform.Plugin
1112import kotlinx.coroutines.CoroutineScope
1213import kotlinx.serialization.json.JsonObject
@@ -200,6 +201,20 @@ class JavaAnalytics private constructor() {
200201 */
201202 fun <T : Plugin > find (plugin : Class <T >) = analytics.find(plugin.kotlin)
202203
204+ /* *
205+ * Retrieve the first match of registered destination plugin by name. It finds
206+ * @param destination [String]
207+ */
208+ fun find (destinationKey : String ): DestinationPlugin ? = analytics.find(destinationKey)
209+
210+ /* *
211+ * Retrieve the first match of registered plugin. It finds
212+ * 1. all instances of the given class/interface
213+ * 2. and all instances of subclass of the given class/interface
214+ * @param plugin [Class]
215+ */
216+ fun <T : Plugin > findAll (plugin : Class <T >): List <T > = analytics.findAll(plugin.kotlin)
217+
203218 /* *
204219 * Remove a plugin from the analytics timeline using its name
205220 * @param plugin [Plugin] to be remove
Original file line number Diff line number Diff line change @@ -91,6 +91,13 @@ internal class Timeline {
9191 return null
9292 }
9393
94+ // Find a destination plugin by its name
95+ fun find (destination : String ) =
96+ plugins[Plugin .Type .Destination ]?.plugins?.find {
97+ it is DestinationPlugin && it.key == destination
98+ } as ? DestinationPlugin
99+
100+
94101 fun <T : Plugin > findAll (pluginClass : KClass <T >): List <T > {
95102 val result = mutableListOf<T >()
96103 plugins.forEach { (_, list) ->
Original file line number Diff line number Diff line change 11package com.segment.analytics.kotlin.core
22
3+ import com.segment.analytics.kotlin.core.platform.DestinationPlugin
34import com.segment.analytics.kotlin.core.platform.Plugin
45import com.segment.analytics.kotlin.core.platform.plugins.ContextPlugin
56import com.segment.analytics.kotlin.core.utils.*
@@ -428,6 +429,19 @@ class AnalyticsTests {
428429 assertTrue(plugins.contains(parent))
429430 assertTrue(plugins.contains(child))
430431 }
432+
433+ @Test
434+ fun `find() finds destination plugin by name` () {
435+ val dest = " testPlugin"
436+ val expected = object : DestinationPlugin () {
437+ override val key = dest
438+ }
439+ analytics.add(expected)
440+
441+ val actual = analytics.find(dest)
442+
443+ assertEquals(expected, actual)
444+ }
431445 }
432446 }
433447
You can’t perform that action at this time.
0 commit comments