Skip to content

Commit 9d02758

Browse files
fix: share AnyEncodable with DatadogInternal for correct attribute decoding
1 parent f9bc7bd commit 9d02758

3 files changed

Lines changed: 5 additions & 42 deletions

File tree

packages/core/ios/Sources/AnyEncodable.swift

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
import Foundation
8+
import DatadogInternal
89

910
internal func castAttributesToSwift(_ attributes: NSDictionary) -> [String: Encodable] {
1011
return castAttributesToSwift(attributes as? [String: Any] ?? [:])
@@ -76,45 +77,6 @@ private func castByPreservingEncodedValue(attributeValue: Any) -> Encodable {
7677
return AnyEncodable(attributeValue)
7778
}
7879

79-
/// Type erasing `Encodable` wrapper to bridge Objective-C's `Any` to Swift `Encodable`.
80-
///
81-
/// Inspired by `AnyCodable` by Flight-School (MIT):
82-
/// https://github.com/Flight-School/AnyCodable/blob/master/Sources/AnyCodable/AnyEncodable.swift
83-
internal class AnyEncodable: Encodable {
84-
internal let value: Any
85-
86-
init(_ value: Any) {
87-
self.value = value
88-
}
89-
90-
func encode(to encoder: Encoder) throws {
91-
var container = encoder.singleValueContainer()
92-
93-
switch value {
94-
case let number as NSNumber:
95-
try encodeNSNumber(number, into: &container)
96-
case is NSNull, is Void:
97-
try container.encodeNil()
98-
case let string as String:
99-
try container.encode(string)
100-
case let date as Date:
101-
try container.encode(date)
102-
case let url as URL:
103-
try container.encode(url)
104-
case let array as [Any]:
105-
try container.encode(array.map { AnyEncodable($0) })
106-
case let dictionary as [String: Any]:
107-
try container.encode(dictionary.mapValues { AnyEncodable($0) })
108-
default:
109-
let context = EncodingError.Context(
110-
codingPath: container.codingPath,
111-
debugDescription: "Value \(value) cannot be encoded - \(type(of: value)) is not supported by `AnyEncodable`."
112-
)
113-
throw EncodingError.invalidValue(value, context)
114-
}
115-
}
116-
}
117-
11880
private func encodeNSNumber(_ nsnumber: NSNumber, into container: inout SingleValueEncodingContainer) throws {
11981
switch CFNumberGetType(nsnumber) {
12082
case .charType:

packages/core/ios/Tests/AnyEncodableTests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import XCTest
88
@testable import DatadogSDKReactNative
9+
@testable import DatadogInternal
910

1011
internal class AnyEncodableTests: XCTestCase {
1112
// MARK: - Casting attributes

packages/core/ios/Tests/DdSdkTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ class DdSdkTests: XCTestCase {
601601
XCTAssertEqual(userInfo.extraInfo["extra-info-3"] as? Bool, true)
602602

603603
if let extraInfo4Encodable = userInfo.extraInfo["extra-info-4"]
604-
as? DatadogSDKReactNative.AnyEncodable,
604+
as? AnyEncodable,
605605
let extraInfo4Dict = extraInfo4Encodable.value as? [String: Int]
606606
{
607607
XCTAssertEqual(extraInfo4Dict, ["nested-extra-info-1": 456])
@@ -664,7 +664,7 @@ class DdSdkTests: XCTestCase {
664664
XCTAssertEqual(userInfo.extraInfo["extra-info-3"] as? Bool, true)
665665

666666
if let extraInfo4Encodable = userInfo.extraInfo["extra-info-4"]
667-
as? DatadogSDKReactNative.AnyEncodable,
667+
as? AnyEncodable,
668668
let extraInfo4Dict = extraInfo4Encodable.value as? [String: Int]
669669
{
670670
XCTAssertEqual(extraInfo4Dict, ["nested-extra-info-1": 456])
@@ -718,7 +718,7 @@ class DdSdkTests: XCTestCase {
718718
XCTAssertEqual(userInfo.extraInfo["extra-info-3"] as? Bool, true)
719719

720720
if let extraInfo4Encodable = userInfo.extraInfo["extra-info-4"]
721-
as? DatadogSDKReactNative.AnyEncodable,
721+
as? AnyEncodable,
722722
let extraInfo4Dict = extraInfo4Encodable.value as? [String: Int]
723723
{
724724
XCTAssertEqual(extraInfo4Dict, ["nested-extra-info-1": 456])

0 commit comments

Comments
 (0)