Skip to content
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

fix: toDictionary function should convert Date object to string #54

Merged
merged 3 commits into from
May 6, 2024
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
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ jobs:
- name: Checkout
uses: actions/checkout@v2

- name: Set Xcode 14.2
- name: Set Xcode 15
run: |
sudo xcode-select -switch /Applications/Xcode_14.2.app
sudo xcode-select -switch /Applications/Xcode_15.1.app

- name: Carthage Bootstrap
run: carthage bootstrap --use-xcframeworks
Expand Down
56 changes: 52 additions & 4 deletions Sources/Experiment/ExperimentUser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ import Foundation
}

internal extension ExperimentUser {

func toDictionary() -> [String:Any] {
var data = [String:Any]()
data["device_id"] = self.deviceId
Expand All @@ -543,9 +543,41 @@ internal extension ExperimentUser {
data["device_model"] = self.deviceModel
data["carrier"] = self.carrier
data["library"] = self.library
data["user_properties"] = self.userPropertiesAnyValue
data["groups"] = self.groups
data["group_properties"] = self.groupProperties

// Convert NSDate objects to ISO 8601 strings in user_properties
if let userProperties = self.userPropertiesAnyValue {
var convertedUserProperties = [String:Any]()
for (key, value) in userProperties {
if let dateValue = value as? Date {
convertedUserProperties[key] = dateValue.iso8601
} else {
convertedUserProperties[key] = value
}
}
data["user_properties"] = convertedUserProperties
}

// Convert NSDate objects to ISO 8601 strings in group_properties
if let groupProperties = self.groupProperties {
var convertedGroupProperties = [String:Any]()
for (groupType, groups) in groupProperties {
var convertedGroups = [String:Any]()
for (groupName, properties) in groups {
var convertedProperties = [String:Any]()
for (key, value) in properties {
if let dateValue = value as? Date {
convertedProperties[key] = dateValue.iso8601
} else {
convertedProperties[key] = value
}
}
convertedGroups[groupName] = convertedProperties
}
convertedGroupProperties[groupType] = convertedGroups
}
data["group_properties"] = convertedGroupProperties
}

return data
}

Expand Down Expand Up @@ -613,3 +645,19 @@ extension ExperimentUser {
return context
}
}

internal extension Date {
var iso8601: String {
return DateFormatter.iso8601.string(from: self)
}
}

internal extension DateFormatter {
static let iso8601: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.timeZone = TimeZone(secondsFromGMT: 0)
return formatter
}()
}
47 changes: 47 additions & 0 deletions Tests/ExperimentTests/ExperimentUserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,51 @@ class ExperimentUserTests: XCTestCase {
let user = ExperimentUserBuilder().userProperty("test", value: "test").build()
XCTAssertTrue(user != ExperimentUser())
}

func testUserPropertiesWithDateExtension() {
let date = Date(timeIntervalSince1970: 1619821200)

let user = ExperimentUserBuilder()
.deviceId("device_id")
.userId("user_id")
.version(nil)
.country("country")
.userProperty("dateUserProperty", value: date)
.build()

let userData = user.toDictionary()

if let userProperties = userData["user_properties"] as? [String: Any] {
if let dateUserProperty = userProperties["dateUserProperty"] as? String {
XCTAssertEqual(dateUserProperty, "2021-04-30T22:20:00.000Z")
} else {
XCTFail("dateUserProperty not found or not a string")
}
} else {
XCTFail("user_properties not found or not a dictionary")
}
}

func testGroupPropertiesWithDateExtension() {
let date = Date(timeIntervalSince1970: 1619821200)

let user = ExperimentUserBuilder()
.deviceId("device_id")
.userId("user_id")
.version(nil)
.country("country")
.groupProperty("groupType", "groupName", "dateGroupProperty", date.iso8601)
.build()

let userData = user.toDictionary()

if let groupProperties = userData["group_properties"] as? [String: Any],
let groupTypeDict = groupProperties["groupType"] as? [String: Any],
let groupNameDict = groupTypeDict["groupName"] as? [String: Any],
let dateGroupProperty = groupNameDict["dateGroupProperty"] as? String {
XCTAssertEqual(dateGroupProperty, "2021-04-30T22:20:00.000Z")
} else {
XCTFail("Unable to access group properties or dateGroupProperty")
}
}
}
Loading