Skip to content

Commit

Permalink
fix: encoding Encodable values from [String: Any] (amplitude#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
slava-semeniuk-ek committed Sep 15, 2023
1 parent cd3b8ee commit 01b7cdb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
10 changes: 1 addition & 9 deletions Sources/Amplitude/Utilities/CodableExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,7 @@ extension KeyedEncodingContainer {
}
var container = self.nestedContainer(keyedBy: JSONCodingKeys.self, forKey: key)
for item in safeValue {
if let val = item.value as? Int {
try container.encodeIfPresent(val, forKey: JSONCodingKeys(stringValue: item.key)!)
} else if let val = item.value as? String {
try container.encodeIfPresent(val, forKey: JSONCodingKeys(stringValue: item.key)!)
} else if let val = item.value as? Double {
try container.encodeIfPresent(val, forKey: JSONCodingKeys(stringValue: item.key)!)
} else if let val = item.value as? Float {
try container.encodeIfPresent(val, forKey: JSONCodingKeys(stringValue: item.key)!)
} else if let val = item.value as? Bool {
if let val = item.value as? Encodable {
try container.encodeIfPresent(val, forKey: JSONCodingKeys(stringValue: item.key)!)
} else if let val = item.value as? [Any] {
try container.encodeIfPresent(val, forKey: JSONCodingKeys(stringValue: item.key)!)
Expand Down
10 changes: 10 additions & 0 deletions Tests/AmplitudeTests/Events/BaseEventTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ final class BaseEventTests: XCTestCase {
"integer": 1,
"string": "stringValue",
"array": [1, 2, 3],
"int64": 1 as Int64,
"int32": 1 as Int32
]
)

Expand All @@ -42,6 +44,14 @@ final class BaseEventTests: XCTestCase {
baseEventDict!["event_properties"]!["integer" as NSString] as! Int,
1
)
XCTAssertEqual(
baseEventDict!["event_properties"]!["int32" as NSString] as! Int32,
1
)
XCTAssertEqual(
baseEventDict!["event_properties"]!["int64" as NSString] as! Int64,
1
)
XCTAssertEqual(
baseEventDict!["event_properties"]!["string" as NSString] as! String,
"stringValue"
Expand Down

0 comments on commit 01b7cdb

Please sign in to comment.