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 1 commit
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
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
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]
}