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 1
1
package com.segment.analytics.kotlin.core
2
2
3
+ import com.segment.analytics.kotlin.core.platform.DestinationPlugin
3
4
import com.segment.analytics.kotlin.core.platform.EventPlugin
4
5
import com.segment.analytics.kotlin.core.platform.Plugin
5
6
import com.segment.analytics.kotlin.core.platform.Timeline
@@ -405,6 +406,12 @@ open class Analytics protected constructor(
405
406
*/
406
407
fun <T : Plugin > find (plugin : KClass <T >): T ? = this .timeline.find(plugin)
407
408
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
+
408
415
/* *
409
416
* Retrieve the first match of registered plugin. It finds
410
417
* 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
7
7
import com.segment.analytics.kotlin.core.Storage
8
8
import com.segment.analytics.kotlin.core.Traits
9
9
import com.segment.analytics.kotlin.core.emptyJsonObject
10
+ import com.segment.analytics.kotlin.core.platform.DestinationPlugin
10
11
import com.segment.analytics.kotlin.core.platform.Plugin
11
12
import kotlinx.coroutines.CoroutineScope
12
13
import kotlinx.serialization.json.JsonObject
@@ -200,6 +201,20 @@ class JavaAnalytics private constructor() {
200
201
*/
201
202
fun <T : Plugin > find (plugin : Class <T >) = analytics.find(plugin.kotlin)
202
203
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
+
203
218
/* *
204
219
* Remove a plugin from the analytics timeline using its name
205
220
* @param plugin [Plugin] to be remove
Original file line number Diff line number Diff line change @@ -91,6 +91,13 @@ internal class Timeline {
91
91
return null
92
92
}
93
93
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
+
94
101
fun <T : Plugin > findAll (pluginClass : KClass <T >): List <T > {
95
102
val result = mutableListOf<T >()
96
103
plugins.forEach { (_, list) ->
Original file line number Diff line number Diff line change 1
1
package com.segment.analytics.kotlin.core
2
2
3
+ import com.segment.analytics.kotlin.core.platform.DestinationPlugin
3
4
import com.segment.analytics.kotlin.core.platform.Plugin
4
5
import com.segment.analytics.kotlin.core.platform.plugins.ContextPlugin
5
6
import com.segment.analytics.kotlin.core.utils.*
@@ -428,6 +429,19 @@ class AnalyticsTests {
428
429
assertTrue(plugins.contains(parent))
429
430
assertTrue(plugins.contains(child))
430
431
}
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
+ }
431
445
}
432
446
}
433
447
You can’t perform that action at this time.
0 commit comments