NameResolverRegistry.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright 2024, 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. /// A registry for name resolver factories.
  17. ///
  18. /// The registry provides name resolvers for resolvable targets. You can control which name
  19. /// resolvers are available by registering and removing resolvers by type. The following code
  20. /// demonstrates how to create a registry, add and remove resolver factories, and create a resolver.
  21. ///
  22. /// ```swift
  23. /// // Create a new resolver registry with the default resolvers.
  24. /// var registry = NameResolverRegistry.defaults
  25. ///
  26. /// // Register a custom resolver, the registry can now resolve targets of
  27. /// // type `CustomResolver.ResolvableTarget`.
  28. /// registry.registerFactory(CustomResolver())
  29. ///
  30. /// // Remove the Unix Domain Socket and VSOCK resolvers, if they exist.
  31. /// registry.removeFactory(ofType: NameResolvers.UnixDomainSocket.self)
  32. /// registry.removeFactory(ofType: NameResolvers.VSOCK.self)
  33. ///
  34. /// // Resolve an IPv4 target
  35. /// if let resolver = registry.makeResolver(for: .ipv4(host: "localhost", port: 80)) {
  36. /// // ...
  37. /// }
  38. /// ```
  39. @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
  40. public struct NameResolverRegistry {
  41. private enum Factory {
  42. case other(any NameResolverFactory)
  43. init(_ factory: some NameResolverFactory) {
  44. self = .other(factory)
  45. }
  46. func makeResolverIfCompatible<Target: ResolvableTarget>(_ target: Target) -> NameResolver? {
  47. switch self {
  48. case .other(let factory):
  49. return factory.makeResolverIfCompatible(target)
  50. }
  51. }
  52. func hasTarget<Target: ResolvableTarget>(_ target: Target) -> Bool {
  53. switch self {
  54. case .other(let factory):
  55. return factory.isCompatible(withTarget: target)
  56. }
  57. }
  58. func `is`<Factory: NameResolverFactory>(ofType factoryType: Factory.Type) -> Bool {
  59. switch self {
  60. case .other(let factory):
  61. return type(of: factory) == factoryType
  62. }
  63. }
  64. }
  65. private var factories: [Factory]
  66. /// Creates a new name resolver registry with no resolve factories.
  67. public init() {
  68. self.factories = []
  69. }
  70. /// Returns a new name resolver registry with the default factories registered.
  71. public static var defaults: Self {
  72. return NameResolverRegistry()
  73. }
  74. /// The number of resolver factories in the registry.
  75. public var count: Int {
  76. return self.factories.count
  77. }
  78. /// Whether there are no resolver factories in the registry.
  79. public var isEmpty: Bool {
  80. return self.factories.isEmpty
  81. }
  82. /// Registers a new name resolver factory.
  83. ///
  84. /// Any factories of the same type are removed prior to inserting the factory.
  85. ///
  86. /// - Parameter factory: The factory to register.
  87. public mutating func registerFactory<Factory: NameResolverFactory>(_ factory: Factory) {
  88. self.removeFactory(ofType: Factory.self)
  89. self.factories.append(Self.Factory(factory))
  90. }
  91. /// Removes any factories which have the given type
  92. ///
  93. /// - Parameter type: The type of factory to remove.
  94. /// - Returns: Whether a factory was removed.
  95. @discardableResult
  96. public mutating func removeFactory<Factory: NameResolverFactory>(
  97. ofType type: Factory.Type
  98. ) -> Bool {
  99. let factoryCount = self.factories.count
  100. self.factories.removeAll {
  101. $0.is(ofType: Factory.self)
  102. }
  103. return self.factories.count < factoryCount
  104. }
  105. /// Returns whether the registry contains a factory of the given type.
  106. ///
  107. /// - Parameter type: The type of factory to look for.
  108. /// - Returns: Whether the registry contained the factory of the given type.
  109. public func containsFactory<Factory: NameResolverFactory>(ofType type: Factory.Type) -> Bool {
  110. self.factories.contains {
  111. $0.is(ofType: Factory.self)
  112. }
  113. }
  114. /// Returns whether the registry contains a factory capable of resolving the given target.
  115. ///
  116. /// - Parameter target:
  117. /// - Returns: Whether the registry contains a resolve capable of resolving the target.
  118. public func containsFactory(capableOfResolving target: some ResolvableTarget) -> Bool {
  119. self.factories.contains { $0.hasTarget(target) }
  120. }
  121. /// Makes a ``NameResolver`` for the target, if a suitable factory exists.
  122. ///
  123. /// If multiple factories exist which are capable of resolving the target then the first
  124. /// is used.
  125. ///
  126. /// - Parameter target: The target to make a resolver for.
  127. /// - Returns: The resolver, or `nil` if no factory could make a resolver for the target.
  128. public func makeResolver(for target: some ResolvableTarget) -> NameResolver? {
  129. for factory in self.factories {
  130. if let resolver = factory.makeResolverIfCompatible(target) {
  131. return resolver
  132. }
  133. }
  134. return nil
  135. }
  136. }