Skip to content

Commit

Permalink
Simplify typealias translator (#2126)
Browse files Browse the repository at this point in the history
Motivation:

The typealias used to generte an enum per package name and nested enums
for each service within that namespace. In order for that to work, the
services had to be grouped by namespace. This is no longer the case,
each service is generated within a combined package-service enum.

It's simpler to just generate the services in the order they are
created.

Modifications:

- Remove the sorting
- Remove tests which test the unwanted behaviour

Result:

Less code
  • Loading branch information
glbrntt authored Nov 20, 2024
1 parent 3cbc033 commit 5260579
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 384 deletions.
27 changes: 12 additions & 15 deletions Sources/GRPCCodeGen/Internal/Translator/TypealiasTranslator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,25 +81,22 @@ struct TypealiasTranslator: SpecializedTranslator {

func translate(from codeGenerationRequest: CodeGenerationRequest) throws -> [CodeBlock] {
var codeBlocks = [CodeBlock]()
let services = codeGenerationRequest.services
let servicesByEnumName = Dictionary(
grouping: services,
by: { $0.namespacedGeneratedName }
)

// Sorting the keys of the dictionary is necessary so that the generated enums are deterministically ordered.
for (generatedEnumName, services) in servicesByEnumName.sorted(by: { $0.key < $1.key }) {
for service in services {
codeBlocks.append(
CodeBlock(
item: .declaration(try self.makeServiceEnum(from: service, named: generatedEnumName))
for service in codeGenerationRequest.services {
codeBlocks.append(
CodeBlock(
item: .declaration(
try self.makeServiceEnum(
from: service,
named: service.namespacedGeneratedName
)
)
)
)

codeBlocks.append(
CodeBlock(item: .declaration(self.makeServiceDescriptorExtension(for: service)))
)
}
codeBlocks.append(
CodeBlock(item: .declaration(self.makeServiceDescriptorExtension(for: service)))
)
}

return codeBlocks
Expand Down
Loading

0 comments on commit 5260579

Please sign in to comment.