MethodConfigs.swift 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 ``MethodConfig``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. package struct MethodConfigs: Sendable, Hashable {
  26. private var elements: [MethodConfig.Name: MethodConfig]
  27. /// Create a new ``_MethodConfigs``.
  28. ///
  29. /// - Parameter serviceConfig: The configuration to read ``MethodConfig`` from.
  30. package init(serviceConfig: ServiceConfig = ServiceConfig()) {
  31. self.elements = [:]
  32. for configuration in serviceConfig.methodConfig {
  33. for name in configuration.names {
  34. self.elements[name] = configuration
  35. }
  36. }
  37. }
  38. /// Get or set the corresponding ``MethodConfig`` for the given ``MethodDescriptor``.
  39. ///
  40. /// Configuration is hierarchical and can be set per-method, per-service
  41. /// (``setDefaultConfiguration(_:forService:)``) and globally (``setDefaultConfiguration(_:)``).
  42. /// This subscript sets the per-method configuration but retrieves a configuration respecting
  43. /// the hierarchy. If no per-method configuration is present, the per-service configuration is
  44. /// checked and returned if present. If the per-service configuration isn't present then the
  45. /// global configuration is returned, if present.
  46. ///
  47. /// - Parameters:
  48. /// - descriptor: The ``MethodDescriptor`` for which to get or set a ``MethodConfig``.
  49. package subscript(_ descriptor: MethodDescriptor) -> MethodConfig? {
  50. get {
  51. var name = MethodConfig.Name(service: descriptor.service, method: descriptor.method)
  52. if let configuration = self.elements[name] {
  53. return configuration
  54. }
  55. // Check if the config is set at the service level by clearing the method.
  56. name.method = ""
  57. if let configuration = self.elements[name] {
  58. return configuration
  59. }
  60. // Check if the config is set at the global level by clearing the service and method.
  61. name.service = ""
  62. return self.elements[name]
  63. }
  64. set {
  65. let name = MethodConfig.Name(service: descriptor.service, method: descriptor.method)
  66. self.elements[name] = newValue
  67. }
  68. }
  69. /// Set a default configuration for all methods that have no overrides.
  70. ///
  71. /// - Parameter configuration: The default configuration.
  72. package mutating func setDefaultConfiguration(_ configuration: MethodConfig?) {
  73. let name = MethodConfig.Name(service: "", method: "")
  74. self.elements[name] = configuration
  75. }
  76. /// Set a default configuration for a service.
  77. ///
  78. /// If getting a configuration for a method that's part of a service, and the method itself doesn't have an
  79. /// override, then this configuration will be used instead of the default configuration passed when creating
  80. /// this instance of ``MethodConfigs``.
  81. ///
  82. /// - Parameters:
  83. /// - configuration: The default configuration for the service.
  84. /// - service: The name of the service for which this override applies.
  85. package mutating func setDefaultConfiguration(
  86. _ configuration: MethodConfig?,
  87. forService service: String
  88. ) {
  89. let name = MethodConfig.Name(service: "", method: "")
  90. self.elements[name] = configuration
  91. }
  92. }