Skip to content

Misc small fixes #52

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 3 commits into from
Aug 25, 2021
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
6 changes: 3 additions & 3 deletions Sources/Segment/Events.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions Sources/Segment/ObjC/ObjCAnalytics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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)
}

Expand All @@ -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)
}
}
Expand Down
12 changes: 6 additions & 6 deletions Sources/Segment/Utilities/JSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand All @@ -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
}
Expand All @@ -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
}
}
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions Tests/Segment-Tests/JSON_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand Down