Browse Source

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`.
George Barnett 1 year ago
parent
commit
cc2c32d67d

+ 1 - 0
Sources/protoc-gen-grpc-swift/main.swift

@@ -221,6 +221,7 @@ extension SourceGenerator.Configuration {
     }
     self.init(
       accessLevel: accessLevel,
+      accessLevelOnImports: options.useAccessLevelOnImports,
       client: options.generateClient,
       server: options.generateServer
     )

+ 8 - 0
Sources/protoc-gen-grpc-swift/options.swift

@@ -71,6 +71,7 @@ final class GeneratorOptions {
   #if compiler(>=6.0)
   private(set) var v2 = false
   #endif
+  private(set) var useAccessLevelOnImports = true
 
   init(parameter: String?) throws {
     for pair in GeneratorOptions.parseParameter(string: parameter) {
@@ -166,6 +167,13 @@ final class GeneratorOptions {
         }
       #endif
 
+      case "UseAccessLevelOnImports":
+        if let value = Bool(pair.value) {
+          self.useAccessLevelOnImports = value
+        } else {
+          throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
+        }
+
       default:
         throw GenerationError.unknownParameter(name: pair.key)
       }

+ 32 - 2
Tests/GRPCProtobufCodeGenTests/ProtobufCodeGeneratorTests.swift

@@ -471,22 +471,52 @@ final class ProtobufCodeGeneratorTests: XCTestCase {
     )
   }
 
+  func testNoAccessLevelOnImports() throws {
+    let proto = Google_Protobuf_FileDescriptorProto(name: "helloworld.proto", package: "")
+    try testCodeGeneration(
+      proto: proto,
+      indentation: 2,
+      visibility: .package,
+      client: true,
+      server: true,
+      accessLevelOnImports: false,
+      expectedCode: """
+        // DO NOT EDIT.
+        // swift-format-ignore-file
+        //
+        // Generated by the gRPC Swift generator plugin for the protocol buffer compiler.
+        // Source: helloworld.proto
+        //
+        // For information on using the generated types, please see the documentation:
+        //   https://github.com/grpc/grpc-swift
+
+        import GRPCCore
+        import GRPCProtobuf
+        import ExtraModule
+
+        """
+    )
+  }
+
   func testCodeGeneration(
     proto: Google_Protobuf_FileDescriptorProto,
     indentation: Int,
     visibility: SourceGenerator.Configuration.AccessLevel,
     client: Bool,
     server: Bool,
+    accessLevelOnImports: Bool = true,
     expectedCode: String,
     file: StaticString = #filePath,
     line: UInt = #line
   ) throws {
-    let configs = SourceGenerator.Configuration(
+    let config = SourceGenerator.Configuration(
       accessLevel: visibility,
+      accessLevelOnImports: accessLevelOnImports,
       client: client,
       server: server,
       indentation: indentation
     )
+
     let descriptorSet = DescriptorSet(
       protos: [
         Google_Protobuf_FileDescriptorProto(name: "same-module.proto", package: "same-package"),
@@ -512,7 +542,7 @@ final class ProtobufCodeGeneratorTests: XCTestCase {
         }
       ]
     }
-    let generator = ProtobufCodeGenerator(configuration: configs)
+    let generator = ProtobufCodeGenerator(configuration: config)
     try XCTAssertEqualWithDiff(
       try generator.generateCode(
         from: fileDescriptor,