UserInfo.swift 3.1 KB

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