From 895be17eec74131d584b3a3a51cc16d9397332cb Mon Sep 17 00:00:00 2001 From: Brandon Sneed Date: Tue, 24 Aug 2021 13:11:16 -0700 Subject: [PATCH 1/3] Renamed mapKeys to mapTransform --- Sources/Segment/Utilities/JSON.swift | 12 ++++++------ Tests/Segment-Tests/JSON_Tests.swift | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Sources/Segment/Utilities/JSON.swift b/Sources/Segment/Utilities/JSON.swift index f08fdf09..1160bd77 100644 --- a/Sources/Segment/Utilities/JSON.swift +++ b/Sources/Segment/Utilities/JSON.swift @@ -274,9 +274,9 @@ extension JSON { /// - Parameters: /// - keys: A dictionary containing key mappings, in the format of ["Old": "New"]. /// - valueTransform: An optional value transform closure. Key represents the new key name. - public func mapKeys(_ keys: [String: String], valueTransform: ((_ key: String, _ value: Any) -> Any)? = nil) throws -> JSON { + public func mapTransform(_ keys: [String: String], valueTransform: ((_ key: String, _ value: Any) -> Any)? = nil) throws -> JSON { guard let dict = self.dictionaryValue else { return self } - let mapped = try dict.mapKeys(keys, valueTransform: valueTransform) + let mapped = try dict.mapTransform(keys, valueTransform: valueTransform) let result = try JSON(mapped) return result } @@ -285,7 +285,7 @@ extension JSON { // MARK: - Helpers extension Dictionary where Key == String, Value == Any { - internal func mapKeys(_ keys: [String: String], valueTransform: ((_ key: Key, _ value: Value) -> Any)? = nil) throws -> [Key: Value] { + internal func mapTransform(_ keys: [String: String], valueTransform: ((_ key: Key, _ value: Value) -> Any)? = nil) throws -> [Key: Value] { let mapped = Dictionary(uniqueKeysWithValues: self.map { key, value -> (Key, Value) in var newKey = key var newValue = value @@ -299,7 +299,7 @@ extension Dictionary where Key == String, Value == Any { } // is this value a dictionary? if let dictValue = value as? [Key: Value] { - if let r = try? dictValue.mapKeys(keys, valueTransform: valueTransform) { + if let r = try? dictValue.mapTransform(keys, valueTransform: valueTransform) { // if so, lets recurse... newValue = r } @@ -309,7 +309,7 @@ extension Dictionary where Key == String, Value == Any { newValue = arrayValue.map { item -> Value in var newValue = item if let dictValue = item as? [Key: Value] { - if let r = try? dictValue.mapKeys(keys, valueTransform: valueTransform) { + if let r = try? dictValue.mapTransform(keys, valueTransform: valueTransform) { newValue = r } } @@ -318,7 +318,7 @@ extension Dictionary where Key == String, Value == Any { } if !(newValue is [Key: Value]), let transform = valueTransform { - // it's not a dictionary apply our transform. + // it's not a dictionary so apply our transform. // note: if it's an array, we've processed any dictionaries inside // already, but this gives the opportunity to apply a transform to the other diff --git a/Tests/Segment-Tests/JSON_Tests.swift b/Tests/Segment-Tests/JSON_Tests.swift index ca02d477..1a847593 100644 --- a/Tests/Segment-Tests/JSON_Tests.swift +++ b/Tests/Segment-Tests/JSON_Tests.swift @@ -137,7 +137,7 @@ class JSONTests: XCTestCase { let json = try! JSON(dict) - let output = try! json.mapKeys(keys).dictionaryValue + let output = try! json.mapTransform(keys).dictionaryValue XCTAssertTrue(output!["AKey1"] as! Int == 1) XCTAssertTrue(output!["AKey2"] as! Int == 2) @@ -153,7 +153,7 @@ class JSONTests: XCTestCase { let json = try! JSON(dict) - let output = try! json.mapKeys(keys, valueTransform: { key, value in + let output = try! json.mapTransform(keys, valueTransform: { key, value in var newValue = value if let v = newValue as? Int { if v == 1 { From 394af8ebe8a96fe3494ebf91d70a67cd13533218 Mon Sep 17 00:00:00 2001 From: Brandon Sneed Date: Wed, 25 Aug 2021 13:07:33 -0700 Subject: [PATCH 2/3] Changed event functions to use [String: AnyHashable] across the board. --- Sources/Segment/Events.swift | 2 +- Sources/Segment/ObjC/ObjCAnalytics.swift | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/Segment/Events.swift b/Sources/Segment/Events.swift index dc9afb36..7214da79 100644 --- a/Sources/Segment/Events.swift +++ b/Sources/Segment/Events.swift @@ -136,7 +136,7 @@ extension Analytics { /// but want to record traits, you should pass nil. For more information on how we /// 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: AnyHashable]? = nil) { var props: JSON? = nil if let properties = properties { do { diff --git a/Sources/Segment/ObjC/ObjCAnalytics.swift b/Sources/Segment/ObjC/ObjCAnalytics.swift index 718650ef..a57b4479 100644 --- a/Sources/Segment/ObjC/ObjCAnalytics.swift +++ b/Sources/Segment/ObjC/ObjCAnalytics.swift @@ -32,7 +32,7 @@ extension ObjCAnalytics { @objc(track:properties:) - public func track(name: String, properties: [String: Any]?) { + public func track(name: String, properties: [String: AnyHashable]?) { analytics.track(name: name, properties: properties) } From 37c2052035cd82fbd61e6860f9488a0c461612ae Mon Sep 17 00:00:00 2001 From: Brandon Sneed Date: Wed, 25 Aug 2021 13:16:09 -0700 Subject: [PATCH 3/3] AnyHashable took too long to evaluate; Conforming to Any instead. --- Sources/Segment/Events.swift | 8 ++++---- Sources/Segment/ObjC/ObjCAnalytics.swift | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Sources/Segment/Events.swift b/Sources/Segment/Events.swift index 7214da79..b28bbdf5 100644 --- a/Sources/Segment/Events.swift +++ b/Sources/Segment/Events.swift @@ -136,7 +136,7 @@ extension Analytics { /// but want to record traits, you should pass nil. For more information on how we /// 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: AnyHashable]? = nil) { + public func track(name: String, properties: [String: Any]? = nil) { var props: JSON? = nil if let properties = properties { do { @@ -155,7 +155,7 @@ extension Analytics { /// but want to record traits, you should pass nil. For more information on how we /// generate the UUID and Apple's policies on IDs, see https://segment.io/libraries/ios#ids /// - traits: A dictionary of traits you know about the user. Things like: email, name, plan, etc. - public func identify(userId: String, traits: [String: AnyHashable]? = nil) { + public func identify(userId: String, traits: [String: Any]? = nil) { do { if let traits = traits { let traits = try JSON(traits as Any) @@ -178,7 +178,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(screenTitle: String, category: String? = nil, properties: [String: AnyHashable]? = nil) { + public func screen(screenTitle: String, category: String? = nil, properties: [String: Any]? = nil) { var event = ScreenEvent(screenTitle: screenTitle, category: category, properties: nil) if let properties = properties { do { @@ -195,7 +195,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: AnyHashable]?) { + public func group(groupId: String, traits: [String: Any]?) { var event = GroupEvent(groupId: groupId) if let traits = traits { do { diff --git a/Sources/Segment/ObjC/ObjCAnalytics.swift b/Sources/Segment/ObjC/ObjCAnalytics.swift index a57b4479..ff9ba44f 100644 --- a/Sources/Segment/ObjC/ObjCAnalytics.swift +++ b/Sources/Segment/ObjC/ObjCAnalytics.swift @@ -32,7 +32,7 @@ extension ObjCAnalytics { @objc(track:properties:) - public func track(name: String, properties: [String: AnyHashable]?) { + public func track(name: String, properties: [String: Any]?) { analytics.track(name: name, properties: properties) } @@ -53,7 +53,7 @@ extension ObjCAnalytics { /// generate the UUID and Apple's policies on IDs, see https://segment.io/libraries/ios#ids /// - traits: A dictionary of traits you know about the user. Things like: email, name, plan, etc. @objc(identify:traits:) - public func identify(userId: String, traits: [String: AnyHashable]?) { + public func identify(userId: String, traits: [String: Any]?) { analytics.identify(userId: userId, traits: traits) } @@ -79,7 +79,7 @@ extension ObjCAnalytics { /// - 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. @objc(screen:category:properties:) - public func screen(screenTitle: String, category: String?, properties: [String: AnyHashable]?) { + public func screen(screenTitle: String, category: String?, properties: [String: Any]?) { analytics.screen(screenTitle: screenTitle, category: category, properties: properties) } @@ -96,7 +96,7 @@ extension ObjCAnalytics { /// - 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. @objc(group:traits:) - public func group(groupId: String, traits: [String: AnyHashable]?) { + public func group(groupId: String, traits: [String: Any]?) { analytics.group(groupId: groupId, traits: traits) } }