FIRConfigValue.m 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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/Sources/Private/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. + (NSNumberFormatter *)numberFormatter {
  25. static NSNumberFormatter *formatter;
  26. static dispatch_once_t onceToken = 0;
  27. dispatch_once(&onceToken, ^{
  28. formatter = [[NSNumberFormatter alloc] init];
  29. formatter.numberStyle = NSNumberFormatterDecimalStyle;
  30. formatter.locale = [NSLocale autoupdatingCurrentLocale];
  31. });
  32. return formatter;
  33. }
  34. /// Designated initializer
  35. - (instancetype)initWithData:(NSData *)data source:(FIRRemoteConfigSource)source {
  36. self = [super init];
  37. if (self) {
  38. _data = [data copy];
  39. _source = source;
  40. }
  41. return self;
  42. }
  43. /// Superclass's designated initializer
  44. - (instancetype)init {
  45. return [self initWithData:nil source:FIRRemoteConfigSourceStatic];
  46. }
  47. /// The string is a UTF-8 representation of NSData.
  48. - (NSString *)stringValue {
  49. return [[NSString alloc] initWithData:_data encoding:NSUTF8StringEncoding];
  50. }
  51. /// Number representation of a UTF-8 string.
  52. - (NSNumber *)numberValue {
  53. NSString *stringValue = self.stringValue;
  54. if (stringValue == nil) {
  55. return nil;
  56. }
  57. return [[FIRRemoteConfigValue numberFormatter] numberFromString:stringValue];
  58. }
  59. /// Internal representation of the FIRRemoteConfigValue as a NSData object.
  60. - (NSData *)dataValue {
  61. return _data;
  62. }
  63. /// Boolean representation of a UTF-8 string.
  64. - (BOOL)boolValue {
  65. return self.stringValue.boolValue;
  66. }
  67. /// Returns a foundation object (NSDictionary / NSArray) representation for JSON data.
  68. - (id)JSONValue {
  69. NSError *error;
  70. if (!_data) {
  71. return nil;
  72. }
  73. id JSONObject = [NSJSONSerialization JSONObjectWithData:_data options:kNilOptions error:&error];
  74. if (error) {
  75. FIRLogDebug(kFIRLoggerRemoteConfig, @"I-RCN000065", @"Error parsing data as JSON.");
  76. return nil;
  77. }
  78. return JSONObject;
  79. }
  80. /// Debug description showing the representations of all types.
  81. - (NSString *)debugDescription {
  82. NSString *content = [NSString
  83. stringWithFormat:@"Boolean: %d, String: %@, Number: %@, JSON:%@, Data: %@, Source: %zd",
  84. self.boolValue, self.stringValue, self.numberValue, self.JSONValue, _data,
  85. (long)self.source];
  86. return [NSString stringWithFormat:@"<%@: %p, %@>", [self class], self, content];
  87. }
  88. /// Copy method.
  89. - (id)copyWithZone:(NSZone *)zone {
  90. FIRRemoteConfigValue *value = [[[self class] allocWithZone:zone] initWithData:_data];
  91. return value;
  92. }
  93. @end