Skip to content

Commit 8351d93

Browse files
committed
Fix module naming
1 parent 069ce31 commit 8351d93

File tree

3 files changed

+53
-38
lines changed

3 files changed

+53
-38
lines changed

Sources/protoc-gen-grpc-swift-2/Options.swift

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ struct GeneratorOptions {
6262
}
6363

6464
init(pairs: [(key: String, value: String)]) throws {
65+
var moduleMapPath: String?
66+
6567
for pair in pairs {
6668
switch pair.key {
6769
case "Visibility":
@@ -87,14 +89,7 @@ struct GeneratorOptions {
8789

8890
case "ProtoPathModuleMappings":
8991
if !pair.value.isEmpty {
90-
do {
91-
self.protoToModuleMappings = try ProtoFileToModuleMappings(path: pair.value)
92-
} catch let e {
93-
throw GenerationError.wrappedError(
94-
message: "Parameter 'ProtoPathModuleMappings=\(pair.value)'",
95-
error: e
96-
)
97-
}
92+
moduleMapPath = pair.value
9893
}
9994

10095
case "FileNaming":
@@ -164,6 +159,24 @@ struct GeneratorOptions {
164159
throw GenerationError.unknownParameter(name: pair.key)
165160
}
166161
}
162+
163+
if let moduleMapPath = moduleMapPath {
164+
do {
165+
self.protoToModuleMappings = try ProtoFileToModuleMappings(
166+
path: moduleMapPath,
167+
swiftProtobufModuleName: self.config.moduleNames.swiftProtobuf
168+
)
169+
} catch let e {
170+
throw GenerationError.wrappedError(
171+
message: "Parameter 'ProtoPathModuleMappings=\(moduleMapPath)'",
172+
error: e
173+
)
174+
}
175+
} else {
176+
self.protoToModuleMappings = ProtoFileToModuleMappings(
177+
swiftProtobufModuleName: self.config.moduleNames.swiftProtobuf
178+
)
179+
}
167180
}
168181

169182
static func parseParameter(string: String?) -> [(key: String, value: String)] {

Tests/GRPCProtobufCodeGenTests/ProtobufCodeGenParserTests.swift

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,5 +265,37 @@ struct ProtobufCodeGenParserTests {
265265
]
266266
#expect(codeGen.dependencies == expected)
267267
}
268+
269+
@Test("Generate with different module names")
270+
@available(gRPCSwiftProtobuf 2.0, *)
271+
func generateWithDifferentModuleNames() throws {
272+
var config = ProtobufCodeGenerator.Config.defaults
273+
let defaultNames = config.moduleNames
274+
275+
config.accessLevel = .public
276+
config.indentation = 2
277+
config.moduleNames.grpcCore = String(config.moduleNames.grpcCore.reversed())
278+
config.moduleNames.grpcProtobuf = String(config.moduleNames.grpcProtobuf.reversed())
279+
config.moduleNames.swiftProtobuf = String(config.moduleNames.swiftProtobuf.reversed())
280+
281+
let generator = ProtobufCodeGenerator(config: config)
282+
let generated = try generator.generateCode(
283+
fileDescriptor: Self.fileDescriptor,
284+
protoFileModuleMappings: ProtoFileToModuleMappings(
285+
swiftProtobufModuleName: config.moduleNames.swiftProtobuf
286+
),
287+
extraModuleImports: []
288+
)
289+
290+
// Mustn't contain the default names.
291+
#expect(!generated.contains(defaultNames.grpcCore))
292+
#expect(!generated.contains(defaultNames.grpcProtobuf))
293+
#expect(!generated.contains(defaultNames.swiftProtobuf))
294+
295+
// Must contain the configured names.
296+
#expect(generated.contains(config.moduleNames.grpcCore))
297+
#expect(generated.contains(config.moduleNames.grpcProtobuf))
298+
#expect(generated.contains(config.moduleNames.swiftProtobuf))
299+
}
268300
}
269301
}

Tests/GRPCProtobufCodeGenTests/ProtobufCodeGeneratorTests.swift

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ struct ProtobufCodeGeneratorTests {
9999
100100
import GRPCCore
101101
import GRPCProtobuf
102-
import SwiftProtobuf
103102
104103
// MARK: - test.TestService
105104
@@ -1103,35 +1102,6 @@ struct ProtobufCodeGeneratorTests {
11031102
#expect(generated == expected)
11041103
}
11051104

1106-
@Test("Generate with different module names")
1107-
@available(gRPCSwiftProtobuf 2.0, *)
1108-
func generateWithDifferentModuleNames() throws {
1109-
var config = ProtobufCodeGenerator.Config.defaults
1110-
let defaultNames = config.moduleNames
1111-
1112-
config.accessLevel = .public
1113-
config.indentation = 2
1114-
config.moduleNames.grpcCore = String(config.moduleNames.grpcCore.reversed())
1115-
config.moduleNames.grpcProtobuf = String(config.moduleNames.grpcProtobuf.reversed())
1116-
config.moduleNames.swiftProtobuf = String(config.moduleNames.swiftProtobuf.reversed())
1117-
1118-
let generator = ProtobufCodeGenerator(config: config)
1119-
let generated = try generator.generateCode(
1120-
fileDescriptor: Self.fileDescriptor,
1121-
protoFileModuleMappings: ProtoFileToModuleMappings(),
1122-
extraModuleImports: []
1123-
)
1124-
1125-
// Mustn't contain the default names.
1126-
#expect(!generated.contains(defaultNames.grpcCore))
1127-
#expect(!generated.contains(defaultNames.grpcProtobuf))
1128-
#expect(!generated.contains(defaultNames.swiftProtobuf))
1129-
1130-
// Must contain the configured names.
1131-
#expect(generated.contains(config.moduleNames.grpcCore))
1132-
#expect(generated.contains(config.moduleNames.grpcProtobuf))
1133-
#expect(generated.contains(config.moduleNames.swiftProtobuf))
1134-
}
11351105
}
11361106

11371107
@Suite("File-without-services (foo-messages.proto)")

0 commit comments

Comments
 (0)