Skip to content

Commit 2cdc570

Browse files
authored
Use new structured swift representations (#2128)
Motivation: The new structured Swift representations are more thoroughly tested than the existing translators, so reimplement the translators in terms of the structured Swift. Modifications: - Reimplement translators Result: Most tests remain exactly the same, however the generated client code changes slightly to make the naming of generic placeholders and closure parameters clearer.
1 parent 67efe3c commit 2cdc570

12 files changed

+401
-1488
lines changed

Diff for: Sources/GRPCCodeGen/CodeGenerationRequest.swift

+14
Original file line numberDiff line numberDiff line change
@@ -335,3 +335,17 @@ extension Name {
335335
return self.base.replacingOccurrences(of: ".", with: "_")
336336
}
337337
}
338+
339+
extension ServiceDescriptor {
340+
var namespacedServicePropertyName: String {
341+
let prefix: String
342+
343+
if self.namespace.normalizedBase.isEmpty {
344+
prefix = ""
345+
} else {
346+
prefix = self.namespace.normalizedBase + "_"
347+
}
348+
349+
return prefix + self.name.normalizedBase
350+
}
351+
}

Diff for: Sources/GRPCCodeGen/Internal/StructuredSwift+Server.swift

+10-7
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,16 @@ extension ExtensionDescription {
9797
return ExtensionDescription(
9898
onType: extensionName,
9999
declarations: [
100-
.function(
101-
.registerMethods(
102-
accessLevel: accessLevel,
103-
serviceNamespace: serviceNamespace,
104-
methods: methods,
105-
serializer: serializer,
106-
deserializer: deserializer
100+
.guarded(
101+
.grpc,
102+
.function(
103+
.registerMethods(
104+
accessLevel: accessLevel,
105+
serviceNamespace: serviceNamespace,
106+
methods: methods,
107+
serializer: serializer,
108+
deserializer: deserializer
109+
)
107110
)
108111
)
109112
]

Diff for: Sources/GRPCCodeGen/Internal/StructuredSwift+ServiceMetadata.swift

+40
Original file line numberDiff line numberDiff line change
@@ -302,3 +302,43 @@ extension EnumDescription {
302302
return description
303303
}
304304
}
305+
306+
extension [CodeBlock] {
307+
/// ```
308+
/// enum <Service> {
309+
/// ...
310+
/// }
311+
///
312+
/// extension GRPCCore.ServiceDescriptor {
313+
/// ...
314+
/// }
315+
/// ```
316+
package static func serviceMetadata(
317+
accessModifier: AccessModifier? = nil,
318+
service: ServiceDescriptor,
319+
client: Bool,
320+
server: Bool
321+
) -> Self {
322+
var blocks: [CodeBlock] = []
323+
324+
let serviceNamespace: EnumDescription = .serviceNamespace(
325+
accessModifier: accessModifier,
326+
name: service.namespacedGeneratedName,
327+
serviceDescriptorProperty: service.namespacedServicePropertyName,
328+
client: client,
329+
server: server,
330+
methods: service.methods
331+
)
332+
blocks.append(CodeBlock(item: .declaration(.enum(serviceNamespace))))
333+
334+
let descriptorExtension: ExtensionDescription = .serviceDescriptor(
335+
accessModifier: accessModifier,
336+
propertyName: service.namespacedServicePropertyName,
337+
literalNamespace: service.namespace.base,
338+
literalService: service.name.base
339+
)
340+
blocks.append(CodeBlock(item: .declaration(.extension(descriptorExtension))))
341+
342+
return blocks
343+
}
344+
}

Diff for: Sources/GRPCCodeGen/Internal/StructuredSwiftRepresentation.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ struct ImportDescription: Equatable, Codable, Sendable {
100100
/// A description of an access modifier.
101101
///
102102
/// For example: `public`.
103-
internal enum AccessModifier: String, Sendable, Equatable, Codable, CaseIterable {
103+
package enum AccessModifier: String, Sendable, Equatable, Codable, CaseIterable {
104104
/// A declaration accessible outside of the module.
105105
case `public`
106106

0 commit comments

Comments
 (0)