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

Make empty generated source files descriptive #2151

Merged
merged 7 commits into from
Jan 6, 2025

Conversation

rnro
Copy link
Collaborator

@rnro rnro commented Jan 3, 2025

Motivation:

Sometimes protobuf definition files contain no gRPC services and result in an empty file. This is intentional but could be confusing.

Modifications:

Empty files now contain a comment indicating they are intentional.

// This file contained no services.

This is analogous to the behavior of swift-protobuf which adds:

// This file contained no messages, enums, or extensions.

Result:

More descriptive empty source files.

Motivation:

Sometimes protobuf definition files contain no gRPC services and result
in an empty file. This is intentional but could be confusing.

Modifications:

Empty files now contain a comment indicating they are intentional.
```
// This file contained no services.
```

Result:

More descriptive empty source files.
@rnro rnro added the 🔨 semver/patch No public API change. label Jan 3, 2025
@rnro rnro requested a review from glbrntt January 3, 2025 16:20
Comment on lines 98 to 103
if structuredSwiftRepresentation.file.contents.codeBlocks.isEmpty {
structuredSwiftRepresentation.file.contents.imports = []
structuredSwiftRepresentation.file.contents.codeBlocks.append(
CodeBlock(comment: .inline("This file contained no services."))
)
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you do this within the translator? And could you also add a test for this please?

Copy link
Collaborator

@glbrntt glbrntt left a comment

Choose a reason for hiding this comment

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

One nit, looks good otherwise

topComment: .preFormatted(codeGenerationRequest.leadingTrivia),
imports: try self.makeImports(
let imports: [ImportDescription]
if codeBlocks.isEmpty {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we check this instead? It's easy to imagine unconditionally adding a code block which would result in this check failing.

Suggested change
if codeBlocks.isEmpty {
if codeGenerationRequest.services.isEmpty {

@rnro rnro force-pushed the make_empty_files_more_descriptive branch from c270085 to ec55cd5 Compare January 6, 2025 14:59
@rnro rnro requested a review from glbrntt January 6, 2025 15:00
@rnro
Copy link
Collaborator Author

rnro commented Jan 6, 2025

I have switched the import tests to a new set of tests which render the imports directly. This is because this code change meant that the minimal tests which defined no services no longer rendered the imports.

Copy link
Collaborator

@glbrntt glbrntt left a comment

Choose a reason for hiding this comment

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

A few more nits

@@ -87,7 +97,7 @@ struct IDLToStructuredSwiftTranslator: Translator {
return StructuredSwiftRepresentation(file: file)
}

private func makeImports(
internal func makeImports(
Copy link
Collaborator

Choose a reason for hiding this comment

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

package is your friend here

@@ -0,0 +1,200 @@
/*
* Copyright 2024, gRPC Authors All rights reserved.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
* Copyright 2024, gRPC Authors All rights reserved.
* Copyright 2025, gRPC Authors All rights reserved.


import Testing

@testable import GRPCCodeGen
Copy link
Collaborator

Choose a reason for hiding this comment

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

We can use a regular import if you use package appropriately

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

🙏 will do.

Copy link
Collaborator

@glbrntt glbrntt left a comment

Choose a reason for hiding this comment

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

Great, thanks @rnro!

@glbrntt glbrntt enabled auto-merge (squash) January 6, 2025 15:59
@glbrntt glbrntt merged commit 5e92f64 into grpc:main Jan 6, 2025
31 of 33 checks passed
@rnro rnro deleted the make_empty_files_more_descriptive branch January 6, 2025 16:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🔨 semver/patch No public API change.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants