MethodConfigs.swift 4.1 KB

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