Skip to content

fix generated method names #2224

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 68 additions & 38 deletions Sources/protoc-gen-grpc-swift/Generator-Client+AsyncAwait.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,49 +95,79 @@ extension Generator {
self.method = method

let rpcType = streamingType(self.method)
let callType = Types.call(for: rpcType)
let callTypeWithoutPrefix = Types.call(for: rpcType, withGRPCPrefix: false)
printRPCFunctionImplementation(rpcType: rpcType)
printRPCFunctionWrapper(rpcType: rpcType)
}
}
}

switch rpcType {
case .unary, .serverStreaming:
self.printFunction(
name: self.methodMakeFunctionCallName,
arguments: [
"_ request: \(self.methodInputName)",
"callOptions: \(Types.clientCallOptions)? = nil",
],
returnType: "\(callType)<\(self.methodInputName), \(self.methodOutputName)>",
access: self.access
) {
self.withIndentation("return self.make\(callTypeWithoutPrefix)", braces: .round) {
self.println("path: \(self.methodPathUsingClientMetadata),")
self.println("request: request,")
self.println("callOptions: callOptions ?? self.defaultCallOptions,")
self.println(
"interceptors: self.interceptors?.\(self.methodInterceptorFactoryName)() ?? []"
)
}
}
private func printRPCFunctionImplementation(rpcType: StreamingType) {
let argumentsBuilder: (() -> Void)?
switch rpcType {
case .unary, .serverStreaming:
argumentsBuilder = {
self.println("request: request,")
}
default:
argumentsBuilder = nil
}
let callTypeWithoutPrefix = Types.call(for: rpcType, withGRPCPrefix: false)
printRPCFunction(rpcType: rpcType, name: self.methodMakeFunctionCallName) {
self.withIndentation("return self.make\(callTypeWithoutPrefix)", braces: .round) {
self.println("path: \(self.methodPathUsingClientMetadata),")
argumentsBuilder?()
self.println("callOptions: callOptions ?? self.defaultCallOptions,")
self.println(
"interceptors: self.interceptors?.\(self.methodInterceptorFactoryName)() ?? []"
)
}
}
}

case .clientStreaming, .bidirectionalStreaming:
self.printFunction(
name: self.methodMakeFunctionCallName,
arguments: ["callOptions: \(Types.clientCallOptions)? = nil"],
returnType: "\(callType)<\(self.methodInputName), \(self.methodOutputName)>",
access: self.access
) {
self.withIndentation("return self.make\(callTypeWithoutPrefix)", braces: .round) {
self.println("path: \(self.methodPathUsingClientMetadata),")
self.println("callOptions: callOptions ?? self.defaultCallOptions,")
self.println(
"interceptors: self.interceptors?.\(self.methodInterceptorFactoryName)() ?? []"
)
}
}
}
private func printRPCFunctionWrapper(rpcType: StreamingType) {
let functionName = methodMakeFunctionCallName
let functionWrapperName = methodMakeFunctionCallWrapperName
guard functionName != functionWrapperName else { return }
self.println()

let argumentsBuilder: (() -> Void)?
switch rpcType {
case .unary, .serverStreaming:
argumentsBuilder = {
self.println("request,")
}
default:
argumentsBuilder = nil
}
printRPCFunction(rpcType: rpcType, name: functionWrapperName) {
self.withIndentation("return self.\(functionName)", braces: .round) {
argumentsBuilder?()
self.println("callOptions: callOptions")
}
}
}

private func printRPCFunction(rpcType: StreamingType, name: String, bodyBuilder: (() -> Void)?) {
let callType = Types.call(for: rpcType)
self.printFunction(
name: name,
arguments: rpcFunctionArguments(rpcType: rpcType),
returnType: "\(callType)<\(self.methodInputName), \(self.methodOutputName)>",
access: self.access,
bodyBuilder: bodyBuilder
)
}

private func rpcFunctionArguments(rpcType: StreamingType) -> [String] {
var arguments = ["callOptions: \(Types.clientCallOptions)? = nil"]
switch rpcType {
case .unary, .serverStreaming:
arguments.insert("_ request: \(self.methodInputName)", at: .zero)
default:
break
}
return arguments
}
}

// MARK: - Client protocol extension: "Simple, but safe" call wrappers.
Expand Down
13 changes: 12 additions & 1 deletion Sources/protoc-gen-grpc-swift/Generator-Names.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ extension Generator {

internal var methodMakeFunctionCallName: String {
let name: String

if self.options.keepMethodCasing {
name = self.method.name
} else {
Expand All @@ -140,6 +139,18 @@ extension Generator {
return self.sanitize(fieldName: fnName)
}

internal var methodMakeFunctionCallWrapperName: String {
return "make\(methodComposableName)Call"
}

internal var methodComposableName: String {
var name = method.name
if !options.keepMethodCasing {
name = name.prefix(1).uppercased() + name.dropFirst()
}
return name
}

internal func sanitize(fieldName string: String) -> String {
if quotableFieldNames.contains(string) {
return "`\(string)`"
Expand Down