-
Notifications
You must be signed in to change notification settings - Fork 419
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
Conversation
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.
if structuredSwiftRepresentation.file.contents.codeBlocks.isEmpty { | ||
structuredSwiftRepresentation.file.contents.imports = [] | ||
structuredSwiftRepresentation.file.contents.codeBlocks.append( | ||
CodeBlock(comment: .inline("This file contained no services.")) | ||
) | ||
} |
There was a problem hiding this comment.
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?
There was a problem hiding this 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 { |
There was a problem hiding this comment.
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.
if codeBlocks.isEmpty { | |
if codeGenerationRequest.services.isEmpty { |
c270085
to
ec55cd5
Compare
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. |
There was a problem hiding this 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( |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* Copyright 2024, gRPC Authors All rights reserved. | |
* Copyright 2025, gRPC Authors All rights reserved. |
|
||
import Testing | ||
|
||
@testable import GRPCCodeGen |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🙏 will do.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great, thanks @rnro!
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 is analogous to the behavior of swift-protobuf which adds:
Result:
More descriptive empty source files.