Skip to content

Tighten up Codable compile time assistance #202

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 1 addition & 17 deletions .github/workflows/swift.yml
Original file line number Diff line number Diff line change
Expand Up @@ -117,20 +117,4 @@ jobs:
run: |
cd Examples/apps/SegmentUIKitExample
xcodebuild -workspace "SegmentUIKitExample.xcworkspace" -scheme "SegmentUIKitExample" -destination 'platform=macOS,variant=Mac Catalyst'


build_and_test_dest_examples:
needs: cancel_previous
runs-on: macos-11
steps:
- uses: maxim-lobanov/setup-xcode@v1
with:
xcode-version: latest-stable
- uses: actions/checkout@v2
- uses: webfactory/[email protected]
with:
ssh-private-key: ${{ secrets.SOVRAN_SSH_KEY }}
- name: build for ios simulator
run: |
cd Examples/apps/DestinationsExample
xcodebuild -workspace "DestinationsExample.xcworkspace" -scheme "DestinationsExample" -sdk iphonesimulator -destination 'platform=iOS Simulator,name=iPhone 11,OS=14.0'

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
objects = {

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

/* Begin PBXFileReference section */
465879BC268A511600180335 /* ConsoleLogger.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ConsoleLogger.swift; path = ../../../other_plugins/ConsoleLogger.swift; sourceTree = "<group>"; };
4663C72A267A8D6B00ADDD1A /* BasicExample.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = BasicExample.entitlements; sourceTree = "<group>"; };
469F7AF8265C25890038E773 /* EventData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EventData.swift; sourceTree = "<group>"; };
46E38361265837EA00BA2502 /* BasicExample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = BasicExample.app; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -68,7 +66,6 @@
children = (
4663C72A267A8D6B00ADDD1A /* BasicExample.entitlements */,
46E38364265837EA00BA2502 /* AppDelegate.swift */,
465879BC268A511600180335 /* ConsoleLogger.swift */,
46E38366265837EA00BA2502 /* SceneDelegate.swift */,
469F7AF8265C25890038E773 /* EventData.swift */,
46E38368265837EA00BA2502 /* ViewController.swift */,
Expand Down Expand Up @@ -167,7 +164,6 @@
46E38369265837EA00BA2502 /* ViewController.swift in Sources */,
46E38365265837EA00BA2502 /* AppDelegate.swift in Sources */,
469F7AF9265C25890038E773 /* EventData.swift in Sources */,
465879BD268A511600180335 /* ConsoleLogger.swift in Sources */,
46E38367265837EA00BA2502 /* SceneDelegate.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
1 change: 0 additions & 1 deletion Examples/apps/BasicExample/BasicExample/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
.flushInterval(10)

analytics = Analytics(configuration: configuration)
analytics?.add(plugin: ConsoleLogger(name: "consoleLogger"))

return true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
46871695270E16080028B595 /* UIKitScreenTracking.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4687168F270E16080028B595 /* UIKitScreenTracking.swift */; };
46871696270E16080028B595 /* ConsentTracking.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46871690270E16080028B595 /* ConsentTracking.swift */; };
46871697270E16080028B595 /* IDFACollection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46871691270E16080028B595 /* IDFACollection.swift */; };
46871698270E16080028B595 /* ConsoleLogger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46871692270E16080028B595 /* ConsoleLogger.swift */; };
468DE06527D28C6500F2D94B /* SegmentAmplitude in Frameworks */ = {isa = PBXBuildFile; productRef = 468DE06427D28C6500F2D94B /* SegmentAmplitude */; };
468DE06827D28C7E00F2D94B /* SegmentAppsFlyer in Frameworks */ = {isa = PBXBuildFile; productRef = 468DE06727D28C7E00F2D94B /* SegmentAppsFlyer */; };
468DE06B27D28CB200F2D94B /* SegmentFacebook in Frameworks */ = {isa = PBXBuildFile; productRef = 468DE06A27D28CB200F2D94B /* SegmentFacebook */; };
Expand All @@ -34,7 +33,6 @@
4687168F270E16080028B595 /* UIKitScreenTracking.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIKitScreenTracking.swift; sourceTree = "<group>"; };
46871690270E16080028B595 /* ConsentTracking.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ConsentTracking.swift; sourceTree = "<group>"; };
46871691270E16080028B595 /* IDFACollection.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = IDFACollection.swift; sourceTree = "<group>"; };
46871692270E16080028B595 /* ConsoleLogger.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ConsoleLogger.swift; sourceTree = "<group>"; };
469EC8CF266066130068F9E3 /* SystemConfiguration.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SystemConfiguration.framework; path = System/Library/Frameworks/SystemConfiguration.framework; sourceTree = SDKROOT; };
469EC8E1266828AF0068F9E3 /* DestinationsExample-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "DestinationsExample-Bridging-Header.h"; sourceTree = "<group>"; };
469F7B04266011690038E773 /* DestinationsExample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = DestinationsExample.app; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -75,7 +73,6 @@
4687168F270E16080028B595 /* UIKitScreenTracking.swift */,
46871690270E16080028B595 /* ConsentTracking.swift */,
46871691270E16080028B595 /* IDFACollection.swift */,
46871692270E16080028B595 /* ConsoleLogger.swift */,
);
name = other_plugins;
path = ../../../other_plugins;
Expand Down Expand Up @@ -220,7 +217,6 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
46871698270E16080028B595 /* ConsoleLogger.swift in Sources */,
469F7B0C266011690038E773 /* ViewController.swift in Sources */,
469F7B08266011690038E773 /* AppDelegate.swift in Sources */,
46871694270E16080028B595 /* NotificationTracking.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ struct ContentView: View {
HStack {
Button(action: {
Analytics.main.group(groupId: "12345-Group")
Analytics.main.log(message: "Started group")
}, label: {
Text("Group")
}).padding(6)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
46E3834C26582D9E00BA2502 /* UIKitScreenTracking.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46E3834826582D9E00BA2502 /* UIKitScreenTracking.swift */; };
46E3834D26582D9E00BA2502 /* ConsentTracking.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46E3834926582D9E00BA2502 /* ConsentTracking.swift */; };
46E3834E26582D9E00BA2502 /* IDFACollection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46E3834A26582D9E00BA2502 /* IDFACollection.swift */; };
46E3834F26582D9E00BA2502 /* ConsoleLogger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46E3834B26582D9E00BA2502 /* ConsoleLogger.swift */; };
46E3835326582DA400BA2502 /* CustomScreenTracking.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46E3835126582DA400BA2502 /* CustomScreenTracking.swift */; };
46E3835426582DA400BA2502 /* MultiInstance.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46E3835226582DA400BA2502 /* MultiInstance.swift */; };
46E383572658307800BA2502 /* Segment in Frameworks */ = {isa = PBXBuildFile; productRef = 46E383562658307800BA2502 /* Segment */; };
Expand All @@ -39,7 +38,6 @@
46E3834826582D9E00BA2502 /* UIKitScreenTracking.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIKitScreenTracking.swift; sourceTree = "<group>"; };
46E3834926582D9E00BA2502 /* ConsentTracking.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ConsentTracking.swift; sourceTree = "<group>"; };
46E3834A26582D9E00BA2502 /* IDFACollection.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = IDFACollection.swift; sourceTree = "<group>"; };
46E3834B26582D9E00BA2502 /* ConsoleLogger.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ConsoleLogger.swift; sourceTree = "<group>"; };
46E3835126582DA400BA2502 /* CustomScreenTracking.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CustomScreenTracking.swift; sourceTree = "<group>"; };
46E3835226582DA400BA2502 /* MultiInstance.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MultiInstance.swift; sourceTree = "<group>"; };
46E73DA226F531320021042C /* NotificationTracking.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationTracking.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -100,7 +98,6 @@
46E73DA226F531320021042C /* NotificationTracking.swift */,
46E3834926582D9E00BA2502 /* ConsentTracking.swift */,
46E3834A26582D9E00BA2502 /* IDFACollection.swift */,
46E3834B26582D9E00BA2502 /* ConsoleLogger.swift */,
);
name = other_plugins;
path = ../../other_plugins;
Expand Down Expand Up @@ -200,7 +197,6 @@
buildActionMask = 2147483647;
files = (
46E3835326582DA400BA2502 /* CustomScreenTracking.swift in Sources */,
46E3834F26582D9E00BA2502 /* ConsoleLogger.swift in Sources */,
46022789261F860100A9E913 /* Tab1ViewController.swift in Sources */,
46022785261F860100A9E913 /* AppDelegate.swift in Sources */,
46E3834C26582D9E00BA2502 /* UIKitScreenTracking.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

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

Analytics.support.add(plugin: ConsoleLogger(name: "support"))
Analytics.support.add(plugin: ConsentTracking())

Analytics.support.track(name: "test event")
Expand Down
68 changes: 0 additions & 68 deletions Examples/other_plugins/ConsoleLogger.swift

This file was deleted.

6 changes: 3 additions & 3 deletions Examples/other_plugins/NotificationTracking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class NotificationTracking: Plugin {
var type: PluginType = .utility
weak var analytics: Analytics?

func trackNotification(_ properties: [String: Any], fromLaunch launch: Bool) {
func trackNotification(_ properties: [String: Codable], fromLaunch launch: Bool) {
if launch {
analytics?.track(name: "Push Notification Tapped", properties: properties)
} else {
Expand All @@ -55,7 +55,7 @@ class NotificationTracking: Plugin {
// determination if a push notification caused the app to open.
extension NotificationTracking: RemoteNotifications {
func receivedRemoteNotification(userInfo: [AnyHashable: Any]) {
if let notification = userInfo as? [String: Any] {
if let notification = userInfo as? [String: Codable] {
trackNotification(notification, fromLaunch: false)
}
}
Expand Down Expand Up @@ -88,7 +88,7 @@ import UIKit

extension NotificationTracking: iOSLifecycle {
func application(_ application: UIApplication?, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) {
if let notification = launchOptions?[UIApplication.LaunchOptionsKey.remoteNotification] as? [String: Any] {
if let notification = launchOptions?[UIApplication.LaunchOptionsKey.remoteNotification] as? [String: Codable] {
trackNotification(notification, fromLaunch: true)
}
}
Expand Down
8 changes: 4 additions & 4 deletions Sources/Segment/Events.swift
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ extension Analytics {
/// generate the UUID and Apple's policies on IDs, see
/// https://segment.io/libraries/ios#ids
/// - properties: A dictionary of traits you know about the user. Things like: email, name, plan, etc.
public func track(name: String, properties: [String: Any]? = nil) {
public func track(name: String, properties: [String: Codable]? = nil) {
var props: JSON? = nil
if let properties = properties {
do {
Expand All @@ -165,7 +165,7 @@ extension Analytics {
/// https://segment.io/libraries/ios#ids
/// - traits: A dictionary of traits you know about the user. Things like: email, name, plan, etc.
/// In the case when user logs out, make sure to call ``reset()`` to clear user's identity info.
public func identify(userId: String, traits: [String: Any]? = nil) {
public func identify(userId: String, traits: [String: Codable]? = nil) {
do {
if let traits = traits {
let traits = try JSON(traits as Any)
Expand All @@ -187,7 +187,7 @@ extension Analytics {
/// - screenTitle: The title of the screen being tracked.
/// - category: A category to the type of screen if it applies.
/// - properties: Any extra metadata associated with the screen. e.g. method of access, size, etc.
public func screen(title: String, category: String? = nil, properties: [String: Any]? = nil) {
public func screen(title: String, category: String? = nil, properties: [String: Codable]? = nil) {
var event = ScreenEvent(title: title, category: category, properties: nil)
if let properties = properties {
do {
Expand All @@ -204,7 +204,7 @@ extension Analytics {
/// - Parameters:
/// - groupId: A unique identifier for the group identification in your system.
/// - traits: Traits of the group you may be interested in such as email, phone or name.
public func group(groupId: String, traits: [String: Any]?) {
public func group(groupId: String, traits: [String: Codable]?) {
var event = GroupEvent(groupId: groupId)
if let traits = traits {
do {
Expand Down
8 changes: 4 additions & 4 deletions Sources/Segment/ObjC/ObjCAnalytics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ extension ObjCAnalytics {

@objc(track:properties:)
public func track(name: String, properties: [String: Any]?) {
analytics.track(name: name, properties: properties)
analytics.track(name: name, properties: properties as? [String: Codable])
}

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

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

@objc(alias:)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,15 @@ class iOSLifecycleEvents: PlatformPlugin, iOSLifecycle {
])
}

let sourceApp: String? = launchOptions?[UIApplication.LaunchOptionsKey.sourceApplication] as? String ?? ""
let url: String? = launchOptions?[UIApplication.LaunchOptionsKey.url] as? String ?? ""

analytics?.track(name: "Application Opened", properties: [
"from_background": false,
"version": currentVersion ?? "",
"build": currentBuild ?? "",
"referring_application": launchOptions?[UIApplication.LaunchOptionsKey.sourceApplication] ?? "",
"url": launchOptions?[UIApplication.LaunchOptionsKey.url] ?? ""
"referring_application": sourceApp,
"url": url
])

UserDefaults.standard.setValue(currentVersion, forKey: Self.versionKey)
Expand Down
4 changes: 4 additions & 0 deletions Sources/Segment/Utilities/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,7 @@ extension Optional: Flattenable {
}
}
}

internal func codable(_ dict: [String: Any]?) -> [String: Codable]? {
return dict as? [String: Codable]
}