-
Notifications
You must be signed in to change notification settings - Fork 420
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
Allow generating test stubs separately from the client implementation #402
Conversation
Sorry, it's still unclear to me how the logic has changed, especially with regards to how it used to behave depending on the Client/Server/TestStubs flags and how it behaves now. Would you mind elaborating further? It also looks like your changes only affect the generated code for the client, not the server? Should this be done for both? |
There are effectively 3 changes here:
Feedback is welcome. I'm happy to farm off smaller PRs if you feel any parts of this are less controversial, or would make it easier to review. |
@mpetrov thank you for the elaboration! 1. and 2. sound reasonable to me. Same for 3., but I agree that we should have more granular options for this. Sounds like we have four different binary flags here, but not all combinations would make sense, for example this one:
So far, I tend towards having the following options:
So these would be the conditions to generate the corresponding code:
Thoughts? |
This will make it simpler to conditionally turn of generating the implementation, so that test stubs can be compiled separately. See PR grpc#402 for more discussion.
This will make it simpler to conditionally turn of generating the implementation, so that test stubs can be compiled separately. See PR #402 for more discussion.
@rebello95 any preference on naming? Otherwise, I would suggest |
Fix unary test stub
Rebased to include #403 and add new option. |
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.
Thank you for these changes! I'll merge once @rebello95 has taken a look as well.
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.
Adding these options is a useful change overall, some comments
README.md
Outdated
@@ -102,8 +102,10 @@ separated from the output directory by a colon. | |||
| `Client` | `true`/`false` | `true` | Whether to generate client code | | |||
| `Async` | `true`/`false` | `true` | Whether to generate asynchronous code | | |||
| `Sync` | `true`/`false` | `true` | Whether to generate synchronous code | | |||
| `Implementations` | `true`/`false` | `true` | Whether to generate protocols and non-test service code. Toggling this to `false` is mostly useful when combined with `TestStubs=true` to generate files containing only test stub code. | |
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.
Nit: Other options don't have a trailing period in their description
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.
Removed trailing periods.
README.md
Outdated
| `TestStubs` | `true`/`false` | `false` | Whether to generate test stub code | | ||
| `FileNaming` | `FullPath`/`PathToUnderscores`/`DropPath` | `FullPath` | How to handle the naming of generated sources | | ||
| `ExtraModuleImports` | `String` | `` | Extra module to import in generated code | |
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.
The description is Extra module to import in generated code
, but I assume multiple imports can be passed? If so, can we provide details on how that works?
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.
Updated the text a bit. Imports could look as follows:
--swiftgrpc_opt=ExtraModuleImports=foo,ExtraModuleImports=qux --swiftgrpc_opt=ExtraModuleImports=baz
@@ -88,7 +88,7 @@ class Generator { | |||
"SwiftGRPC", | |||
"SwiftProtobuf"] | |||
} | |||
for moduleName in moduleNames { | |||
for moduleName in moduleNames + options.extraModuleImports { |
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.
Might be nice to sort these
@@ -118,6 +131,9 @@ class Generator { | |||
if options.generateTestStubs && options.generateNIOImplementation { | |||
fatalError("Generating test stubs is not yet supported for SwiftGRPC-NIO.") | |||
} | |||
if options.generateTestStubs && !options.generateImplementations { | |||
fatalError("Generating server test stubs without an implementation is not yet supported.") |
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.
Do we plan to support this? If not, maybe remove "yet"
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.
Removed for now.
@@ -131,6 +140,13 @@ final class GeneratorOptions { | |||
throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value) | |||
} | |||
|
|||
case "ExtraModuleImports": | |||
if !pair.value.isEmpty { | |||
extraModuleImports.append(pair.value) |
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.
Do we need to split the value here to handle multiple import statements? I.e., pair.value.components(separatedBy: ",")
?
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.
No, per other comment multiple imports would be done by repeating the parameter.
--swiftgrpc_opt=ExtraModuleImports=foo,ExtraModuleImports=qux --swiftgrpc_opt=ExtraModuleImports=baz
The options are already comma separated in parseParameter(string:)
, and changing the semantics there could break existing callers.
Took another pass, thanks for the feedback! |
This allows generating test stubs in a separate file. Generating an implementation can be disabled with
--swiftgrpc_opt=Imlpementations=false
.A new
ExtraModuleImports
option allows adding import statement for the implementation. This enables generating test stubs in a separate module from implementation files. The name of the implementation module might very depending on the build system, so it can't be reliably inferred.One use case for this would be generating different bazel build targets: a
client
target that's usable in production, and astub
target that'stestonly = True
(and only usable from test targets).For example, a client could be generated in a
echo_swift_grpc_client
module follows:With a separate module for test stubs: