|
5 | 5 | */ |
6 | 6 |
|
7 | 7 | import Foundation |
| 8 | +import DatadogInternal |
8 | 9 |
|
9 | 10 | internal func castAttributesToSwift(_ attributes: NSDictionary) -> [String: Encodable] { |
10 | 11 | return castAttributesToSwift(attributes as? [String: Any] ?? [:]) |
@@ -76,45 +77,6 @@ private func castByPreservingEncodedValue(attributeValue: Any) -> Encodable { |
76 | 77 | return AnyEncodable(attributeValue) |
77 | 78 | } |
78 | 79 |
|
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 | | - |
118 | 80 | private func encodeNSNumber(_ nsnumber: NSNumber, into container: inout SingleValueEncodingContainer) throws { |
119 | 81 | switch CFNumberGetType(nsnumber) { |
120 | 82 | case .charType: |
|
0 commit comments