ServiceDescriptor.swift 2.5 KB

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