| 
 | 1 | +// Copyright 2024 Google LLC  | 
 | 2 | +//  | 
 | 3 | +// Licensed under the Apache License, Version 2.0 (the "License");  | 
 | 4 | +// you may not use this file except in compliance with the License.  | 
 | 5 | +// You may obtain a copy of the License at  | 
 | 6 | +//  | 
 | 7 | +//      http://www.apache.org/licenses/LICENSE-2.0  | 
 | 8 | +//  | 
 | 9 | +// Unless required by applicable law or agreed to in writing, software  | 
 | 10 | +// distributed under the License is distributed on an "AS IS" BASIS,  | 
 | 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  | 
 | 12 | +// See the License for the specific language governing permissions and  | 
 | 13 | +// limitations under the License.  | 
 | 14 | + | 
 | 15 | +import FirebaseCore  | 
 | 16 | +import Foundation  | 
 | 17 | + | 
 | 18 | +@objc(FIRRemoteConfigValue)  | 
 | 19 | +public class RemoteConfigValue: NSObject, NSCopying {  | 
 | 20 | +  /// Data backing the config value.  | 
 | 21 | +  @objc public let dataValue: Data  | 
 | 22 | + | 
 | 23 | +  /// Identifies the source of the fetched value.  | 
 | 24 | +  @objc public let source: RemoteConfigSource  | 
 | 25 | + | 
 | 26 | +  /// Designated initializer  | 
 | 27 | +  @objc public init(data: Data, source: RemoteConfigSource) {  | 
 | 28 | +    dataValue = data  | 
 | 29 | +    self.source = source  | 
 | 30 | +  }  | 
 | 31 | + | 
 | 32 | +  /// Gets the value as a string.  | 
 | 33 | +  @objc public var stringValue: String {  | 
 | 34 | +    if let string = String(data: dataValue, encoding: .utf8) {  | 
 | 35 | +      return string  | 
 | 36 | +    }  | 
 | 37 | +    return "" // Return empty string if data is not valid UTF-8  | 
 | 38 | +  }  | 
 | 39 | + | 
 | 40 | +  /// Gets the value as a number value.  | 
 | 41 | +  @objc public var numberValue: NSNumber {  | 
 | 42 | +    return NSNumber(value: Double(stringValue) ?? 0)  | 
 | 43 | +  }  | 
 | 44 | + | 
 | 45 | +  /// Gets the value as a boolean.  | 
 | 46 | +  @objc public var boolValue: Bool {  | 
 | 47 | +    return (stringValue as NSString).boolValue  | 
 | 48 | +  }  | 
 | 49 | + | 
 | 50 | +  /// Gets a foundation object (NSDictionary / NSArray) by parsing the value as JSON.  | 
 | 51 | +  @objc(JSONValue) public var jsonValue: Any? {  | 
 | 52 | +    guard !dataValue.isEmpty else {  | 
 | 53 | +      return nil  | 
 | 54 | +    }  | 
 | 55 | +    do {  | 
 | 56 | +      let jsonObject = try JSONSerialization.jsonObject(with: dataValue, options: [])  | 
 | 57 | +      return jsonObject  | 
 | 58 | +    } catch {  | 
 | 59 | +      RCLog.debug("I-RCN000065", "Error parsing data as JSON.")  | 
 | 60 | +      return nil  | 
 | 61 | +    }  | 
 | 62 | +  }  | 
 | 63 | + | 
 | 64 | +  /// Debug description showing the representations of all types.  | 
 | 65 | +  override public var debugDescription: String {  | 
 | 66 | +    let content = """  | 
 | 67 | +    Boolean: \(boolValue), String: \(stringValue), Number: \(numberValue), \  | 
 | 68 | +    JSON:\(String(describing: jsonValue)), Data: \(dataValue), Source: \(source.rawValue)  | 
 | 69 | +    """  | 
 | 70 | +    return "<\(type(of: self)): \(Unmanaged.passUnretained(self).toOpaque()), \(content)>"  | 
 | 71 | +  }  | 
 | 72 | + | 
 | 73 | +  /// Copy method.  | 
 | 74 | +  @objc public func copy(with zone: NSZone? = nil) -> Any {  | 
 | 75 | +    return RemoteConfigValue(data: dataValue, source: source)  | 
 | 76 | +  }  | 
 | 77 | +}  | 
0 commit comments