Skip to content

Commit 3bd6a19

Browse files
authored
Fix _metadata population (#114)
* fix metadata logic * optimize building of sets * more optimizations
1 parent 802364b commit 3bd6a19

File tree

2 files changed

+67
-4
lines changed

2 files changed

+67
-4
lines changed

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,31 @@ class DestinationMetadataPlugin : Plugin {
3131
?.filter { it.enabled && it !is SegmentDestination }
3232
val metadata = DestinationMetadata().apply {
3333
// Mark all loaded destinations as bundled
34-
this.bundled = enabledDestinations?.map { it.key }
34+
val bundled = buildSet { enabledDestinations?.forEach { add(it.key) } }
3535

36+
// All active integrations, not in `bundled` are put in `unbundled`
3637
// All unbundledIntegrations not in `bundled` are put in `unbundled`
37-
this.unbundled = analyticsSettings.integrations["Segment.io"]?.safeJsonObject
38-
?.get("unbundledIntegrations")?.safeJsonArray?.map { (it as JsonPrimitive).content }
39-
?.filter { !(bundled?.contains(it) ?: false) }
38+
val unbundled = buildSet {
39+
analyticsSettings.integrations.keys.forEach {
40+
if (it != "Segment.io" && !bundled.contains(it)) {
41+
add(it)
42+
}
43+
}
44+
45+
analyticsSettings.integrations["Segment.io"]?.safeJsonObject
46+
?.get("unbundledIntegrations")?.safeJsonArray
47+
?.forEach {
48+
val content = (it as JsonPrimitive).content
49+
if (!bundled.contains(content)) {
50+
add(content)
51+
}
52+
}
53+
}
4054

4155
// `bundledIds` for mobile is empty
4256
this.bundledIds = emptyList()
57+
this.bundled = bundled.toList()
58+
this.unbundled = unbundled.toList()
4359
}
4460

4561
val payload = event.copy<BaseEvent>().apply {

core/src/test/kotlin/com/segment/analytics/kotlin/core/platform/plugins/DestinationMetadataPluginTests.kt

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,51 @@ class DestinationMetadataPluginTests {
109109
assertEquals(expected, actual)
110110
assertEquals(emptyJsonObject, actual.integrations)
111111
}
112+
113+
@Test
114+
fun `plugin respects both integrations list and unbundled list`() {
115+
val trackEvent = TrackEvent(
116+
event = "clicked",
117+
properties = buildJsonObject { put("behaviour", "good") })
118+
.apply {
119+
messageId = "qwerty-1234"
120+
anonymousId = "anonId"
121+
integrations = emptyJsonObject
122+
context = emptyJsonObject
123+
timestamp = epochTimestamp
124+
_metadata = DestinationMetadata()
125+
}
126+
val mixpanelDest = object : DestinationPlugin() {
127+
override val key: String = "Mixpanel"
128+
}
129+
analytics.add(mixpanelDest)
130+
analytics.manuallyEnableDestination(mixpanelDest)
131+
plugin.update(Settings(
132+
integrations = buildJsonObject {
133+
put("Segment.io", buildJsonObject {
134+
put("apiKey", "123")
135+
put("apiHost", "api.segment.io/v1")
136+
put("unbundledIntegrations", buildJsonArray {
137+
add("Customer.io")
138+
add("Mixpanel")
139+
add("Amplitude")
140+
add("dest1")
141+
})
142+
})
143+
put("dest1", true)
144+
put("dest2", true)
145+
}
146+
), Plugin.UpdateType.Initial)
147+
148+
val expected = trackEvent.copy<TrackEvent>().apply {
149+
_metadata = DestinationMetadata(
150+
unbundled = listOf("dest1", "dest2", "Customer.io","Amplitude"),
151+
bundled = listOf("Mixpanel"),
152+
bundledIds = emptyList()
153+
)
154+
}
155+
val actual = plugin.execute(trackEvent)
156+
assertEquals(expected, actual)
157+
assertEquals(emptyJsonObject, actual.integrations)
158+
}
112159
}

0 commit comments

Comments
 (0)