Skip to content

Commit

Permalink
Only generate SwiftProtobuf import for well-known types (#17)
Browse files Browse the repository at this point in the history
Motivation:

In (#16) an import for SwiftProtobuf was added to account for well-known
types. However, if no well-known types are used then warnings are
emitted when the access level is included on the import.

Modifications:

- Only generate the SwiftProtobuf import for well-known types

Result:

Fewer warnings
  • Loading branch information
glbrntt authored Dec 5, 2024
1 parent 39c40fa commit 97dfdde
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 7 deletions.
18 changes: 16 additions & 2 deletions Sources/GRPCProtobufCodeGen/ProtobufCodeGenParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,23 @@ extension ProtobufCodeGenParser {
file: FileDescriptor
) -> [Dependency] {
var codeDependencies: [Dependency] = [
Dependency(module: "GRPCProtobuf", accessLevel: .internal),
Dependency(module: "SwiftProtobuf", accessLevel: self.accessLevel),
Dependency(module: "GRPCProtobuf", accessLevel: .internal)
]

// If any services in the file depend on well-known Protobuf types then also import
// SwiftProtobuf. Importing SwiftProtobuf unconditionally results in warnings in the generated
// code if access-levels are used on imports and no well-known types are used.
let usesAnyWellKnownTypesInServices = file.services.contains { service in
service.methods.contains { method in
let inputIsWellKnown = method.inputType.wellKnownType != nil
let outputIsWellKnown = method.outputType.wellKnownType != nil
return inputIsWellKnown || outputIsWellKnown
}
}
if usesAnyWellKnownTypesInServices {
codeDependencies.append(Dependency(module: "SwiftProtobuf", accessLevel: self.accessLevel))
}

// Adding as dependencies the modules containing generated code or types for
// '.proto' files imported in the '.proto' file we are parsing.
codeDependencies.append(
Expand Down
Binary file not shown.
28 changes: 24 additions & 4 deletions Tests/GRPCProtobufCodeGenTests/ProtobufCodeGenParserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ struct ProtobufCodeGenParserTests {
@Test("Dependencies")
func dependencies() {
let expected: [GRPCCodeGen.Dependency] = [
.init(module: "GRPCProtobuf", accessLevel: .internal), // Always an internal import
.init(module: "SwiftProtobuf", accessLevel: .internal),
.init(module: "GRPCProtobuf", accessLevel: .internal) // Always an internal import
]
#expect(self.codeGen.dependencies == expected)
}
Expand Down Expand Up @@ -174,8 +173,7 @@ struct ProtobufCodeGenParserTests {
@Test("Dependencies")
func dependencies() {
let expected: [GRPCCodeGen.Dependency] = [
.init(module: "GRPCProtobuf", accessLevel: .internal), // Always an internal import
.init(module: "SwiftProtobuf", accessLevel: .internal),
.init(module: "GRPCProtobuf", accessLevel: .internal) // Always an internal import
]
#expect(self.codeGen.dependencies == expected)
}
Expand Down Expand Up @@ -237,4 +235,26 @@ struct ProtobufCodeGenParserTests {
#expect(self.service.namespace.base == "")
}
}

@Suite("Service using 'well-known types' (wkt-service.proto)")
struct WKTService: UsesDescriptorSet {
static let descriptorSetName = "wkt-service"
static let fileDescriptorName = "wkt-service"

let codeGen: CodeGenerationRequest

init() throws {
let descriptor = try #require(try Self.fileDescriptor)
self.codeGen = try parseDescriptor(descriptor)
}

@Test("Dependencies")
func dependencies() {
let expected: [Dependency] = [
Dependency(module: "GRPCProtobuf", accessLevel: .internal),
Dependency(module: "SwiftProtobuf", accessLevel: .internal),
]
#expect(self.codeGen.dependencies == expected)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ struct ProtobufCodeGeneratorTests: UsesDescriptorSet {
import GRPCCore
import GRPCProtobuf
import SwiftProtobuf
// MARK: - test.TestService
Expand Down
12 changes: 12 additions & 0 deletions dev/protos/generate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,21 @@ function generate_bar_service_descriptor_set {
--include_imports
}

function generate_wkt_service_descriptor_set {
local proto proto_path output
proto="$here/local/wkt-service.proto"
proto_path="$(dirname "$proto")"
output="$root/Tests/GRPCProtobufCodeGenTests/Generated/wkt-service.pb"

invoke_protoc --descriptor_set_out="$output" "$proto" -I "$proto_path" \
--include_source_info \
--include_imports
}

#------------------------------------------------------------------------------

# Descriptor sets
generate_test_service_descriptor_set
generate_foo_service_descriptor_set
generate_bar_service_descriptor_set
generate_wkt_service_descriptor_set
10 changes: 10 additions & 0 deletions dev/protos/local/wkt-service.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Leading trivia.
syntax = "proto3";

package test;

import "google/protobuf/any.proto";

service WKTService {
rpc Method (google.protobuf.Any) returns (google.protobuf.Any) {}
}

0 comments on commit 97dfdde

Please sign in to comment.