Skip to content

Commit 512c6d9

Browse files
authored
Add support for closure based enrichments (#208)
1 parent 2c80798 commit 512c6d9

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

Sources/Segment/Plugins.swift

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,22 @@ public protocol VersionedPlugin {
6565
// For internal platform-specific bits
6666
internal protocol PlatformPlugin: Plugin { }
6767

68+
public typealias EnrichmentClosure = (_ event: RawEvent?) -> RawEvent?
69+
public class ClosureEnrichment: Plugin {
70+
public var type: PluginType = .enrichment
71+
public var analytics: Analytics? = nil
72+
73+
internal let closure: EnrichmentClosure
74+
75+
init(closure: @escaping EnrichmentClosure) {
76+
self.closure = closure
77+
}
78+
79+
public func execute<T: RawEvent>(event: T?) -> T? {
80+
return closure(event) as? T
81+
}
82+
}
83+
6884

6985
// MARK: - Plugin instance helpers
7086
extension Plugin {
@@ -98,7 +114,7 @@ extension DestinationPlugin {
98114
Adds a new plugin to the currently loaded set.
99115

100116
- Parameter plugin: The plugin to be added.
101-
- Returns: Returns the name of the supplied plugin.
117+
- Returns: Returns the supplied plugin.
102118

103119
*/
104120
@discardableResult
@@ -110,6 +126,23 @@ extension DestinationPlugin {
110126
return plugin
111127
}
112128

129+
/**
130+
Adds a new enrichment to the currently loaded set of plugins.
131+
132+
- Parameter enrichment: The enrichment closure to be added.
133+
- Returns: Returns the the generated plugin.
134+
135+
*/
136+
@discardableResult
137+
public func add(enrichment: @escaping EnrichmentClosure) -> Plugin {
138+
let plugin = ClosureEnrichment(closure: enrichment)
139+
if let analytics = self.analytics {
140+
plugin.configure(analytics: analytics)
141+
}
142+
timeline.add(plugin: plugin)
143+
return plugin
144+
}
145+
113146
/**
114147
Removes and unloads plugins with a matching name from the system.
115148

@@ -147,6 +180,21 @@ extension Analytics {
147180
return plugin
148181
}
149182

183+
/**
184+
Adds a new enrichment to the currently loaded set of plugins.
185+
186+
- Parameter enrichment: The enrichment closure to be added.
187+
- Returns: Returns the the generated plugin.
188+
189+
*/
190+
@discardableResult
191+
public func add(enrichment: @escaping EnrichmentClosure) -> Plugin {
192+
let plugin = ClosureEnrichment(closure: enrichment)
193+
plugin.configure(analytics: self)
194+
timeline.add(plugin: plugin)
195+
return plugin
196+
}
197+
150198
/**
151199
Removes and unloads plugins with a matching name from the system.
152200

Tests/Segment-Tests/Analytics_Tests.swift

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,4 +566,43 @@ final class Analytics_Tests: XCTestCase {
566566

567567
RunLoop.main.run(until: Date(timeIntervalSinceNow: 5))
568568
}
569+
570+
func testEnrichment() {
571+
var sourceHit: Bool = false
572+
let sourceEnrichment: EnrichmentClosure = { event in
573+
print("source enrichment applied")
574+
sourceHit = true
575+
return event
576+
}
577+
578+
var destHit: Bool = true
579+
let destEnrichment: EnrichmentClosure = { event in
580+
print("destination enrichment applied")
581+
destHit = true
582+
return event
583+
}
584+
585+
let config = Configuration(writeKey: "testEnrichments")
586+
let analytics = Analytics(configuration: config)
587+
analytics.storage.hardReset(doYouKnowHowToUseThis: true)
588+
let outputReader = OutputReaderPlugin()
589+
analytics.add(plugin: outputReader)
590+
591+
analytics.add(enrichment: sourceEnrichment)
592+
593+
let segment = analytics.find(pluginType: SegmentDestination.self)
594+
segment?.add(enrichment: destEnrichment)
595+
596+
waitUntilStarted(analytics: analytics)
597+
598+
analytics.track(name: "something")
599+
600+
analytics.flush()
601+
602+
RunLoop.main.run(until: Date(timeIntervalSinceNow: 5))
603+
604+
XCTAssertTrue(sourceHit)
605+
XCTAssertTrue(destHit)
606+
607+
}
569608
}

0 commit comments

Comments
 (0)