Skip to content

Commit

Permalink
Simplify naming of code gen types
Browse files Browse the repository at this point in the history
Motivation:

A number of types are nested within 'CodeGenerationRequest' which make
them verbose to type out and a bit of a pain to work with.

Modification:

Remove the nesting of types.

Result:

Easier types to work with.
  • Loading branch information
glbrntt committed Nov 13, 2024
1 parent c3f09df commit 704dbed
Show file tree
Hide file tree
Showing 10 changed files with 301 additions and 317 deletions.
436 changes: 218 additions & 218 deletions Sources/GRPCCodeGen/CodeGenerationRequest.swift

Large diffs are not rendered by default.

46 changes: 23 additions & 23 deletions Sources/GRPCCodeGen/Internal/Translator/ClientCodeTranslator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ struct ClientCodeTranslator: SpecializedTranslator {

extension ClientCodeTranslator {
private func makeClientProtocol(
for service: CodeGenerationRequest.ServiceDescriptor,
for service: ServiceDescriptor,
in codeGenerationRequest: CodeGenerationRequest
) -> Declaration {
let methods = service.methods.map {
Expand All @@ -130,7 +130,7 @@ extension ClientCodeTranslator {
}

private func makeDefaultImplementation(
for service: CodeGenerationRequest.ServiceDescriptor,
for service: ServiceDescriptor,
in codeGenerationRequest: CodeGenerationRequest
) -> Declaration {
let methods = service.methods.map {
Expand All @@ -156,7 +156,7 @@ extension ClientCodeTranslator {
}

private func makeSugaredAPI(
forService service: CodeGenerationRequest.ServiceDescriptor,
forService service: ServiceDescriptor,
request: CodeGenerationRequest
) -> Declaration {
let sugaredAPIExtension = Declaration.extension(
Expand All @@ -175,7 +175,7 @@ extension ClientCodeTranslator {
}

private func makeSugaredMethodDeclaration(
method: CodeGenerationRequest.ServiceDescriptor.MethodDescriptor,
method: MethodDescriptor,
accessModifier: AccessModifier?
) -> Declaration {
let signature = FunctionSignatureDescription(
Expand Down Expand Up @@ -205,7 +205,7 @@ extension ClientCodeTranslator {
}

private func makeParametersForSugaredMethodDeclaration(
method: CodeGenerationRequest.ServiceDescriptor.MethodDescriptor
method: MethodDescriptor
) -> [ParameterDescription] {
var parameters = [ParameterDescription]()

Expand Down Expand Up @@ -295,7 +295,7 @@ extension ClientCodeTranslator {
}

private func makeFunctionBodyForSugaredMethodDeclaration(
method: CodeGenerationRequest.ServiceDescriptor.MethodDescriptor
method: MethodDescriptor
) -> [CodeBlock] {
// Produces the following:
//
Expand Down Expand Up @@ -375,8 +375,8 @@ extension ClientCodeTranslator {
}

private func makeClientProtocolMethod(
for method: CodeGenerationRequest.ServiceDescriptor.MethodDescriptor,
in service: CodeGenerationRequest.ServiceDescriptor,
for method: MethodDescriptor,
in service: ServiceDescriptor,
from codeGenerationRequest: CodeGenerationRequest,
includeBody: Bool,
accessModifier: AccessModifier? = nil,
Expand Down Expand Up @@ -421,8 +421,8 @@ extension ClientCodeTranslator {
}

private func makeClientProtocolMethodCall(
for method: CodeGenerationRequest.ServiceDescriptor.MethodDescriptor,
in service: CodeGenerationRequest.ServiceDescriptor,
for method: MethodDescriptor,
in service: ServiceDescriptor,
from codeGenerationRequest: CodeGenerationRequest
) -> [CodeBlock] {
let functionCall = Expression.functionCall(
Expand Down Expand Up @@ -455,8 +455,8 @@ extension ClientCodeTranslator {
}

private func makeParameters(
for method: CodeGenerationRequest.ServiceDescriptor.MethodDescriptor,
in service: CodeGenerationRequest.ServiceDescriptor,
for method: MethodDescriptor,
in service: ServiceDescriptor,
from codeGenerationRequest: CodeGenerationRequest,
includeSerializationParameters: Bool,
includeDefaultCallOptions: Bool,
Expand Down Expand Up @@ -487,8 +487,8 @@ extension ClientCodeTranslator {
return parameters
}
private func clientRequestParameter(
for method: CodeGenerationRequest.ServiceDescriptor.MethodDescriptor,
in service: CodeGenerationRequest.ServiceDescriptor
for method: MethodDescriptor,
in service: ServiceDescriptor
) -> ParameterDescription {
let requestType = method.isInputStreaming ? "Streaming" : ""
let clientRequestType = ExistingTypeDescription.member([
Expand All @@ -505,8 +505,8 @@ extension ClientCodeTranslator {
}

private func serializerParameter(
for method: CodeGenerationRequest.ServiceDescriptor.MethodDescriptor,
in service: CodeGenerationRequest.ServiceDescriptor
for method: MethodDescriptor,
in service: ServiceDescriptor
) -> ParameterDescription {
return ParameterDescription(
label: "serializer",
Expand All @@ -520,8 +520,8 @@ extension ClientCodeTranslator {
}

private func deserializerParameter(
for method: CodeGenerationRequest.ServiceDescriptor.MethodDescriptor,
in service: CodeGenerationRequest.ServiceDescriptor
for method: MethodDescriptor,
in service: ServiceDescriptor
) -> ParameterDescription {
return ParameterDescription(
label: "deserializer",
Expand All @@ -535,8 +535,8 @@ extension ClientCodeTranslator {
}

private func bodyParameter(
for method: CodeGenerationRequest.ServiceDescriptor.MethodDescriptor,
in service: CodeGenerationRequest.ServiceDescriptor,
for method: MethodDescriptor,
in service: ServiceDescriptor,
includeDefaultResponseHandler: Bool
) -> ParameterDescription {
let clientStreaming = method.isOutputStreaming ? "Streaming" : ""
Expand Down Expand Up @@ -571,7 +571,7 @@ extension ClientCodeTranslator {
}

private func makeClientStruct(
for service: CodeGenerationRequest.ServiceDescriptor,
for service: ServiceDescriptor,
in codeGenerationRequest: CodeGenerationRequest
) -> Declaration {
let clientProperty = Declaration.variable(
Expand Down Expand Up @@ -637,8 +637,8 @@ extension ClientCodeTranslator {
}

private func makeClientMethod(
for method: CodeGenerationRequest.ServiceDescriptor.MethodDescriptor,
in service: CodeGenerationRequest.ServiceDescriptor,
for method: MethodDescriptor,
in service: ServiceDescriptor,
from codeGenerationRequest: CodeGenerationRequest
) -> Declaration {
let parameters = self.makeParameters(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ struct IDLToStructuredSwiftTranslator: Translator {
}

private func makeImports(
dependencies: [CodeGenerationRequest.Dependency],
dependencies: [Dependency],
accessLevel: SourceGenerator.Config.AccessLevel,
accessLevelOnImports: Bool
) throws -> [ImportDescription] {
Expand Down Expand Up @@ -98,7 +98,7 @@ extension AccessModifier {

extension IDLToStructuredSwiftTranslator {
private func translateImport(
dependency: CodeGenerationRequest.Dependency,
dependency: Dependency,
accessLevelOnImports: Bool
) throws -> ImportDescription {
var importDescription = ImportDescription(
Expand Down Expand Up @@ -146,7 +146,7 @@ extension IDLToStructuredSwiftTranslator {

// Verify service enum names are unique.
private func checkServiceEnumNamesAreUnique(
for servicesByGeneratedEnumName: [String: [CodeGenerationRequest.ServiceDescriptor]]
for servicesByGeneratedEnumName: [String: [ServiceDescriptor]]
) throws {
for (generatedEnumName, services) in servicesByGeneratedEnumName {
if services.count > 1 {
Expand All @@ -163,7 +163,7 @@ extension IDLToStructuredSwiftTranslator {

// Verify method names are unique within a service.
private func checkMethodNamesAreUnique(
in service: CodeGenerationRequest.ServiceDescriptor
in service: ServiceDescriptor
) throws {
// Check that the method descriptors are unique, by checking that the base names
// of the methods of a specific service are unique.
Expand Down Expand Up @@ -206,7 +206,7 @@ extension IDLToStructuredSwiftTranslator {
}

private func checkServiceDescriptorsAreUnique(
_ services: [CodeGenerationRequest.ServiceDescriptor]
_ services: [ServiceDescriptor]
) throws {
var descriptors: Set<String> = []
for service in services {
Expand All @@ -227,7 +227,7 @@ extension IDLToStructuredSwiftTranslator {
}
}

extension CodeGenerationRequest.ServiceDescriptor {
extension ServiceDescriptor {
var namespacedGeneratedName: String {
if self.namespace.generatedUpperCase.isEmpty {
return self.name.generatedUpperCase
Expand Down
40 changes: 20 additions & 20 deletions Sources/GRPCCodeGen/Internal/Translator/ServerCodeTranslator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ struct ServerCodeTranslator: SpecializedTranslator {

extension ServerCodeTranslator {
private func makeStreamingProtocol(
for service: CodeGenerationRequest.ServiceDescriptor
for service: ServiceDescriptor
) -> Declaration {
let methods = service.methods.compactMap {
Declaration.commentable(
Expand Down Expand Up @@ -136,8 +136,8 @@ extension ServerCodeTranslator {
}

private func makeStreamingMethodSignature(
for method: CodeGenerationRequest.ServiceDescriptor.MethodDescriptor,
in service: CodeGenerationRequest.ServiceDescriptor,
for method: MethodDescriptor,
in service: ServiceDescriptor,
accessModifier: AccessModifier? = nil
) -> FunctionSignatureDescription {
return FunctionSignatureDescription(
Expand All @@ -164,7 +164,7 @@ extension ServerCodeTranslator {
}

private func makeConformanceToRPCServiceExtension(
for service: CodeGenerationRequest.ServiceDescriptor,
for service: ServiceDescriptor,
in codeGenerationRequest: CodeGenerationRequest
) -> Declaration {
let streamingProtocol = self.protocolNameTypealias(service: service, streaming: true)
Expand All @@ -179,7 +179,7 @@ extension ServerCodeTranslator {
}

private func makeRegisterRPCsMethod(
for service: CodeGenerationRequest.ServiceDescriptor,
for service: ServiceDescriptor,
in codeGenerationRequest: CodeGenerationRequest
) -> Declaration {
let registerRPCsSignature = FunctionSignatureDescription(
Expand All @@ -202,7 +202,7 @@ extension ServerCodeTranslator {
}

private func makeRegisterRPCsMethodBody(
for service: CodeGenerationRequest.ServiceDescriptor,
for service: ServiceDescriptor,
in codeGenerationRequest: CodeGenerationRequest
) -> [CodeBlock] {
let registerHandlerCalls = service.methods.compactMap {
Expand All @@ -224,8 +224,8 @@ extension ServerCodeTranslator {
}

private func makeArgumentsForRegisterHandler(
for method: CodeGenerationRequest.ServiceDescriptor.MethodDescriptor,
in service: CodeGenerationRequest.ServiceDescriptor,
for method: MethodDescriptor,
in service: ServiceDescriptor,
from codeGenerationRequest: CodeGenerationRequest
) -> [FunctionArgumentDescription] {
var arguments = [FunctionArgumentDescription]()
Expand Down Expand Up @@ -284,7 +284,7 @@ extension ServerCodeTranslator {
}

private func makeServiceProtocol(
for service: CodeGenerationRequest.ServiceDescriptor
for service: ServiceDescriptor
) -> Declaration {
let methods = service.methods.compactMap {
self.makeServiceProtocolMethod(for: $0, in: service)
Expand All @@ -309,8 +309,8 @@ extension ServerCodeTranslator {
}

private func makeServiceProtocolMethod(
for method: CodeGenerationRequest.ServiceDescriptor.MethodDescriptor,
in service: CodeGenerationRequest.ServiceDescriptor,
for method: MethodDescriptor,
in service: ServiceDescriptor,
accessModifier: AccessModifier? = nil
) -> Declaration {
let inputStreaming = method.isInputStreaming ? "Streaming" : ""
Expand Down Expand Up @@ -346,7 +346,7 @@ extension ServerCodeTranslator {
}

private func makeExtensionServiceProtocol(
for service: CodeGenerationRequest.ServiceDescriptor
for service: ServiceDescriptor
) -> Declaration {
let methods = service.methods.compactMap {
self.makeServiceProtocolExtensionMethod(for: $0, in: service)
Expand All @@ -363,8 +363,8 @@ extension ServerCodeTranslator {
}

private func makeServiceProtocolExtensionMethod(
for method: CodeGenerationRequest.ServiceDescriptor.MethodDescriptor,
in service: CodeGenerationRequest.ServiceDescriptor
for method: MethodDescriptor,
in service: ServiceDescriptor
) -> Declaration? {
// The method has the same definition in StreamingServiceProtocol and ServiceProtocol.
if method.isInputStreaming && method.isOutputStreaming {
Expand All @@ -385,7 +385,7 @@ extension ServerCodeTranslator {
}

private func makeResponse(
for method: CodeGenerationRequest.ServiceDescriptor.MethodDescriptor
for method: MethodDescriptor
) -> Declaration {
let serverRequest: Expression
if !method.isInputStreaming {
Expand Down Expand Up @@ -422,7 +422,7 @@ extension ServerCodeTranslator {
}

private func makeReturnStatement(
for method: CodeGenerationRequest.ServiceDescriptor.MethodDescriptor
for method: MethodDescriptor
) -> Expression {
let returnValue: Expression
// Transforming the unary response into a streaming one.
Expand All @@ -447,16 +447,16 @@ extension ServerCodeTranslator {

/// Generates the fully qualified name of a method descriptor.
private func methodDescriptorPath(
for method: CodeGenerationRequest.ServiceDescriptor.MethodDescriptor,
service: CodeGenerationRequest.ServiceDescriptor
for method: MethodDescriptor,
service: ServiceDescriptor
) -> String {
return
"\(service.namespacedGeneratedName).Method.\(method.name.generatedUpperCase).descriptor"
}

/// Generates the fully qualified name of the type alias for a service protocol.
internal func protocolNameTypealias(
service: CodeGenerationRequest.ServiceDescriptor,
service: ServiceDescriptor,
streaming: Bool
) -> String {
if streaming {
Expand All @@ -467,7 +467,7 @@ extension ServerCodeTranslator {

/// Generates the name of a service protocol.
internal func protocolName(
service: CodeGenerationRequest.ServiceDescriptor,
service: ServiceDescriptor,
streaming: Bool
) -> String {
if streaming {
Expand Down
Loading

0 comments on commit 704dbed

Please sign in to comment.