RemoteConfigValue.swift 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import Foundation
  2. /// This enum indicates the source of fetched Remote Config data.
  3. /// - Note: This mirrors the Objective-C enum `FIRRemoteConfigSource`.
  4. @objc(FIRRemoteConfigSource) public enum RemoteConfigSource: Int {
  5. /// The data source is the Remote Config backend.
  6. case remote = 0
  7. /// The data source is the default config defined for this app.
  8. case defaultValue = 1
  9. /// The data doesn't exist, returns a static initialized value.
  10. case staticValue = 2
  11. }
  12. /// This class provides a wrapper for Remote Config parameter values, with methods to get parameter
  13. /// values as different data types.
  14. @objc(FIRRemoteConfigValue)
  15. public class RemoteConfigValue: NSObject, NSCopying {
  16. /// Underlying data for the config value.
  17. private let valueData: Data
  18. /// Identifies the source of the fetched value.
  19. @objc public let source: RemoteConfigSource
  20. /// Designated initializer.
  21. /// - Parameters:
  22. /// - data: The data representation of the value.
  23. /// - source: The source of the config value.
  24. @objc
  25. public init(data: Data, source: RemoteConfigSource) {
  26. self.valueData = data
  27. self.source = source
  28. super.init()
  29. }
  30. /// Convenience initializer for static values (empty data).
  31. override convenience init() {
  32. self.init(data: Data(), source: .staticValue)
  33. }
  34. /// Gets the value as a string. Returns an empty string if the data cannot be UTF-8 decoded.
  35. @objc public var stringValue: String {
  36. return String(data: valueData, encoding: .utf8) ?? ""
  37. }
  38. /// Gets the value as an `NSNumber`. Tries to interpret the string value as a double.
  39. @objc public var numberValue: NSNumber {
  40. // Mimics Objective-C behavior: NSString.doubleValue returns 0.0 if conversion fails.
  41. return NSNumber(value: Double(stringValue) ?? 0.0)
  42. }
  43. /// Gets the value as a `Data` object.
  44. @objc public var dataValue: Data {
  45. return valueData
  46. }
  47. /// Gets the value as a boolean. Tries to interpret the string value as a boolean.
  48. @objc public var boolValue: Bool {
  49. // Mimics Objective-C behavior: NSString.boolValue checks for specific prefixes/values.
  50. let lowercasedString = stringValue.lowercased()
  51. return lowercasedString == "true" || lowercasedString == "yes" || lowercasedString == "on" ||
  52. lowercasedString == "1" || (Double(stringValue) != 0.0 && lowercasedString != "false" && lowercasedString != "no" && lowercasedString != "off")
  53. }
  54. /// Gets a Foundation object (`NSDictionary` / `NSArray`) by parsing the value as JSON.
  55. /// Returns `nil` if the data is not valid JSON.
  56. @objc public var jsonValue: Any? {
  57. guard !valueData.isEmpty else { return nil }
  58. do {
  59. // Mimics Objective-C behavior using kNilOptions
  60. return try JSONSerialization.jsonObject(with: valueData, options: [])
  61. } catch {
  62. // Log error similar to Objective-C implementation? Maybe not needed in Swift directly.
  63. // FIRLogDebug(...)
  64. return nil
  65. }
  66. }
  67. // MARK: - NSCopying
  68. @objc public func copy(with zone: NSZone? = nil) -> Any {
  69. // Create a new instance with the same data and source.
  70. let copy = RemoteConfigValue(data: valueData, source: source)
  71. return copy
  72. }
  73. // MARK: - Debug Description
  74. /// Debug description showing the representations of all types.
  75. public override var debugDescription: String {
  76. let jsonString = jsonValue.map { "\($0)" } ?? "nil"
  77. return "<\(String(describing: type(of: self))): \(Unmanaged.passUnretained(self).toOpaque()), " +
  78. "Boolean: \(boolValue), String: \(stringValue), Number: \(numberValue), " +
  79. "JSON: \(jsonString), Data: \(dataValue.count) bytes, Source: \(source.rawValue)>"
  80. }
  81. }