CustomSignals.swift 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. import Foundation
  15. #if SWIFT_PACKAGE
  16. @_exported import FirebaseRemoteConfigInternal
  17. #endif // SWIFT_PACKAGE
  18. /// Represents a value associated with a key in a custom signal, restricted to the allowed data
  19. /// types : String, Int, Double.
  20. public struct CustomSignalValue {
  21. private enum Kind {
  22. case string(String)
  23. case integer(Int)
  24. case double(Double)
  25. }
  26. private let kind: Kind
  27. private init(kind: Kind) {
  28. self.kind = kind
  29. }
  30. /// Returns a string backed custom signal.
  31. /// - Parameter string: The given string to back the custom signal with.
  32. /// - Returns: A string backed custom signal.
  33. public static func string(_ string: String) -> Self {
  34. Self(kind: .string(string))
  35. }
  36. /// Returns an integer backed custom signal.
  37. /// - Parameter integer: The given integer to back the custom signal with.
  38. /// - Returns: An integer backed custom signal.
  39. public static func integer(_ integer: Int) -> Self {
  40. Self(kind: .integer(integer))
  41. }
  42. /// Returns an floating-point backed custom signal.
  43. /// - Parameter double: The given floating-point value to back the custom signal with.
  44. /// - Returns: An floating-point backed custom signal
  45. public static func double(_ double: Double) -> Self {
  46. Self(kind: .double(double))
  47. }
  48. func toNSObject() -> NSObject {
  49. switch kind {
  50. case let .string(string):
  51. return string as NSString
  52. case let .integer(int):
  53. return int as NSNumber
  54. case let .double(double):
  55. return double as NSNumber
  56. }
  57. }
  58. }
  59. extension CustomSignalValue: ExpressibleByStringInterpolation {
  60. public init(stringLiteral value: String) {
  61. self = .string(value)
  62. }
  63. }
  64. extension CustomSignalValue: ExpressibleByIntegerLiteral {
  65. public init(integerLiteral value: Int) {
  66. self = .integer(value)
  67. }
  68. }
  69. extension CustomSignalValue: ExpressibleByFloatLiteral {
  70. public init(floatLiteral value: Double) {
  71. self = .double(value)
  72. }
  73. }