Skip to content

Use nested protocols #2132

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

Merged
merged 2 commits into from
Nov 27, 2024
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
105 changes: 40 additions & 65 deletions Sources/GRPCCodeGen/Internal/StructuredSwift+ServiceMetadata.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ extension TypealiasDescription {
extension VariableDescription {
/// ```
/// static let descriptor = GRPCCore.MethodDescriptor(
/// service: <serviceNamespace>.descriptor.fullyQualifiedService,
/// service: GRPCCore.ServiceDescriptor(fullyQualifiedServiceName: "<literalFullyQualifiedService>"),
/// method: "<literalMethodName>"
/// ```
package static func methodDescriptor(
accessModifier: AccessModifier? = nil,
serviceNamespace: String,
literalFullyQualifiedService: String,
literalMethodName: String
) -> Self {
return VariableDescription(
Expand All @@ -62,9 +62,11 @@ extension VariableDescription {
arguments: [
FunctionArgumentDescription(
label: "service",
expression: .identifierType(
.member([serviceNamespace, "descriptor"])
).dot("fullyQualifiedService")
expression: .functionCall(
.serviceDescriptor(
literalFullyQualifiedService: literalFullyQualifiedService
)
)
),
FunctionArgumentDescription(
label: "method",
Expand All @@ -77,18 +79,34 @@ extension VariableDescription {
}

/// ```
/// static let descriptor = GRPCCore.ServiceDescriptor.<namespacedProperty>
/// static let descriptor = GRPCCore.ServiceDescriptor(fullyQualifiedService: <LiteralFullyQualifiedService>)
/// ```
package static func serviceDescriptor(
accessModifier: AccessModifier? = nil,
namespacedProperty: String
literalFullyQualifiedService name: String
) -> Self {
return VariableDescription(
accessModifier: accessModifier,
isStatic: true,
kind: .let,
left: .identifierPattern("descriptor"),
right: .identifier(.type(.serviceDescriptor)).dot(namespacedProperty)
right: .functionCall(.serviceDescriptor(literalFullyQualifiedService: name))
Copy link
Collaborator

Choose a reason for hiding this comment

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

The example has both package and service parameters on the right but the code doesn't seem to reflect that at least to my inexpert eye.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

You're absolutely right, I had an intermediary change which the docs were correct for, I just forgot to update them when I landed on the final change. Good spot!

)
}
}

extension FunctionCallDescription {
package static func serviceDescriptor(
literalFullyQualifiedService: String
) -> Self {
FunctionCallDescription(
calledExpression: .identifier(.type(.serviceDescriptor)),
arguments: [
FunctionArgumentDescription(
label: "fullyQualifiedService",
expression: .literal(literalFullyQualifiedService)
)
]
)
}
}
Expand All @@ -97,16 +115,14 @@ extension ExtensionDescription {
/// ```
/// extension GRPCCore.ServiceDescriptor {
/// static let <PropertyName> = Self(
/// package: "<LiteralNamespaceName>",
/// service: "<LiteralServiceName>"
/// fullyQualifiedService: <LiteralFullyQualifiedService>
/// )
/// }
/// ```
package static func serviceDescriptor(
accessModifier: AccessModifier? = nil,
propertyName: String,
literalNamespace: String,
literalService: String
literalFullyQualifiedService: String
) -> ExtensionDescription {
return ExtensionDescription(
onType: "GRPCCore.ServiceDescriptor",
Expand All @@ -117,17 +133,7 @@ extension ExtensionDescription {
kind: .let,
left: .identifier(.pattern(propertyName)),
right: .functionCall(
calledExpression: .identifierType(.member("Self")),
arguments: [
FunctionArgumentDescription(
label: "package",
expression: .literal(literalNamespace)
),
FunctionArgumentDescription(
label: "service",
expression: .literal(literalService)
),
]
.serviceDescriptor(literalFullyQualifiedService: literalFullyQualifiedService)
)
)
]
Expand Down Expand Up @@ -169,7 +175,7 @@ extension EnumDescription {
accessModifier: AccessModifier? = nil,
name: String,
literalMethod: String,
serviceNamespace: String,
literalFullyQualifiedService: String,
inputType: String,
outputType: String
) -> Self {
Expand All @@ -182,7 +188,7 @@ extension EnumDescription {
.variable(
.methodDescriptor(
accessModifier: accessModifier,
serviceNamespace: serviceNamespace,
literalFullyQualifiedService: literalFullyQualifiedService,
literalMethodName: literalMethod
)
),
Expand All @@ -209,7 +215,7 @@ extension EnumDescription {
/// ```
package static func methodsNamespace(
accessModifier: AccessModifier? = nil,
serviceNamespace: String,
literalFullyQualifiedService: String,
methods: [MethodDescriptor]
) -> EnumDescription {
var description = EnumDescription(accessModifier: accessModifier, name: "Method")
Expand All @@ -221,7 +227,7 @@ extension EnumDescription {
accessModifier: accessModifier,
name: method.name.base,
literalMethod: method.name.base,
serviceNamespace: serviceNamespace,
literalFullyQualifiedService: literalFullyQualifiedService,
inputType: method.inputType,
outputType: method.outputType
)
Expand All @@ -245,57 +251,31 @@ extension EnumDescription {
/// enum Method {
/// ...
/// }
/// @available(...)
/// typealias StreamingServiceProtocol = ...
/// @available(...)
/// typealias ServiceProtocol = ...
/// ...
/// }
/// ```
package static func serviceNamespace(
accessModifier: AccessModifier? = nil,
name: String,
serviceDescriptorProperty: String,
client: Bool,
server: Bool,
literalFullyQualifiedService: String,
methods: [MethodDescriptor]
) -> EnumDescription {
var description = EnumDescription(accessModifier: accessModifier, name: name)

// static let descriptor = GRPCCore.ServiceDescriptor.<namespacedServicePropertyName>
// static let descriptor = GRPCCore.ServiceDescriptor(fullyQualifiedService: "...")
let descriptor = VariableDescription.serviceDescriptor(
accessModifier: accessModifier,
namespacedProperty: serviceDescriptorProperty
literalFullyQualifiedService: literalFullyQualifiedService
)
description.members.append(.variable(descriptor))

// enum Method { ... }
let methodsNamespace: EnumDescription = .methodsNamespace(
accessModifier: accessModifier,
serviceNamespace: name,
literalFullyQualifiedService: literalFullyQualifiedService,
methods: methods
)
description.members.append(.enum(methodsNamespace))

// Typealiases for the various protocols.
var typealiasNames: [String] = []
if server {
typealiasNames.append("StreamingServiceProtocol")
typealiasNames.append("ServiceProtocol")
}
if client {
typealiasNames.append("ClientProtocol")
typealiasNames.append("Client")
}
let typealiases: [Declaration] = typealiasNames.map { alias in
.typealias(
accessModifier: accessModifier,
name: alias,
existingType: .member(name + "_" + alias)
)
}
description.members.append(contentsOf: typealiases)

return description
}
}
Expand All @@ -312,27 +292,22 @@ extension [CodeBlock] {
/// ```
package static func serviceMetadata(
accessModifier: AccessModifier? = nil,
service: ServiceDescriptor,
client: Bool,
server: Bool
service: ServiceDescriptor
) -> Self {
var blocks: [CodeBlock] = []

let serviceNamespace: EnumDescription = .serviceNamespace(
accessModifier: accessModifier,
name: service.namespacedGeneratedName,
serviceDescriptorProperty: service.namespacedServicePropertyName,
client: client,
server: server,
literalFullyQualifiedService: service.fullyQualifiedName,
methods: service.methods
)
blocks.append(CodeBlock(item: .declaration(.enum(serviceNamespace))))

let descriptorExtension: ExtensionDescription = .serviceDescriptor(
accessModifier: accessModifier,
propertyName: service.namespacedServicePropertyName,
literalNamespace: service.namespace.base,
literalService: service.name.base
literalFullyQualifiedService: service.fullyQualifiedName
)
blocks.append(CodeBlock(item: .declaration(.extension(descriptorExtension))))

Expand Down
60 changes: 31 additions & 29 deletions Sources/GRPCCodeGen/Internal/Translator/ClientCodeTranslator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,25 +86,41 @@ struct ClientCodeTranslator {
) -> [CodeBlock] {
var blocks = [CodeBlock]()

let protocolName = "\(service.namespacedGeneratedName)_ClientProtocol"
let protocolTypealias = "\(service.namespacedGeneratedName).ClientProtocol"
let structName = "\(service.namespacedGeneratedName)_Client"
let `extension` = ExtensionDescription(
onType: service.namespacedGeneratedName,
declarations: [
// protocol ClientProtocol { ... }
.commentable(
.preFormatted(service.documentation),
.protocol(
.clientProtocol(
accessLevel: accessModifier,
name: "ClientProtocol",
methods: service.methods
)
)
),

let clientProtocol: ProtocolDescription = .clientProtocol(
accessLevel: accessModifier,
name: protocolName,
methods: service.methods
)
blocks.append(
CodeBlock(
comment: .preFormatted(service.documentation),
item: .declaration(.protocol(clientProtocol))
)
// struct Client: ClientProtocol { ... }
.commentable(
.preFormatted(service.documentation),
.struct(
.client(
accessLevel: accessModifier,
name: "Client",
serviceEnum: service.namespacedGeneratedName,
clientProtocol: "ClientProtocol",
methods: service.methods
)
)
),
]
)
blocks.append(.declaration(.extension(`extension`)))

let extensionWithDefaults: ExtensionDescription = .clientMethodSignatureWithDefaults(
accessLevel: accessModifier,
name: protocolTypealias,
name: "\(service.namespacedGeneratedName).ClientProtocol",
methods: service.methods,
serializer: serializer,
deserializer: deserializer
Expand All @@ -115,27 +131,13 @@ struct ClientCodeTranslator {

let extensionWithExplodedAPI: ExtensionDescription = .explodedClientMethods(
accessLevel: accessModifier,
on: protocolTypealias,
on: "\(service.namespacedGeneratedName).ClientProtocol",
methods: service.methods
)
blocks.append(
CodeBlock(item: .declaration(.extension(extensionWithExplodedAPI)))
)

let clientStruct: StructDescription = .client(
accessLevel: accessModifier,
name: structName,
serviceEnum: service.namespacedGeneratedName,
clientProtocol: protocolTypealias,
methods: service.methods
)
blocks.append(
CodeBlock(
comment: .preFormatted(service.documentation),
item: .declaration(.struct(clientStruct))
)
)

return blocks
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ struct IDLToStructuredSwiftTranslator: Translator {

let metadata = metadataTranslator.translate(
accessModifier: accessModifier,
service: service,
client: client,
server: server
service: service
)
codeBlocks.append(contentsOf: metadata)

Expand Down
11 changes: 2 additions & 9 deletions Sources/GRPCCodeGen/Internal/Translator/MetadataTranslator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,8 @@ struct MetadataTranslator {

func translate(
accessModifier: AccessModifier,
service: ServiceDescriptor,
client: Bool,
server: Bool
service: ServiceDescriptor
) -> [CodeBlock] {
.serviceMetadata(
accessModifier: accessModifier,
service: service,
client: client,
server: server
)
.serviceMetadata(accessModifier: accessModifier, service: service)
}
}
Loading
Loading