Skip to content

Commit

Permalink
Add code gen config for the module name (#2201)
Browse files Browse the repository at this point in the history
Modifications:

- Add config to the code generator allowing a different module name to
be specified.
- Remove unused code

Result:

The grpc module name can be specified in generated code
  • Loading branch information
glbrntt authored Feb 28, 2025
1 parent d412128 commit f9fbd68
Show file tree
Hide file tree
Showing 14 changed files with 458 additions and 308 deletions.
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

0 comments on commit f9fbd68

Please sign in to comment.