Skip to content

Commit b8e2be8

Browse files
committed
Expose if there are any module mappings.
Small optimization to the imports generation to do no work if there are no mappings.
1 parent bc8ed9a commit b8e2be8

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

Sources/SwiftProtobufPluginLibrary/ProtoFileToModuleMappings.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ public struct ProtoFileToModuleMappings {
3838
/// access it to verify things.
3939
let mappings: [String:String]
4040

41+
/// A Boolean value that indicates that there were developer provided
42+
/// mappings.
43+
///
44+
/// Since `mappings` will have the bundled proto files also, this is used
45+
/// to track whether there are any provided mappings.
46+
public let hasMappings: Bool
47+
4148
/// The name of the runtime module for SwiftProtobuf (usually "SwiftProtobuf").
4249
/// We expect to find the WKTs in the module named here.
4350
public let swiftProtobufModuleName: String
@@ -71,6 +78,7 @@ public struct ProtoFileToModuleMappings {
7178
public init(moduleMappingsProto mappings: SwiftProtobuf_GenSwift_ModuleMappings, swiftProtobufModuleName: String?) throws {
7279
self.swiftProtobufModuleName = swiftProtobufModuleName ?? defaultSwiftProtobufModuleName
7380
var builder = wktMappings(swiftProtobufModuleName: self.swiftProtobufModuleName)
81+
let initialCount = builder.count
7482
for (idx, mapping) in mappings.mapping.lazy.enumerated() {
7583
if mapping.moduleName.isEmpty {
7684
throw LoadError.entryMissingModuleName(mappingIndex: idx)
@@ -92,6 +100,7 @@ public struct ProtoFileToModuleMappings {
92100
}
93101
}
94102
self.mappings = builder
103+
self.hasMappings = initialCount != builder.count
95104
}
96105

97106
public init() {
@@ -110,6 +119,7 @@ public struct ProtoFileToModuleMappings {
110119
/// Returns the list of modules that need to be imported for a given file based on
111120
/// the dependencies it has.
112121
public func neededModules(forFile file: FileDescriptor) -> [String]? {
122+
guard hasMappings else { return nil }
113123
if file.dependencies.isEmpty {
114124
return nil
115125
}

Tests/SwiftProtobufPluginLibraryTests/Test_ProtoFileToModuleMappings.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ final class Test_ProtoFileToModuleMappings: XCTestCase {
7676
let mapper = try ProtoFileToModuleMappings(moduleMappingsProto: config)
7777
XCTAssertEqual(mapper.mappings.count, expectMappings + baselineEntries, "Index: \(idx)")
7878
XCTAssertEqual(Set(mapper.mappings.values).count, expectedModules + baselineModules, "Index: \(idx)")
79+
XCTAssert(mapper.hasMappings == (expectMappings != 0), "Index: \(idx)")
7980
} catch let error {
8081
XCTFail("Index \(idx) - Unexpected error: \(error)")
8182
}

0 commit comments

Comments
 (0)