Skip to content
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

Add code gen config for the module name #2201

Merged
merged 3 commits into from
Feb 28, 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
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
133 changes: 133 additions & 0 deletions Sources/GRPCCodeGen/Internal/Namer.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
* 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?, isStreaming: Bool) -> ExistingTypeDescription {
return self.requestResponse(
for: type,
isRequest: true,
isStreaming: isStreaming,
isClient: false
)
}

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

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

func clientResponse(forType type: String?, isStreaming: Bool) -> ExistingTypeDescription {
return self.requestResponse(
for: type,
isRequest: false,
isStreaming: isStreaming,
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