Skip to content

Commit cc2c32d

Browse files
authored
Allow access level on imports to be disabled (#2032)
Motivation: The v2 code generator defaults to generating access levels on imports. SwiftProtobuf also has but hasn't yet been released. This causes problems as explicit 'internal' imports in gRPC are ambiguous with the implicit imports from Protobuf. Modifications: - Allow an opt-out of explicit imports for v2 code generation Result: Can build against current version of Protobuf without `InternalImportsByDefault`.
1 parent cb151aa commit cc2c32d

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

Sources/protoc-gen-grpc-swift/main.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ extension SourceGenerator.Configuration {
221221
}
222222
self.init(
223223
accessLevel: accessLevel,
224+
accessLevelOnImports: options.useAccessLevelOnImports,
224225
client: options.generateClient,
225226
server: options.generateServer
226227
)

Sources/protoc-gen-grpc-swift/options.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ final class GeneratorOptions {
7171
#if compiler(>=6.0)
7272
private(set) var v2 = false
7373
#endif
74+
private(set) var useAccessLevelOnImports = true
7475

7576
init(parameter: String?) throws {
7677
for pair in GeneratorOptions.parseParameter(string: parameter) {
@@ -166,6 +167,13 @@ final class GeneratorOptions {
166167
}
167168
#endif
168169

170+
case "UseAccessLevelOnImports":
171+
if let value = Bool(pair.value) {
172+
self.useAccessLevelOnImports = value
173+
} else {
174+
throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
175+
}
176+
169177
default:
170178
throw GenerationError.unknownParameter(name: pair.key)
171179
}

Tests/GRPCProtobufCodeGenTests/ProtobufCodeGeneratorTests.swift

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -471,22 +471,52 @@ final class ProtobufCodeGeneratorTests: XCTestCase {
471471
)
472472
}
473473

474+
func testNoAccessLevelOnImports() throws {
475+
let proto = Google_Protobuf_FileDescriptorProto(name: "helloworld.proto", package: "")
476+
try testCodeGeneration(
477+
proto: proto,
478+
indentation: 2,
479+
visibility: .package,
480+
client: true,
481+
server: true,
482+
accessLevelOnImports: false,
483+
expectedCode: """
484+
// DO NOT EDIT.
485+
// swift-format-ignore-file
486+
//
487+
// Generated by the gRPC Swift generator plugin for the protocol buffer compiler.
488+
// Source: helloworld.proto
489+
//
490+
// For information on using the generated types, please see the documentation:
491+
// https://github.com/grpc/grpc-swift
492+
493+
import GRPCCore
494+
import GRPCProtobuf
495+
import ExtraModule
496+
497+
"""
498+
)
499+
}
500+
474501
func testCodeGeneration(
475502
proto: Google_Protobuf_FileDescriptorProto,
476503
indentation: Int,
477504
visibility: SourceGenerator.Configuration.AccessLevel,
478505
client: Bool,
479506
server: Bool,
507+
accessLevelOnImports: Bool = true,
480508
expectedCode: String,
481509
file: StaticString = #filePath,
482510
line: UInt = #line
483511
) throws {
484-
let configs = SourceGenerator.Configuration(
512+
let config = SourceGenerator.Configuration(
485513
accessLevel: visibility,
514+
accessLevelOnImports: accessLevelOnImports,
486515
client: client,
487516
server: server,
488517
indentation: indentation
489518
)
519+
490520
let descriptorSet = DescriptorSet(
491521
protos: [
492522
Google_Protobuf_FileDescriptorProto(name: "same-module.proto", package: "same-package"),
@@ -512,7 +542,7 @@ final class ProtobufCodeGeneratorTests: XCTestCase {
512542
}
513543
]
514544
}
515-
let generator = ProtobufCodeGenerator(configuration: configs)
545+
let generator = ProtobufCodeGenerator(configuration: config)
516546
try XCTAssertEqualWithDiff(
517547
try generator.generateCode(
518548
from: fileDescriptor,

0 commit comments

Comments
 (0)