Skip to content

Commit e75afb2

Browse files
authored
Tighten up Codable compile time assistance (#202)
* Tighten up Codable compile time conformance * Fixed ios/tvos typecheck issue * Removed console logger * Example project fixes * more project fixes * Remove dest example build
1 parent 7042b47 commit e75afb2

File tree

13 files changed

+21
-114
lines changed

13 files changed

+21
-114
lines changed

.github/workflows/swift.yml

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -117,20 +117,4 @@ jobs:
117117
run: |
118118
cd Examples/apps/SegmentUIKitExample
119119
xcodebuild -workspace "SegmentUIKitExample.xcworkspace" -scheme "SegmentUIKitExample" -destination 'platform=macOS,variant=Mac Catalyst'
120-
121-
122-
build_and_test_dest_examples:
123-
needs: cancel_previous
124-
runs-on: macos-11
125-
steps:
126-
- uses: maxim-lobanov/setup-xcode@v1
127-
with:
128-
xcode-version: latest-stable
129-
- uses: actions/checkout@v2
130-
- uses: webfactory/[email protected]
131-
with:
132-
ssh-private-key: ${{ secrets.SOVRAN_SSH_KEY }}
133-
- name: build for ios simulator
134-
run: |
135-
cd Examples/apps/DestinationsExample
136-
xcodebuild -workspace "DestinationsExample.xcworkspace" -scheme "DestinationsExample" -sdk iphonesimulator -destination 'platform=iOS Simulator,name=iPhone 11,OS=14.0'
120+

Examples/apps/BasicExample/BasicExample.xcodeproj/project.pbxproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10-
465879BD268A511600180335 /* ConsoleLogger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 465879BC268A511600180335 /* ConsoleLogger.swift */; };
1110
469F7AF9265C25890038E773 /* EventData.swift in Sources */ = {isa = PBXBuildFile; fileRef = 469F7AF8265C25890038E773 /* EventData.swift */; };
1211
46E38365265837EA00BA2502 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46E38364265837EA00BA2502 /* AppDelegate.swift */; };
1312
46E38367265837EA00BA2502 /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46E38366265837EA00BA2502 /* SceneDelegate.swift */; };
@@ -20,7 +19,6 @@
2019
/* End PBXBuildFile section */
2120

2221
/* Begin PBXFileReference section */
23-
465879BC268A511600180335 /* ConsoleLogger.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ConsoleLogger.swift; path = ../../../other_plugins/ConsoleLogger.swift; sourceTree = "<group>"; };
2422
4663C72A267A8D6B00ADDD1A /* BasicExample.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = BasicExample.entitlements; sourceTree = "<group>"; };
2523
469F7AF8265C25890038E773 /* EventData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EventData.swift; sourceTree = "<group>"; };
2624
46E38361265837EA00BA2502 /* BasicExample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = BasicExample.app; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -68,7 +66,6 @@
6866
children = (
6967
4663C72A267A8D6B00ADDD1A /* BasicExample.entitlements */,
7068
46E38364265837EA00BA2502 /* AppDelegate.swift */,
71-
465879BC268A511600180335 /* ConsoleLogger.swift */,
7269
46E38366265837EA00BA2502 /* SceneDelegate.swift */,
7370
469F7AF8265C25890038E773 /* EventData.swift */,
7471
46E38368265837EA00BA2502 /* ViewController.swift */,
@@ -167,7 +164,6 @@
167164
46E38369265837EA00BA2502 /* ViewController.swift in Sources */,
168165
46E38365265837EA00BA2502 /* AppDelegate.swift in Sources */,
169166
469F7AF9265C25890038E773 /* EventData.swift in Sources */,
170-
465879BD268A511600180335 /* ConsoleLogger.swift in Sources */,
171167
46E38367265837EA00BA2502 /* SceneDelegate.swift in Sources */,
172168
);
173169
runOnlyForDeploymentPostprocessing = 0;

Examples/apps/BasicExample/BasicExample/AppDelegate.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
2121
.flushInterval(10)
2222

2323
analytics = Analytics(configuration: configuration)
24-
analytics?.add(plugin: ConsoleLogger(name: "consoleLogger"))
2524

2625
return true
2726
}

Examples/apps/DestinationsExample/DestinationsExample.xcodeproj/project.pbxproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
46871695270E16080028B595 /* UIKitScreenTracking.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4687168F270E16080028B595 /* UIKitScreenTracking.swift */; };
1212
46871696270E16080028B595 /* ConsentTracking.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46871690270E16080028B595 /* ConsentTracking.swift */; };
1313
46871697270E16080028B595 /* IDFACollection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46871691270E16080028B595 /* IDFACollection.swift */; };
14-
46871698270E16080028B595 /* ConsoleLogger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46871692270E16080028B595 /* ConsoleLogger.swift */; };
1514
468DE06527D28C6500F2D94B /* SegmentAmplitude in Frameworks */ = {isa = PBXBuildFile; productRef = 468DE06427D28C6500F2D94B /* SegmentAmplitude */; };
1615
468DE06827D28C7E00F2D94B /* SegmentAppsFlyer in Frameworks */ = {isa = PBXBuildFile; productRef = 468DE06727D28C7E00F2D94B /* SegmentAppsFlyer */; };
1716
468DE06B27D28CB200F2D94B /* SegmentFacebook in Frameworks */ = {isa = PBXBuildFile; productRef = 468DE06A27D28CB200F2D94B /* SegmentFacebook */; };
@@ -34,7 +33,6 @@
3433
4687168F270E16080028B595 /* UIKitScreenTracking.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIKitScreenTracking.swift; sourceTree = "<group>"; };
3534
46871690270E16080028B595 /* ConsentTracking.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ConsentTracking.swift; sourceTree = "<group>"; };
3635
46871691270E16080028B595 /* IDFACollection.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = IDFACollection.swift; sourceTree = "<group>"; };
37-
46871692270E16080028B595 /* ConsoleLogger.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ConsoleLogger.swift; sourceTree = "<group>"; };
3836
469EC8CF266066130068F9E3 /* SystemConfiguration.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SystemConfiguration.framework; path = System/Library/Frameworks/SystemConfiguration.framework; sourceTree = SDKROOT; };
3937
469EC8E1266828AF0068F9E3 /* DestinationsExample-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "DestinationsExample-Bridging-Header.h"; sourceTree = "<group>"; };
4038
469F7B04266011690038E773 /* DestinationsExample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = DestinationsExample.app; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -75,7 +73,6 @@
7573
4687168F270E16080028B595 /* UIKitScreenTracking.swift */,
7674
46871690270E16080028B595 /* ConsentTracking.swift */,
7775
46871691270E16080028B595 /* IDFACollection.swift */,
78-
46871692270E16080028B595 /* ConsoleLogger.swift */,
7976
);
8077
name = other_plugins;
8178
path = ../../../other_plugins;
@@ -220,7 +217,6 @@
220217
isa = PBXSourcesBuildPhase;
221218
buildActionMask = 2147483647;
222219
files = (
223-
46871698270E16080028B595 /* ConsoleLogger.swift in Sources */,
224220
469F7B0C266011690038E773 /* ViewController.swift in Sources */,
225221
469F7B08266011690038E773 /* AppDelegate.swift in Sources */,
226222
46871694270E16080028B595 /* NotificationTracking.swift in Sources */,

Examples/apps/SegmentSwiftUIExample/SegmentSwiftUIExample/ContentView.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ struct ContentView: View {
2626
HStack {
2727
Button(action: {
2828
Analytics.main.group(groupId: "12345-Group")
29-
Analytics.main.log(message: "Started group")
3029
}, label: {
3130
Text("Group")
3231
}).padding(6)

Examples/apps/SegmentUIKitExample/SegmentUIKitExample.xcodeproj/project.pbxproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
46E3834C26582D9E00BA2502 /* UIKitScreenTracking.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46E3834826582D9E00BA2502 /* UIKitScreenTracking.swift */; };
1818
46E3834D26582D9E00BA2502 /* ConsentTracking.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46E3834926582D9E00BA2502 /* ConsentTracking.swift */; };
1919
46E3834E26582D9E00BA2502 /* IDFACollection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46E3834A26582D9E00BA2502 /* IDFACollection.swift */; };
20-
46E3834F26582D9E00BA2502 /* ConsoleLogger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46E3834B26582D9E00BA2502 /* ConsoleLogger.swift */; };
2120
46E3835326582DA400BA2502 /* CustomScreenTracking.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46E3835126582DA400BA2502 /* CustomScreenTracking.swift */; };
2221
46E3835426582DA400BA2502 /* MultiInstance.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46E3835226582DA400BA2502 /* MultiInstance.swift */; };
2322
46E383572658307800BA2502 /* Segment in Frameworks */ = {isa = PBXBuildFile; productRef = 46E383562658307800BA2502 /* Segment */; };
@@ -39,7 +38,6 @@
3938
46E3834826582D9E00BA2502 /* UIKitScreenTracking.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIKitScreenTracking.swift; sourceTree = "<group>"; };
4039
46E3834926582D9E00BA2502 /* ConsentTracking.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ConsentTracking.swift; sourceTree = "<group>"; };
4140
46E3834A26582D9E00BA2502 /* IDFACollection.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = IDFACollection.swift; sourceTree = "<group>"; };
42-
46E3834B26582D9E00BA2502 /* ConsoleLogger.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ConsoleLogger.swift; sourceTree = "<group>"; };
4341
46E3835126582DA400BA2502 /* CustomScreenTracking.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CustomScreenTracking.swift; sourceTree = "<group>"; };
4442
46E3835226582DA400BA2502 /* MultiInstance.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MultiInstance.swift; sourceTree = "<group>"; };
4543
46E73DA226F531320021042C /* NotificationTracking.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationTracking.swift; sourceTree = "<group>"; };
@@ -100,7 +98,6 @@
10098
46E73DA226F531320021042C /* NotificationTracking.swift */,
10199
46E3834926582D9E00BA2502 /* ConsentTracking.swift */,
102100
46E3834A26582D9E00BA2502 /* IDFACollection.swift */,
103-
46E3834B26582D9E00BA2502 /* ConsoleLogger.swift */,
104101
);
105102
name = other_plugins;
106103
path = ../../other_plugins;
@@ -200,7 +197,6 @@
200197
buildActionMask = 2147483647;
201198
files = (
202199
46E3835326582DA400BA2502 /* CustomScreenTracking.swift in Sources */,
203-
46E3834F26582D9E00BA2502 /* ConsoleLogger.swift in Sources */,
204200
46022789261F860100A9E913 /* Tab1ViewController.swift in Sources */,
205201
46022785261F860100A9E913 /* AppDelegate.swift in Sources */,
206202
46E3834C26582D9E00BA2502 /* UIKitScreenTracking.swift in Sources */,

Examples/apps/SegmentUIKitExample/SegmentUIKitExample/AppDelegate.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
1313

1414
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
1515
// add console logging plugins to our multiple instances
16-
Analytics.main.add(plugin: ConsoleLogger(name: "main"))
1716
Analytics.main.add(plugin: ConsentTracking())
1817
Analytics.main.add(plugin: IDFACollection())
1918
Analytics.main.add(plugin: UIKitScreenTracking())
@@ -23,7 +22,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
2322
Analytics.main.add(plugin: CellularCarrier())
2423
#endif
2524

26-
Analytics.support.add(plugin: ConsoleLogger(name: "support"))
2725
Analytics.support.add(plugin: ConsentTracking())
2826

2927
Analytics.support.track(name: "test event")

Examples/other_plugins/ConsoleLogger.swift

Lines changed: 0 additions & 68 deletions
This file was deleted.

Examples/other_plugins/NotificationTracking.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class NotificationTracking: Plugin {
4242
var type: PluginType = .utility
4343
weak var analytics: Analytics?
4444

45-
func trackNotification(_ properties: [String: Any], fromLaunch launch: Bool) {
45+
func trackNotification(_ properties: [String: Codable], fromLaunch launch: Bool) {
4646
if launch {
4747
analytics?.track(name: "Push Notification Tapped", properties: properties)
4848
} else {
@@ -55,7 +55,7 @@ class NotificationTracking: Plugin {
5555
// determination if a push notification caused the app to open.
5656
extension NotificationTracking: RemoteNotifications {
5757
func receivedRemoteNotification(userInfo: [AnyHashable: Any]) {
58-
if let notification = userInfo as? [String: Any] {
58+
if let notification = userInfo as? [String: Codable] {
5959
trackNotification(notification, fromLaunch: false)
6060
}
6161
}
@@ -88,7 +88,7 @@ import UIKit
8888

8989
extension NotificationTracking: iOSLifecycle {
9090
func application(_ application: UIApplication?, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) {
91-
if let notification = launchOptions?[UIApplication.LaunchOptionsKey.remoteNotification] as? [String: Any] {
91+
if let notification = launchOptions?[UIApplication.LaunchOptionsKey.remoteNotification] as? [String: Codable] {
9292
trackNotification(notification, fromLaunch: true)
9393
}
9494
}

Sources/Segment/Events.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ extension Analytics {
142142
/// generate the UUID and Apple's policies on IDs, see
143143
/// https://segment.io/libraries/ios#ids
144144
/// - properties: A dictionary of traits you know about the user. Things like: email, name, plan, etc.
145-
public func track(name: String, properties: [String: Any]? = nil) {
145+
public func track(name: String, properties: [String: Codable]? = nil) {
146146
var props: JSON? = nil
147147
if let properties = properties {
148148
do {
@@ -165,7 +165,7 @@ extension Analytics {
165165
/// https://segment.io/libraries/ios#ids
166166
/// - traits: A dictionary of traits you know about the user. Things like: email, name, plan, etc.
167167
/// In the case when user logs out, make sure to call ``reset()`` to clear user's identity info.
168-
public func identify(userId: String, traits: [String: Any]? = nil) {
168+
public func identify(userId: String, traits: [String: Codable]? = nil) {
169169
do {
170170
if let traits = traits {
171171
let traits = try JSON(traits as Any)
@@ -187,7 +187,7 @@ extension Analytics {
187187
/// - screenTitle: The title of the screen being tracked.
188188
/// - category: A category to the type of screen if it applies.
189189
/// - properties: Any extra metadata associated with the screen. e.g. method of access, size, etc.
190-
public func screen(title: String, category: String? = nil, properties: [String: Any]? = nil) {
190+
public func screen(title: String, category: String? = nil, properties: [String: Codable]? = nil) {
191191
var event = ScreenEvent(title: title, category: category, properties: nil)
192192
if let properties = properties {
193193
do {
@@ -204,7 +204,7 @@ extension Analytics {
204204
/// - Parameters:
205205
/// - groupId: A unique identifier for the group identification in your system.
206206
/// - traits: Traits of the group you may be interested in such as email, phone or name.
207-
public func group(groupId: String, traits: [String: Any]?) {
207+
public func group(groupId: String, traits: [String: Codable]?) {
208208
var event = GroupEvent(groupId: groupId)
209209
if let traits = traits {
210210
do {

Sources/Segment/ObjC/ObjCAnalytics.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ extension ObjCAnalytics {
4141

4242
@objc(track:properties:)
4343
public func track(name: String, properties: [String: Any]?) {
44-
analytics.track(name: name, properties: properties)
44+
analytics.track(name: name, properties: properties as? [String: Codable])
4545
}
4646

4747
/// Associate a user with their unique ID and record traits about them.
@@ -67,7 +67,7 @@ extension ObjCAnalytics {
6767
if let userId = userId {
6868
// at first glance this looks like recursion. It's actually calling
6969
// into the swift version of this call where userId is NOT optional.
70-
analytics.identify(userId: userId, traits: traits)
70+
analytics.identify(userId: userId, traits: codable(traits))
7171
} else if let traits = try? JSON(traits as Any) {
7272
analytics.store.dispatch(action: UserInfo.SetTraitsAction(traits: traits))
7373
let userInfo: UserInfo? = analytics.store.currentState()
@@ -100,7 +100,7 @@ extension ObjCAnalytics {
100100
/// - properties: Any extra metadata associated with the screen. e.g. method of access, size, etc.
101101
@objc(screen:category:properties:)
102102
public func screen(title: String, category: String?, properties: [String: Any]?) {
103-
analytics.screen(title: title, category: category, properties: properties)
103+
analytics.screen(title: title, category: category, properties: codable(properties))
104104
}
105105

106106
/// Associate a user with a group such as a company, organization, project, etc.
@@ -117,7 +117,7 @@ extension ObjCAnalytics {
117117
/// - traits: Traits of the group you may be interested in such as email, phone or name.
118118
@objc(group:traits:)
119119
public func group(groupId: String, traits: [String: Any]?) {
120-
analytics.group(groupId: groupId, traits: traits)
120+
analytics.group(groupId: groupId, traits: codable(traits))
121121
}
122122

123123
@objc(alias:)

Sources/Segment/Plugins/Platforms/iOS/iOSLifecycleEvents.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,15 @@ class iOSLifecycleEvents: PlatformPlugin, iOSLifecycle {
5454
])
5555
}
5656

57+
let sourceApp: String? = launchOptions?[UIApplication.LaunchOptionsKey.sourceApplication] as? String ?? ""
58+
let url: String? = launchOptions?[UIApplication.LaunchOptionsKey.url] as? String ?? ""
59+
5760
analytics?.track(name: "Application Opened", properties: [
5861
"from_background": false,
5962
"version": currentVersion ?? "",
6063
"build": currentBuild ?? "",
61-
"referring_application": launchOptions?[UIApplication.LaunchOptionsKey.sourceApplication] ?? "",
62-
"url": launchOptions?[UIApplication.LaunchOptionsKey.url] ?? ""
64+
"referring_application": sourceApp,
65+
"url": url
6366
])
6467

6568
UserDefaults.standard.setValue(currentVersion, forKey: Self.versionKey)

Sources/Segment/Utilities/Utils.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,7 @@ extension Optional: Flattenable {
5757
}
5858
}
5959
}
60+
61+
internal func codable(_ dict: [String: Any]?) -> [String: Codable]? {
62+
return dict as? [String: Codable]
63+
}

0 commit comments

Comments
 (0)