Skip to content

Commit 532dfc6

Browse files
Skotiglbrntt
andauthored
fix generated method names (#2224)
This PR fixes incorrectly generated method names. For example: `rpc GRPCUnary(google.protobuf.Empty) returns (google.protobuf.Empty) {}` The plugin correctly generates: - `makeGRPCUnaryInterceptors` - `Methods.gRPCUnary` - `func gRPCUnary` but incorrectly: - `makeGrpcunaryCall` This seems to be the only place with a different logic. And when using lowercase letters, the interceptor method name is incorrect: `rpc unary(google.protobuf.Empty) returns (FunctionName) {}` The plugin correctly generates: - `makeUnaryCall` - `Methods.unary` - `func unary` but incorrectly: - `makeunaryInterceptors` The PR fixes these two issues by introducing a common logic for `methodMakeFunctionCallName` and `methodInterceptorFactoryName`. The logic is similar to the one in `methodFunctionName`, just that it uppercases the first character instead of lowercasing. Thus, for the given examples the result is: `makeGRPCUnaryCall` and `makeUnaryInterceptors` --------- Co-authored-by: George Barnett <[email protected]>
1 parent 07616fd commit 532dfc6

File tree

2 files changed

+80
-39
lines changed

2 files changed

+80
-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: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ extension Generator {
129129

130130
internal var methodMakeFunctionCallName: String {
131131
let name: String
132-
133132
if self.options.keepMethodCasing {
134133
name = self.method.name
135134
} else {
@@ -140,6 +139,18 @@ extension Generator {
140139
return self.sanitize(fieldName: fnName)
141140
}
142141

142+
internal var methodMakeFunctionCallWrapperName: String {
143+
return "make\(methodComposableName)Call"
144+
}
145+
146+
internal var methodComposableName: String {
147+
var name = method.name
148+
if !options.keepMethodCasing {
149+
name = name.prefix(1).uppercased() + name.dropFirst()
150+
}
151+
return name
152+
}
153+
143154
internal func sanitize(fieldName string: String) -> String {
144155
if quotableFieldNames.contains(string) {
145156
return "`\(string)`"

0 commit comments

Comments
 (0)