-
Notifications
You must be signed in to change notification settings - Fork 121
/
ObjcIntercompatibility.swift
28 lines (24 loc) · 1.24 KB
/
ObjcIntercompatibility.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/*
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
* This product includes software developed at Datadog (https://www.datadoghq.com/).
* Copyright 2019-Present Datadog, Inc.
*/
import Foundation
import DatadogInternal
import DatadogCore
/// Casts `[String: Any]` attributes to their `Encodable` representation by wrapping each `Any` into `AnyEncodable`.
internal func castAttributesToSwift(_ attributes: [String: Any]) -> [String: Encodable] {
return attributes.mapValues { AnyEncodable($0) }
}
/// Casts `[String: Encodable]` attributes to their `Any` representation by unwrapping each `AnyEncodable` into `Any`.
internal func castAttributesToObjectiveC(_ attributes: [String: Encodable]) -> [String: Any] {
return attributes
.compactMapValues { value in (value as? AnyEncodable)?.value }
}
/// Helper extension to use `castAttributesToObjectiveC(_:)` in auto generated ObjC interop `RUMDataModels`.
/// Unlike the function it wraps, it has postfix notation which makes it easier to use in generated code.
internal extension Dictionary where Key == String, Value == Encodable {
func castToObjectiveC() -> [String: Any] {
return castAttributesToObjectiveC(self)
}
}