From 01b7cdb90f5bebb94699900935b453a75a979c67 Mon Sep 17 00:00:00 2001 From: Slava Semeniuk Date: Fri, 15 Sep 2023 10:59:28 +0300 Subject: [PATCH] fix: encoding Encodable values from [String: Any] (#82) --- Sources/Amplitude/Utilities/CodableExtension.swift | 10 +--------- Tests/AmplitudeTests/Events/BaseEventTests.swift | 10 ++++++++++ 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/Sources/Amplitude/Utilities/CodableExtension.swift b/Sources/Amplitude/Utilities/CodableExtension.swift index 93f1f325..37bb97e7 100644 --- a/Sources/Amplitude/Utilities/CodableExtension.swift +++ b/Sources/Amplitude/Utilities/CodableExtension.swift @@ -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)!) diff --git a/Tests/AmplitudeTests/Events/BaseEventTests.swift b/Tests/AmplitudeTests/Events/BaseEventTests.swift index c488d9f1..240903ca 100644 --- a/Tests/AmplitudeTests/Events/BaseEventTests.swift +++ b/Tests/AmplitudeTests/Events/BaseEventTests.swift @@ -28,6 +28,8 @@ final class BaseEventTests: XCTestCase { "integer": 1, "string": "stringValue", "array": [1, 2, 3], + "int64": 1 as Int64, + "int32": 1 as Int32 ] ) @@ -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"