2021-11-17 09:25:39 -08:00
|
|
|
//
|
|
|
|
|
// Copyright RevenueCat Inc. All Rights Reserved.
|
|
|
|
|
//
|
|
|
|
|
// Licensed under the MIT License (the "License");
|
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
//
|
|
|
|
|
// https://opensource.org/licenses/MIT
|
|
|
|
|
//
|
|
|
|
|
// RawDataContainer.swift
|
|
|
|
|
//
|
|
|
|
|
// Created by Nacho Soto on 11/16/21.
|
|
|
|
|
|
2022-05-24 14:22:11 -07:00
|
|
|
/// A type which exposes its underlying content for debugging purposes or for getting access
|
2021-11-17 09:25:39 -08:00
|
|
|
/// to future data while using an older version of the SDK.
|
|
|
|
|
public protocol RawDataContainer {
|
|
|
|
|
|
2022-05-24 14:23:07 -07:00
|
|
|
/// The type of the ``RawDataContainer/rawData`` for this type.
|
|
|
|
|
associatedtype Content
|
2021-11-17 09:25:39 -08:00
|
|
|
|
2022-05-24 14:22:11 -07:00
|
|
|
/// The underlying content for debugging purposes or for getting access
|
|
|
|
|
/// to future data while using an older version of the SDK.
|
2022-05-24 14:23:07 -07:00
|
|
|
var rawData: Content { get }
|
2022-05-24 14:22:11 -07:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-24 14:23:07 -07:00
|
|
|
extension Decoder {
|
2022-05-24 14:22:11 -07:00
|
|
|
|
2022-05-24 14:23:07 -07:00
|
|
|
/// Decodes the entire content of this `Decoder` into `[String: Any]` to be used for a `RawDataContainer` type.
|
|
|
|
|
func decodeRawData() -> [String: Any] {
|
2022-05-24 14:22:11 -07:00
|
|
|
do {
|
2022-05-24 14:23:07 -07:00
|
|
|
let value = try self.singleValueContainer()
|
|
|
|
|
.decode(AnyDecodable.self)
|
|
|
|
|
.asAny
|
|
|
|
|
|
|
|
|
|
guard let dictionary = value as? [String: Any] else {
|
|
|
|
|
Logger.warn(Strings.codable.unexpectedValueError(type: type(of: value), value: value))
|
|
|
|
|
return [:]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return dictionary
|
2022-05-24 14:22:11 -07:00
|
|
|
} catch {
|
2023-07-12 06:35:54 -07:00
|
|
|
Logger.warn(Strings.codable.decoding_error(error, AnyDecodable.self))
|
2022-05-24 14:22:11 -07:00
|
|
|
return [:]
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-11-17 09:25:39 -08:00
|
|
|
|
|
|
|
|
}
|