Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate code file for protos with no definitions #27

Merged
merged 3 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ let products: [Product] = [
let dependencies: [Package.Dependency] = [
.package(
url: "https://github.com/grpc/grpc-swift.git",
exact: "2.0.0-beta.2"
branch: "main"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@glbrntt note this change. I'm happy to take a different approach than this if you'd rather.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, this is good. We only switched to exact for the tagged releases, main is fine between releases.

),
.package(
url: "https://github.com/apple/swift-protobuf.git",
Expand Down
8 changes: 5 additions & 3 deletions Sources/GRPCProtobufCodeGen/ProtobufCodeGenParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,11 @@ package struct ProtobufCodeGenParser {

extension ProtobufCodeGenParser {
fileprivate func codeDependencies(file: FileDescriptor) -> [Dependency] {
var codeDependencies: [Dependency] = [
Dependency(module: "GRPCProtobuf", accessLevel: .internal)
]
var codeDependencies: [Dependency] = []

if file.services.count > 0 {
codeDependencies.append(Dependency(module: "GRPCProtobuf", accessLevel: .internal))
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't quite right: we'll still generate some imports in some cases unnecessarily (i.e. if the file defines a message which depends on a type which is bundled with SwiftProtobuf). We should return the empty array if there are no services.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, good point


// If there's a dependency on a bundled proto then add the SwiftProtobuf import.
//
Expand Down
4 changes: 0 additions & 4 deletions Sources/protoc-gen-grpc-swift/GenerateGRPC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,6 @@ final class GenerateGRPC: CodeGenerator {
)
}

if descriptor.services.isEmpty {
continue
}

try self.generateV2Stubs(descriptor, options: options, outputs: outputs)
}
}
Expand Down
Binary file not shown.
Binary file modified Tests/GRPCProtobufCodeGenTests/Generated/foo-service.pb
Binary file not shown.
1,667 changes: 856 additions & 811 deletions Tests/GRPCProtobufCodeGenTests/ProtobufCodeGeneratorTests.swift

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions dev/protos/generate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,17 @@ function generate_foo_service_descriptor_set {
--include_imports
}

function generate_foo_messages_descriptor_set {
local proto proto_path output
proto="$here/local/foo-messages.proto"
proto_path="$(dirname "$proto")"
output="$root/Tests/GRPCProtobufCodeGenTests/Generated/foo-messages.pb"

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

function generate_bar_service_descriptor_set {
local proto proto_path output
proto="$here/local/bar-service.proto"
Expand Down Expand Up @@ -152,5 +163,6 @@ generate_error_service
# Descriptor sets for tests
generate_test_service_descriptor_set
generate_foo_service_descriptor_set
generate_foo_messages_descriptor_set
generate_bar_service_descriptor_set
generate_wkt_service_descriptor_set
1 change: 1 addition & 0 deletions dev/protos/local/foo-messages.proto
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Leading trivia.
syntax = "proto3";

package foo;
Expand Down
Loading