UserInfo.swift 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright 2020, gRPC Authors All rights reserved.
  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. /// `UserInfo` is a dictionary for heterogeneously typed values with type safe access to the stored
  17. /// values.
  18. ///
  19. /// Values are keyed by a type conforming to the `UserInfo.Key` protocol. The protocol requires an
  20. /// `associatedtype`: the type of the value the key is paired with. A key can be created using a
  21. /// caseless `enum`, for example:
  22. ///
  23. /// ```
  24. /// enum IDKey: UserInfo.Key {
  25. /// typealias Value = Int
  26. /// }
  27. /// ```
  28. ///
  29. /// Values can be set and retrieved from `UserInfo` by subscripting with the key:
  30. ///
  31. /// ```
  32. /// userInfo[IDKey.self] = 42
  33. /// let id = userInfo[IDKey.self] // id = 42
  34. ///
  35. /// userInfo[IDKey.self] = nil
  36. /// ```
  37. ///
  38. /// More convenient access can be provided with helper extensions on `UserInfo`:
  39. ///
  40. /// ```
  41. /// extension UserInfo {
  42. /// var id: IDKey.Value? {
  43. /// get { self[IDKey.self] }
  44. /// set { self[IDKey.self] = newValue }
  45. /// }
  46. /// }
  47. /// ```
  48. public struct UserInfo: CustomStringConvertible {
  49. private var storage: [AnyUserInfoKey: Any]
  50. /// A protocol for a key.
  51. public typealias Key = UserInfoKey
  52. /// Create an empty 'UserInfo'.
  53. public init() {
  54. self.storage = [:]
  55. }
  56. /// Allows values to be set and retrieved in a type safe way.
  57. public subscript<Key: UserInfoKey>(key: Key.Type) -> Key.Value? {
  58. get {
  59. if let anyValue = self.storage[AnyUserInfoKey(key)] {
  60. // The types must line up here.
  61. return (anyValue as! Key.Value)
  62. } else {
  63. return nil
  64. }
  65. }
  66. set {
  67. self.storage[AnyUserInfoKey(key)] = newValue
  68. }
  69. }
  70. public var description: String {
  71. return "[" + self.storage.map { key, value in
  72. "\(key): \(value)"
  73. }.joined(separator: ", ") + "]"
  74. }
  75. /// A `UserInfoKey` wrapper.
  76. private struct AnyUserInfoKey: Hashable, CustomStringConvertible {
  77. private let keyType: Any.Type
  78. var description: String {
  79. return String(describing: self.keyType.self)
  80. }
  81. init<Key: UserInfoKey>(_ keyType: Key.Type) {
  82. self.keyType = keyType
  83. }
  84. static func == (lhs: AnyUserInfoKey, rhs: AnyUserInfoKey) -> Bool {
  85. return ObjectIdentifier(lhs.keyType) == ObjectIdentifier(rhs.keyType)
  86. }
  87. func hash(into hasher: inout Hasher) {
  88. hasher.combine(ObjectIdentifier(self.keyType))
  89. }
  90. }
  91. }
  92. public protocol UserInfoKey {
  93. /// The type of identified by this key.
  94. associatedtype Value
  95. }