Skip to content
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
59 changes: 47 additions & 12 deletions Sources/GRPCCodeGen/CodeGenerationRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -216,17 +216,21 @@ public struct CodeGenerationRequest {
}

/// Represents a service described in an IDL file.
public struct ServiceDescriptor {
public struct ServiceDescriptor: Hashable {
/// Documentation from comments above the IDL service description.
public var documentation: String

/// Service name.
public var name: String
/// The service name in different formats.
///
/// All properties of this object must be unique for each service from within a namespace.
public var name: Name

/// The service namespace.
/// The service namespace in different formats.
///
/// For `.proto` files it is the package name.
public var namespace: String
/// All different services from within the same namespace must have
/// the same ``Name`` object as this property.
/// For `.proto` files the base name of this object is the package name.
public var namespace: Name

/// A description of each method of a service.
///
Expand All @@ -235,8 +239,8 @@ public struct CodeGenerationRequest {

public init(
documentation: String,
name: String,
namespace: String,
name: Name,
namespace: Name,
methods: [MethodDescriptor]
) {
self.documentation = documentation
Expand All @@ -246,12 +250,15 @@ public struct CodeGenerationRequest {
}

/// Represents a method described in an IDL file.
public struct MethodDescriptor {
public struct MethodDescriptor: Hashable {
/// Documentation from comments above the IDL method description.
public var documentation: String

/// Method name.
public var name: String
/// Method name in different formats.
///
/// All properties of this object must be unique for each method
/// from within a service.
public var name: Name

/// Identifies if the method is input streaming.
public var isInputStreaming: Bool
Expand All @@ -267,7 +274,7 @@ public struct CodeGenerationRequest {

public init(
documentation: String,
name: String,
name: Name,
isInputStreaming: Bool,
isOutputStreaming: Bool,
inputType: String,
Expand All @@ -282,4 +289,32 @@ public struct CodeGenerationRequest {
}
}
}

/// Represents the name associated with a namespace, service or a method, in three different formats.
public struct Name: Hashable {
/// The base name is the name used for the namespace/service/method in the IDL file, so it should follow
/// the specific casing of the IDL.
///
/// The base name is also used in the descriptors that identify a specific method or service :
/// `<service_namespace_baseName>.<service_baseName>.<method_baseName>`.
public var base: String

/// The `generatedUpperCase` name is used in the generated code. It is expected
/// to be the UpperCamelCase version of the base name
///
/// For example, if `base` is "fooBar", then `generatedUpperCase` is "FooBar".
public var generatedUpperCase: String

/// The `generatedLowerCase` name is used in the generated code. It is expected
/// to be the lowerCamelCase version of the base name
///
/// For example, if `base` is "FooBar", then `generatedLowerCase` is "fooBar".
public var generatedLowerCase: String

public init(base: String, generatedUpperCase: String, generatedLowerCase: String) {
self.base = base
self.generatedUpperCase = generatedUpperCase
self.generatedLowerCase = generatedLowerCase
}
}
}
40 changes: 22 additions & 18 deletions Sources/GRPCCodeGen/Internal/Translator/ClientCodeTranslator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,23 @@
/// a representation for the following generated code:
///
/// ```swift
/// public protocol foo_BarClientProtocol: Sendable {
/// public protocol Foo_BarClientProtocol: Sendable {
/// func baz<R: Sendable>(
/// request: ClientRequest.Single<foo.Bar.Methods.baz.Input>,
/// serializer: some MessageSerializer<foo.Bar.Methods.baz.Input>,
/// deserializer: some MessageDeserializer<foo.Bar.Methods.baz.Output>,
/// _ body: @Sendable @escaping (ClientResponse.Single<foo.Bar.Methods.baz.Output>) async throws -> R
/// ) async throws -> ServerResponse.Stream<foo.Bar.Methods.bazOutput>
/// _ body: @Sendable @escaping (ClientResponse.Single<foo.Bar.Methods.Baz.Output>) async throws -> R
/// ) async throws -> ServerResponse.Stream<foo.Bar.Methods.Baz.Output>
/// }
/// extension foo.Bar.ClientProtocol {
/// extension Foo.Bar.ClientProtocol {
/// public func get<R: Sendable>(
/// request: ClientRequest.Single<foo.Bar.Methods.baz.Input>,
/// _ body: @Sendable @escaping (ClientResponse.Single<foo.Bar.Methods.baz.Output>) async throws -> R
/// request: ClientRequest.Single<Foo.Bar.Methods.Baz.Input>,
/// _ body: @Sendable @escaping (ClientResponse.Single<Foo.Bar.Methods.Baz.Output>) async throws -> R
/// ) async rethrows -> R {
/// try await self.baz(
/// request: request,
/// serializer: ProtobufSerializer<foo.Bar.Methods.baz.Input>(),
/// deserializer: ProtobufDeserializer<foo.Bar.Methods.baz.Output>(),
/// serializer: ProtobufSerializer<Foo.Bar.Methods.Baz.Input>(),
/// deserializer: ProtobufDeserializer<Foo.Bar.Methods.Baz.Output>(),
/// body
/// )
/// }
Expand All @@ -55,7 +55,7 @@
/// ) async rethrows -> R {
/// try await self.client.clientStreaming(
/// request: request,
/// descriptor: namespaceA.ServiceA.Methods.methodA.descriptor,
/// descriptor: NamespaceA.ServiceA.Methods.MethodA.descriptor,
/// serializer: serializer,
/// deserializer: deserializer,
/// handler: body
Expand Down Expand Up @@ -115,7 +115,7 @@ extension ClientCodeTranslator {
let clientProtocol = Declaration.protocol(
ProtocolDescription(
accessModifier: self.accessModifier,
name: "\(service.namespacedPrefix)ClientProtocol",
name: "\(service.namespacedGeneratedName)ClientProtocol",
conformances: ["Sendable"],
members: methods
)
Expand All @@ -138,7 +138,7 @@ extension ClientCodeTranslator {
}
let clientProtocolExtension = Declaration.extension(
ExtensionDescription(
onType: "\(service.namespacedTypealiasPrefix).ClientProtocol",
onType: "\(service.namespacedTypealiasGeneratedName).ClientProtocol",
declarations: methods
)
)
Expand All @@ -161,7 +161,7 @@ extension ClientCodeTranslator {
let functionSignature = FunctionSignatureDescription(
accessModifier: accessModifier,
kind: .function(
name: method.name,
name: method.name.generatedLowerCase,
isStatic: false
),
generics: [.member("R")],
Expand Down Expand Up @@ -190,7 +190,10 @@ extension ClientCodeTranslator {
) -> [CodeBlock] {
let functionCall = Expression.functionCall(
calledExpression: .memberAccess(
MemberAccessDescription(left: .identifierPattern("self"), right: method.name)
MemberAccessDescription(
left: .identifierPattern("self"),
right: method.name.generatedLowerCase
)
),
arguments: [
FunctionArgumentDescription(label: "request", expression: .identifierPattern("request")),
Expand Down Expand Up @@ -329,8 +332,8 @@ extension ClientCodeTranslator {
return .struct(
StructDescription(
accessModifier: self.accessModifier,
name: "\(service.namespacedPrefix)Client",
conformances: ["\(service.namespacedTypealiasPrefix).ClientProtocol"],
name: "\(service.namespacedGeneratedName)Client",
conformances: ["\(service.namespacedTypealiasGeneratedName).ClientProtocol"],
members: [clientProperty, initializer] + methods
)
)
Expand Down Expand Up @@ -393,7 +396,7 @@ extension ClientCodeTranslator {
.init(
label: "descriptor",
expression: .identifierPattern(
"\(service.namespacedTypealiasPrefix).Methods.\(method.name).descriptor"
"\(service.namespacedTypealiasGeneratedName).Methods.\(method.name.generatedUpperCase).descriptor"
)
),
.init(label: "serializer", expression: .identifierPattern("serializer")),
Expand All @@ -409,7 +412,7 @@ extension ClientCodeTranslator {
return .function(
accessModifier: self.accessModifier,
kind: .function(
name: "\(method.name)",
name: "\(method.name.generatedLowerCase)",
isStatic: false
),
generics: [.member("R")],
Expand All @@ -432,7 +435,8 @@ extension ClientCodeTranslator {
service: CodeGenerationRequest.ServiceDescriptor,
type: InputOutputType
) -> String {
var components: String = "\(service.namespacedTypealiasPrefix).Methods.\(method.name)"
var components: String =
"\(service.namespacedTypealiasGeneratedName).Methods.\(method.name.generatedUpperCase)"

switch type {
case .input:
Expand Down
Loading