Skip to content

Commit

Permalink
fix: encoding Encodable values from [String: Any] (#82) (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
slavasemeniuk authored Sep 21, 2023
1 parent c74979a commit b610fa1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
12 changes: 2 additions & 10 deletions Sources/Amplitude/Utilities/CodableExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,8 @@ 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 {
try container.encodeIfPresent(val, forKey: JSONCodingKeys(stringValue: item.key)!)
if let val = item.value as? Encodable {
try container.encode(val, forKey: JSONCodingKeys(stringValue: item.key)!)
} else if let val = item.value as? [Any] {
try container.encodeIfPresent(val, forKey: JSONCodingKeys(stringValue: item.key)!)
} else if let val = item.value as? [String: Any] {
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 b610fa1

Please sign in to comment.