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