Value.swift 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /*
  2. * Copyright 2021 Google LLC
  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 Foundation
  17. #if SWIFT_PACKAGE
  18. @_exported import FirebaseRemoteConfigInternal
  19. #endif // SWIFT_PACKAGE
  20. /// Implements subscript overloads to enable Remote Config values to be accessed
  21. /// in a type-safe way directly from the current config.
  22. public extension RemoteConfig {
  23. /// Return a typed RemoteConfigValue for a key.
  24. /// - Parameter key: A Remote Config key.
  25. /// - Returns: A typed RemoteConfigValue.
  26. subscript<T: Decodable>(decodedValue key: String) -> T? {
  27. return try? configValue(forKey: key).decoded()
  28. }
  29. /// Return a Dictionary for a RemoteConfig JSON key.
  30. /// - Parameter key: A Remote Config key.
  31. /// - Returns: A Dictionary representing a RemoteConfig JSON value.
  32. subscript(jsonValue key: String) -> [String: AnyHashable]? {
  33. guard let value = configValue(forKey: key).jsonValue as? [String: AnyHashable] else {
  34. // nil is the historical behavior for failing to extract JSON.
  35. return nil
  36. }
  37. return value
  38. }
  39. }