Skip to content

Commit 1510073

Browse files
authored
find destination plugin by name (#86)
* add find destination plugin by name * update parameter name
1 parent 6c96588 commit 1510073

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed

core/src/main/java/com/segment/analytics/kotlin/core/Analytics.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.segment.analytics.kotlin.core
22

3+
import com.segment.analytics.kotlin.core.platform.DestinationPlugin
34
import com.segment.analytics.kotlin.core.platform.EventPlugin
45
import com.segment.analytics.kotlin.core.platform.Plugin
56
import 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

core/src/main/java/com/segment/analytics/kotlin/core/compat/JavaAnalytics.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import com.segment.analytics.kotlin.core.Properties
77
import com.segment.analytics.kotlin.core.Storage
88
import com.segment.analytics.kotlin.core.Traits
99
import com.segment.analytics.kotlin.core.emptyJsonObject
10+
import com.segment.analytics.kotlin.core.platform.DestinationPlugin
1011
import com.segment.analytics.kotlin.core.platform.Plugin
1112
import kotlinx.coroutines.CoroutineScope
1213
import 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

core/src/main/java/com/segment/analytics/kotlin/core/platform/Timeline.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff 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) ->

core/src/test/kotlin/com/segment/analytics/kotlin/core/AnalyticsTests.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.segment.analytics.kotlin.core
22

3+
import com.segment.analytics.kotlin.core.platform.DestinationPlugin
34
import com.segment.analytics.kotlin.core.platform.Plugin
45
import com.segment.analytics.kotlin.core.platform.plugins.ContextPlugin
56
import 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

0 commit comments

Comments
 (0)