ServiceDescriptor.swift 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 description of a service.
  17. public struct ServiceDescriptor: Sendable, Hashable {
  18. /// The name of the package the service belongs to. For example, "helloworld".
  19. /// An empty string means that the service does not belong to any package.
  20. public var package: String {
  21. if let index = self.fullyQualifiedService.utf8.lastIndex(of: UInt8(ascii: ".")) {
  22. return String(self.fullyQualifiedService[..<index])
  23. } else {
  24. return ""
  25. }
  26. }
  27. /// The name of the service. For example, "Greeter".
  28. public var service: String {
  29. if var index = self.fullyQualifiedService.utf8.lastIndex(of: UInt8(ascii: ".")) {
  30. self.fullyQualifiedService.utf8.formIndex(after: &index)
  31. return String(self.fullyQualifiedService[index...])
  32. } else {
  33. return self.fullyQualifiedService
  34. }
  35. }
  36. /// The fully qualified service name in the format:
  37. /// - "package.service": if a package name is specified. For example, "helloworld.Greeter".
  38. /// - "service": if a package name is not specified. For example, "Greeter".
  39. public var fullyQualifiedService: String
  40. /// Create a new descriptor from the fully qualified service name.
  41. /// - Parameter fullyQualifiedService: The fully qualified service name.
  42. public init(fullyQualifiedService: String) {
  43. self.fullyQualifiedService = fullyQualifiedService
  44. }
  45. /// - Parameters:
  46. /// - package: The name of the package the service belongs to. For example, "helloworld".
  47. /// An empty string means that the service does not belong to any package.
  48. /// - service: The name of the service. For example, "Greeter".
  49. public init(package: String, service: String) {
  50. if package.isEmpty {
  51. self.fullyQualifiedService = service
  52. } else {
  53. self.fullyQualifiedService = package + "." + service
  54. }
  55. }
  56. }
  57. extension ServiceDescriptor: CustomStringConvertible {
  58. public var description: String {
  59. self.fullyQualifiedService
  60. }
  61. }