Skip to content

Commit b503984

Browse files
committed
add namer
1 parent dfc41f2 commit b503984

10 files changed

+388
-195
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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?, streaming: Bool) -> ExistingTypeDescription {
51+
return self.requestResponse(for: type, isRequest: true, isStreaming: streaming, isClient: false)
52+
}
53+
54+
func serverResponse(forType type: String?, streaming: Bool) -> ExistingTypeDescription {
55+
return self.requestResponse(
56+
for: type,
57+
isRequest: false,
58+
isStreaming: streaming,
59+
isClient: false
60+
)
61+
}
62+
63+
func clientRequest(forType type: String?, streaming: Bool) -> ExistingTypeDescription {
64+
return self.requestResponse(for: type, isRequest: true, isStreaming: streaming, isClient: true)
65+
}
66+
67+
func clientResponse(forType type: String?, streaming: Bool) -> ExistingTypeDescription {
68+
return self.requestResponse(for: type, isRequest: false, isStreaming: streaming, isClient: true)
69+
}
70+
71+
var serverContext: ExistingTypeDescription {
72+
self.grpcCore("ServerContext")
73+
}
74+
75+
func rpcRouter(genericOver type: String) -> ExistingTypeDescription {
76+
.generic(wrapper: self.grpcCore("RPCRouter"), wrapped: .member(type))
77+
}
78+
79+
var serviceDescriptor: ExistingTypeDescription {
80+
self.grpcCore("ServiceDescriptor")
81+
}
82+
83+
var methodDescriptor: ExistingTypeDescription {
84+
self.grpcCore("MethodDescriptor")
85+
}
86+
87+
func serializer(forType type: String) -> ExistingTypeDescription {
88+
.generic(wrapper: self.grpcCore("MessageSerializer"), wrapped: .member(type))
89+
}
90+
91+
func deserializer(forType type: String) -> ExistingTypeDescription {
92+
.generic(wrapper: self.grpcCore("MessageDeserializer"), wrapped: .member(type))
93+
}
94+
95+
func rpcWriter(forType type: String) -> ExistingTypeDescription {
96+
.generic(wrapper: self.grpcCore("RPCWriter"), wrapped: .member(type))
97+
}
98+
99+
func rpcAsyncSequence(forType type: String) -> ExistingTypeDescription {
100+
.generic(
101+
wrapper: self.grpcCore("RPCAsyncSequence"),
102+
wrapped: .member(type),
103+
.any(.member(["Swift", "Error"]))
104+
)
105+
}
106+
107+
var callOptions: ExistingTypeDescription {
108+
self.grpcCore("CallOptions")
109+
}
110+
111+
var metadata: ExistingTypeDescription {
112+
self.grpcCore("Metadata")
113+
}
114+
115+
func grpcClient(genericOver transport: String) -> ExistingTypeDescription {
116+
.generic(wrapper: self.grpcCore("GRPCClient"), wrapped: [.member(transport)])
117+
}
118+
}

0 commit comments

Comments
 (0)