Skip to content

Conversation

@stefanadranca
Copy link
Collaborator

@stefanadranca stefanadranca commented Jan 29, 2024

Motivation:

We want to have a protoc plugin for grpc-swift v2 which builds on top of the code generator pipeline.

Modifications:

  • Created the ProtobufCodeGenerator struct that encapsulates all setps needed to generate code for a given file descriptor
  • Created tests for the ProtobufCodeGenerator
  • added a new option for selecting v2 for the plugin
  • modified main() accordingly
  • adapted the render functions to respect the format we want for the generated code (blank lines between code blocks)

Result:

The protoc plugin for grpc-swift will support v2 and will use the CodeGen library.

Motivation:

We want to have a protoc plugin for grpc-swift v2 which builds on top of the code generator pipeline.

Modifications:

- Created the ProtobufCodeGenerator struct that encapsulates all setps needed to generate code for a given file descriptor
- Created tests for the ProtobufCodeGenerator
- added a new option for selecting v2 for the plugin
- modified main() accordingly

Result:

The protoc plugin for grpc-swift will support v2 and will use the CodeGen library.
@stefanadranca stefanadranca requested a review from glbrntt January 29, 2024 16:17
/// Whether or not server code should be generated.
public var server: Bool

public init(accessLevel: AccessLevel, indentation: Int = 4, client: Bool, server: Bool) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Defaulted parameters should be last (but before any trailing closures, if they exist)

import SwiftProtobufPluginLibrary

public struct ProtobufCodeGenerator {
internal var configs: SourceGenerator.Configuration
Copy link
Collaborator

Choose a reason for hiding this comment

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

There's only one configuration so the abbreviation would be 'config', however, we should just spell out configuration in full.

internal var configs: SourceGenerator.Configuration
internal var file: FileDescriptor

public init(configs: SourceGenerator.Configuration, file: FileDescriptor) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

It's a bit weird to store the file descriptor because it means the generator can only ever generate code for that descriptor. It makes more sense to have it as a parameter to generateCode

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, I followed the existing Generator model, but I will make it a parameter of the function

Copy link
Collaborator

Choose a reason for hiding this comment

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

Don't make the mistake of thinking the, or any for that matter, existing code is good 😉

Comment on lines 119 to 136
func convertOptionsToSourceGeneratortConfiguration(
_ options: GeneratorOptions
) -> SourceGenerator.Configuration {
let accessLevel: SourceGenerator.Configuration.AccessLevel
switch options.visibility {
case .internal:
accessLevel = .internal
case .package:
accessLevel = .package
case .public:
accessLevel = .public
}
return SourceGenerator.Configuration(
accessLevel: accessLevel,
client: options.generateClient,
server: options.generateServer
)
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

This seems more appropriate as an extension on SourceGenerator.Configuration:

extension SourceGenerator.Configuration {
  init(options: GeneratorOptions) {
    // ...
  }
}

throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
}

case "V2":
Copy link
Collaborator

Choose a reason for hiding this comment

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

Given we're not really supporting this yet, let's prefix it with an underscore: _V2

Comment on lines 110 to 112
/// Sends a greeting.
internal func sayHello<R>(
Copy link
Collaborator

Choose a reason for hiding this comment

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

There shouldn't be a blank line between these.

Comment on lines 182 to 184
/// The greeting service definition.
public protocol Helloworld_GreeterStreamingServiceProtocol: GRPCCore.RegistrableRPCService {
Copy link
Collaborator

Choose a reason for hiding this comment

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

There shouldn't be a blank line between these.

Comment on lines 185 to 187
/// Sends a greeting.
func sayHello(request: ServerRequest.Stream<Helloworld.Greeter.Methods.SayHello.Input>) async throws -> ServerResponse.Stream<Helloworld.Greeter.Methods.SayHello.Output>
Copy link
Collaborator

Choose a reason for hiding this comment

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

There shouldn't be a blank line between these.

Comment on lines 204 to 206
/// The greeting service definition.
public protocol Helloworld_GreeterServiceProtocol: Helloworld.Greeter.StreamingServiceProtocol {
Copy link
Collaborator

Choose a reason for hiding this comment

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

There shouldn't be a blank line between these.

Comment on lines 207 to 209
/// Sends a greeting.
func sayHello(request: ServerRequest.Single<Helloworld.Greeter.Methods.SayHello.Input>) async throws -> ServerResponse.Single<Helloworld.Greeter.Methods.SayHello.Output>
Copy link
Collaborator

Choose a reason for hiding this comment

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

There shouldn't be a blank line between these.

@stefanadranca stefanadranca marked this pull request as ready for review January 31, 2024 16:47
@stefanadranca stefanadranca requested a review from glbrntt January 31, 2024 16:47
@stefanadranca stefanadranca force-pushed the sd-protobuf-protoc-gen branch from ceef64e to 775637b Compare February 1, 2024 10:10
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.

LGTM!

@glbrntt glbrntt added the version/v2 Relates to v2 label Feb 1, 2024
@stefanadranca stefanadranca merged commit 53396af into grpc:main Feb 1, 2024
glbrntt pushed a commit to glbrntt/grpc-swift that referenced this pull request Feb 5, 2024
Motivation:

We want to have a protoc plugin for grpc-swift v2 which builds on top of the code generator pipeline.

Modifications:

- Created the ProtobufCodeGenerator struct that encapsulates all setps needed to generate code for a given file descriptor
- Created tests for the ProtobufCodeGenerator
- added a new option for selecting v2 for the plugin
- modified main() accordingly

Result:

The protoc plugin for grpc-swift will support v2 and will use the CodeGen library.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

version/v2 Relates to v2

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants