MethodConfigurations.swift 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Copyright 2023, 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 collection of ``MethodConfiguration``s, mapped to specific methods or services.
  17. ///
  18. /// When creating a new instance, no overrides and no default will be set for using when getting
  19. /// a configuration for a method that has not been given a specific override.
  20. /// Use ``setDefaultConfiguration(_:forService:)`` to set a specific override for a whole
  21. /// service, or set a default configuration for all methods by calling ``setDefaultConfiguration(_:)``.
  22. ///
  23. /// Use the subscript to get and set configurations for specific methods.
  24. @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
  25. public struct MethodConfigurations: Sendable, Hashable {
  26. private var elements: [MethodDescriptor: MethodConfiguration]
  27. /// Create a new ``MethodConfigurations`` with no overrides and no default configuration.
  28. public init() {
  29. self.elements = [:]
  30. }
  31. /// Get or set the corresponding ``MethodConfiguration`` for the given ``MethodDescriptor``.
  32. ///
  33. /// Configuration is hierarchical and can be set per-method, per-service
  34. /// (``setDefaultConfiguration(_:forService:)``) and globally (``setDefaultConfiguration(_:)``).
  35. /// This subscript sets the per-method configuration but retrieves a configuration respecting
  36. /// the hierarchy. If no per-method configuration is present, the per-service configuration is
  37. /// checked and returned if present. If the per-service configuration isn't present then the
  38. /// global configuration is returned, if present.
  39. ///
  40. /// - Parameters:
  41. /// - descriptor: The ``MethodDescriptor`` for which to get or set a ``MethodConfiguration``.
  42. public subscript(_ descriptor: MethodDescriptor) -> MethodConfiguration? {
  43. get {
  44. if let configuration = self.elements[descriptor] {
  45. return configuration
  46. }
  47. // Check if the config is set at the service level by clearing the method.
  48. var descriptor = descriptor
  49. descriptor.method = ""
  50. if let configuration = self.elements[descriptor] {
  51. return configuration
  52. }
  53. // Check if the config is set at the global level by clearing the service and method.
  54. descriptor.service = ""
  55. return self.elements[descriptor]
  56. }
  57. set {
  58. self.elements[descriptor] = newValue
  59. }
  60. }
  61. /// Set a default configuration for all methods that have no overrides.
  62. ///
  63. /// - Parameter configuration: The default configuration.
  64. public mutating func setDefaultConfiguration(_ configuration: MethodConfiguration?) {
  65. let descriptor = MethodDescriptor(service: "", method: "")
  66. self.elements[descriptor] = configuration
  67. }
  68. /// Set a default configuration for a service.
  69. ///
  70. /// If getting a configuration for a method that's part of a service, and the method itself doesn't have an
  71. /// override, then this configuration will be used instead of the default configuration passed when creating
  72. /// this instance of ``MethodConfigurations``.
  73. ///
  74. /// - Parameters:
  75. /// - configuration: The default configuration for the service.
  76. /// - service: The name of the service for which this override applies.
  77. public mutating func setDefaultConfiguration(
  78. _ configuration: MethodConfiguration?,
  79. forService service: String
  80. ) {
  81. self.elements[MethodDescriptor(service: service, method: "")] = configuration
  82. }
  83. }