Skip to content

Commit 2d9c60b

Browse files
committed
generate wrapper functions with proper names
1 parent f7acfc9 commit 2d9c60b

File tree

2 files changed

+81
-39
lines changed

2 files changed

+81
-39
lines changed

Sources/protoc-gen-grpc-swift/Generator-Client+AsyncAwait.swift

Lines changed: 68 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -95,49 +95,79 @@ extension Generator {
9595
self.method = method
9696

9797
let rpcType = streamingType(self.method)
98-
let callType = Types.call(for: rpcType)
99-
let callTypeWithoutPrefix = Types.call(for: rpcType, withGRPCPrefix: false)
98+
printRpcFunctionImplementation(rpcType: rpcType)
99+
printRpcFunctionWrapper(rpcType: rpcType)
100+
}
101+
}
102+
}
100103

101-
switch rpcType {
102-
case .unary, .serverStreaming:
103-
self.printFunction(
104-
name: self.methodMakeFunctionCallName,
105-
arguments: [
106-
"_ request: \(self.methodInputName)",
107-
"callOptions: \(Types.clientCallOptions)? = nil",
108-
],
109-
returnType: "\(callType)<\(self.methodInputName), \(self.methodOutputName)>",
110-
access: self.access
111-
) {
112-
self.withIndentation("return self.make\(callTypeWithoutPrefix)", braces: .round) {
113-
self.println("path: \(self.methodPathUsingClientMetadata),")
114-
self.println("request: request,")
115-
self.println("callOptions: callOptions ?? self.defaultCallOptions,")
116-
self.println(
117-
"interceptors: self.interceptors?.\(self.methodInterceptorFactoryName)() ?? []"
118-
)
119-
}
120-
}
104+
private func printRpcFunctionImplementation(rpcType: StreamingType) {
105+
let argumentsBuilder: (() -> Void)?
106+
switch rpcType {
107+
case .unary, .serverStreaming:
108+
argumentsBuilder = {
109+
self.println("request: request,")
110+
}
111+
default:
112+
argumentsBuilder = nil
113+
}
114+
let callTypeWithoutPrefix = Types.call(for: rpcType, withGRPCPrefix: false)
115+
printRpcFunction(rpcType: rpcType, name: self.methodMakeFunctionCallName) {
116+
self.withIndentation("return self.make\(callTypeWithoutPrefix)", braces: .round) {
117+
self.println("path: \(self.methodPathUsingClientMetadata),")
118+
argumentsBuilder?()
119+
self.println("callOptions: callOptions ?? self.defaultCallOptions,")
120+
self.println(
121+
"interceptors: self.interceptors?.\(self.methodInterceptorFactoryName)() ?? []"
122+
)
123+
}
124+
}
125+
}
121126

122-
case .clientStreaming, .bidirectionalStreaming:
123-
self.printFunction(
124-
name: self.methodMakeFunctionCallName,
125-
arguments: ["callOptions: \(Types.clientCallOptions)? = nil"],
126-
returnType: "\(callType)<\(self.methodInputName), \(self.methodOutputName)>",
127-
access: self.access
128-
) {
129-
self.withIndentation("return self.make\(callTypeWithoutPrefix)", braces: .round) {
130-
self.println("path: \(self.methodPathUsingClientMetadata),")
131-
self.println("callOptions: callOptions ?? self.defaultCallOptions,")
132-
self.println(
133-
"interceptors: self.interceptors?.\(self.methodInterceptorFactoryName)() ?? []"
134-
)
135-
}
136-
}
137-
}
127+
private func printRpcFunctionWrapper(rpcType: StreamingType) {
128+
let functionName = methodMakeFunctionCallName
129+
let functionWrapperName = methodMakeFunctionCallWrapperName
130+
guard functionName != functionWrapperName else { return }
131+
self.println()
132+
133+
let argumentsBuilder: (() -> Void)?
134+
switch rpcType {
135+
case .unary, .serverStreaming:
136+
argumentsBuilder = {
137+
self.println("request,")
138+
}
139+
default:
140+
argumentsBuilder = nil
141+
}
142+
printRpcFunction(rpcType: rpcType, name: functionWrapperName) {
143+
self.withIndentation("return self.\(functionName)", braces: .round) {
144+
argumentsBuilder?()
145+
self.println("callOptions: callOptions")
138146
}
139147
}
140148
}
149+
150+
private func printRpcFunction(rpcType: StreamingType, name: String, bodyBuilder: (() -> Void)?) {
151+
let callType = Types.call(for: rpcType)
152+
self.printFunction(
153+
name: name,
154+
arguments: rpcFunctionArguments(rpcType: rpcType),
155+
returnType: "\(callType)<\(self.methodInputName), \(self.methodOutputName)>",
156+
access: self.access,
157+
bodyBuilder: bodyBuilder
158+
)
159+
}
160+
161+
private func rpcFunctionArguments(rpcType: StreamingType) -> [String] {
162+
var arguments = ["callOptions: \(Types.clientCallOptions)? = nil"]
163+
switch rpcType {
164+
case .unary, .serverStreaming:
165+
arguments.insert("_ request: \(self.methodInputName)", at: .zero)
166+
default:
167+
break
168+
}
169+
return arguments
170+
}
141171
}
142172

143173
// MARK: - Client protocol extension: "Simple, but safe" call wrappers.

Sources/protoc-gen-grpc-swift/Generator-Names.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,18 @@ extension Generator {
128128
}
129129

130130
internal var methodMakeFunctionCallName: String {
131+
let name: String
132+
if self.options.keepMethodCasing {
133+
name = self.method.name
134+
} else {
135+
name = NamingUtils.toUpperCamelCase(self.method.name)
136+
}
137+
138+
let fnName = "make\(name)Call"
139+
return self.sanitize(fieldName: fnName)
140+
}
141+
142+
internal var methodMakeFunctionCallWrapperName: String {
131143
return "make\(methodComposableName)Call"
132144
}
133145

@@ -155,7 +167,7 @@ extension Generator {
155167
}
156168

157169
internal var methodInterceptorFactoryName: String {
158-
return "make\(methodComposableName)Interceptors"
170+
return "make\(self.method.name)Interceptors"
159171
}
160172

161173
internal var servicePath: String {

0 commit comments

Comments
 (0)