Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 5 additions & 1 deletion Sources/GRPCCodeGen/CodeGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public struct CodeGenerator: Sendable {
public var client: Bool
/// Whether or not server code should be generated.
public var server: Bool
/// The name of the core gRPC module.
public var grpcCoreModuleName: String

/// Creates a new configuration.
///
Expand All @@ -61,6 +63,7 @@ public struct CodeGenerator: Sendable {
self.indentation = indentation
self.client = client
self.server = server
self.grpcCoreModuleName = "GRPCCore"
}

/// The possible access levels for the generated code.
Expand Down Expand Up @@ -96,7 +99,8 @@ public struct CodeGenerator: Sendable {
accessLevel: self.config.accessLevel,
accessLevelOnImports: self.config.accessLevelOnImports,
client: self.config.client,
server: self.config.server
server: self.config.server,
grpcCoreModuleName: self.config.grpcCoreModuleName
)

let sourceFile = try textRenderer.render(structured: structuredSwiftRepresentation)
Expand Down
118 changes: 118 additions & 0 deletions Sources/GRPCCodeGen/Internal/Namer.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* Copyright 2024, gRPC Authors All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package struct Namer: Sendable, Hashable {
let grpcCore: String

package init(grpcCore: String = "GRPCCore") {
self.grpcCore = grpcCore
}

private func grpcCore(_ typeName: String) -> ExistingTypeDescription {
return .member([self.grpcCore, typeName])
}

private func requestResponse(
for type: String?,
isRequest: Bool,
isStreaming: Bool,
isClient: Bool
) -> ExistingTypeDescription {
let prefix = isStreaming ? "Streaming" : ""
let peer = isClient ? "Client" : "Server"
let kind = isRequest ? "Request" : "Response"
let baseType = self.grpcCore(prefix + peer + kind)

if let type = type {
return .generic(wrapper: baseType, wrapped: .member(type))
} else {
return baseType
}
}

func literalNamespacedType(_ type: String) -> String {
return self.grpcCore + "." + type
}

func serverRequest(forType type: String?, streaming: Bool) -> ExistingTypeDescription {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I think having isStreaming in all these funcs instead of streaming makes it more consistent with requestResponse (and it's also more idiomatic).

Suggested change
func serverRequest(forType type: String?, streaming: Bool) -> ExistingTypeDescription {
func serverRequest(forType type: String?, isStreaming: Bool) -> ExistingTypeDescription {

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 8fa87bf

return self.requestResponse(for: type, isRequest: true, isStreaming: streaming, isClient: false)
}

func serverResponse(forType type: String?, streaming: Bool) -> ExistingTypeDescription {
return self.requestResponse(
for: type,
isRequest: false,
isStreaming: streaming,
isClient: false
)
}

func clientRequest(forType type: String?, streaming: Bool) -> ExistingTypeDescription {
return self.requestResponse(for: type, isRequest: true, isStreaming: streaming, isClient: true)
}

func clientResponse(forType type: String?, streaming: Bool) -> ExistingTypeDescription {
return self.requestResponse(for: type, isRequest: false, isStreaming: streaming, isClient: true)
}

var serverContext: ExistingTypeDescription {
self.grpcCore("ServerContext")
}

func rpcRouter(genericOver type: String) -> ExistingTypeDescription {
.generic(wrapper: self.grpcCore("RPCRouter"), wrapped: .member(type))
}

var serviceDescriptor: ExistingTypeDescription {
self.grpcCore("ServiceDescriptor")
}

var methodDescriptor: ExistingTypeDescription {
self.grpcCore("MethodDescriptor")
}

func serializer(forType type: String) -> ExistingTypeDescription {
.generic(wrapper: self.grpcCore("MessageSerializer"), wrapped: .member(type))
}

func deserializer(forType type: String) -> ExistingTypeDescription {
.generic(wrapper: self.grpcCore("MessageDeserializer"), wrapped: .member(type))
}

func rpcWriter(forType type: String) -> ExistingTypeDescription {
.generic(wrapper: self.grpcCore("RPCWriter"), wrapped: .member(type))
}

func rpcAsyncSequence(forType type: String) -> ExistingTypeDescription {
.generic(
wrapper: self.grpcCore("RPCAsyncSequence"),
wrapped: .member(type),
.any(.member(["Swift", "Error"]))
)
}

var callOptions: ExistingTypeDescription {
self.grpcCore("CallOptions")
}

var metadata: ExistingTypeDescription {
self.grpcCore("Metadata")
}

func grpcClient(genericOver transport: String) -> ExistingTypeDescription {
.generic(wrapper: self.grpcCore("GRPCClient"), wrapped: [.member(transport)])
}
}
Loading
Loading