Namer.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. package struct Namer: Sendable, Hashable {
  17. let grpcCore: String
  18. package init(grpcCore: String = "GRPCCore") {
  19. self.grpcCore = grpcCore
  20. }
  21. private func grpcCore(_ typeName: String) -> ExistingTypeDescription {
  22. return .member([self.grpcCore, typeName])
  23. }
  24. private func requestResponse(
  25. for type: String?,
  26. isRequest: Bool,
  27. isStreaming: Bool,
  28. isClient: Bool
  29. ) -> ExistingTypeDescription {
  30. let prefix = isStreaming ? "Streaming" : ""
  31. let peer = isClient ? "Client" : "Server"
  32. let kind = isRequest ? "Request" : "Response"
  33. let baseType = self.grpcCore(prefix + peer + kind)
  34. if let type = type {
  35. return .generic(wrapper: baseType, wrapped: .member(type))
  36. } else {
  37. return baseType
  38. }
  39. }
  40. func literalNamespacedType(_ type: String) -> String {
  41. return self.grpcCore + "." + type
  42. }
  43. func serverRequest(forType type: String?, isStreaming: Bool) -> ExistingTypeDescription {
  44. return self.requestResponse(
  45. for: type,
  46. isRequest: true,
  47. isStreaming: isStreaming,
  48. isClient: false
  49. )
  50. }
  51. func serverResponse(forType type: String?, isStreaming: Bool) -> ExistingTypeDescription {
  52. return self.requestResponse(
  53. for: type,
  54. isRequest: false,
  55. isStreaming: isStreaming,
  56. isClient: false
  57. )
  58. }
  59. func clientRequest(forType type: String?, isStreaming: Bool) -> ExistingTypeDescription {
  60. return self.requestResponse(
  61. for: type,
  62. isRequest: true,
  63. isStreaming: isStreaming,
  64. isClient: true
  65. )
  66. }
  67. func clientResponse(forType type: String?, isStreaming: Bool) -> ExistingTypeDescription {
  68. return self.requestResponse(
  69. for: type,
  70. isRequest: false,
  71. isStreaming: isStreaming,
  72. isClient: true
  73. )
  74. }
  75. var serverContext: ExistingTypeDescription {
  76. self.grpcCore("ServerContext")
  77. }
  78. func rpcRouter(genericOver type: String) -> ExistingTypeDescription {
  79. .generic(wrapper: self.grpcCore("RPCRouter"), wrapped: .member(type))
  80. }
  81. var serviceDescriptor: ExistingTypeDescription {
  82. self.grpcCore("ServiceDescriptor")
  83. }
  84. var methodDescriptor: ExistingTypeDescription {
  85. self.grpcCore("MethodDescriptor")
  86. }
  87. func serializer(forType type: String) -> ExistingTypeDescription {
  88. .generic(wrapper: self.grpcCore("MessageSerializer"), wrapped: .member(type))
  89. }
  90. func deserializer(forType type: String) -> ExistingTypeDescription {
  91. .generic(wrapper: self.grpcCore("MessageDeserializer"), wrapped: .member(type))
  92. }
  93. func rpcWriter(forType type: String) -> ExistingTypeDescription {
  94. .generic(wrapper: self.grpcCore("RPCWriter"), wrapped: .member(type))
  95. }
  96. func rpcAsyncSequence(forType type: String) -> ExistingTypeDescription {
  97. .generic(
  98. wrapper: self.grpcCore("RPCAsyncSequence"),
  99. wrapped: .member(type),
  100. .any(.member(["Swift", "Error"]))
  101. )
  102. }
  103. var callOptions: ExistingTypeDescription {
  104. self.grpcCore("CallOptions")
  105. }
  106. var metadata: ExistingTypeDescription {
  107. self.grpcCore("Metadata")
  108. }
  109. func grpcClient(genericOver transport: String) -> ExistingTypeDescription {
  110. .generic(wrapper: self.grpcCore("GRPCClient"), wrapped: [.member(transport)])
  111. }
  112. }