FIRConfigValue.m 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright 2019 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "FirebaseRemoteConfig/Sources/Public/FirebaseRemoteConfig/FIRRemoteConfig.h"
  17. #import "FirebaseCore/Extension/FirebaseCoreInternal.h"
  18. #import "FirebaseRemoteConfig/Sources/RCNConfigValue_Internal.h"
  19. @implementation FIRRemoteConfigValue {
  20. /// Data backing the config value.
  21. NSData *_data;
  22. FIRRemoteConfigSource _source;
  23. }
  24. /// Designated initializer
  25. - (instancetype)initWithData:(NSData *)data source:(FIRRemoteConfigSource)source {
  26. self = [super init];
  27. if (self) {
  28. _data = [data copy];
  29. _source = source;
  30. }
  31. return self;
  32. }
  33. /// Superclass's designated initializer
  34. - (instancetype)init {
  35. return [self initWithData:nil source:FIRRemoteConfigSourceStatic];
  36. }
  37. /// The string is a UTF-8 representation of NSData.
  38. - (NSString *)stringValue {
  39. return [[NSString alloc] initWithData:_data encoding:NSUTF8StringEncoding];
  40. }
  41. /// Number representation of a UTF-8 string.
  42. - (NSNumber *)numberValue {
  43. return [NSNumber numberWithDouble:self.stringValue.doubleValue];
  44. }
  45. /// Internal representation of the FIRRemoteConfigValue as a NSData object.
  46. - (NSData *)dataValue {
  47. return _data;
  48. }
  49. /// Boolean representation of a UTF-8 string.
  50. - (BOOL)boolValue {
  51. return self.stringValue.boolValue;
  52. }
  53. /// Returns a foundation object (NSDictionary / NSArray) representation for JSON data.
  54. - (id)JSONValue {
  55. NSError *error;
  56. if (!_data) {
  57. return nil;
  58. }
  59. id JSONObject = [NSJSONSerialization JSONObjectWithData:_data options:kNilOptions error:&error];
  60. if (error) {
  61. FIRLogDebug(kFIRLoggerRemoteConfig, @"I-RCN000065", @"Error parsing data as JSON.");
  62. return nil;
  63. }
  64. return JSONObject;
  65. }
  66. /// Debug description showing the representations of all types.
  67. - (NSString *)debugDescription {
  68. NSString *content = [NSString
  69. stringWithFormat:@"Boolean: %d, String: %@, Number: %@, JSON:%@, Data: %@, Source: %zd",
  70. self.boolValue, self.stringValue, self.numberValue, self.JSONValue, _data,
  71. (long)self.source];
  72. return [NSString stringWithFormat:@"<%@: %p, %@>", [self class], self, content];
  73. }
  74. /// Copy method.
  75. - (id)copyWithZone:(NSZone *)zone {
  76. FIRRemoteConfigValue *value = [[[self class] allocWithZone:zone] initWithData:_data];
  77. return value;
  78. }
  79. @end