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 Kotlin builders for external types #373

Open
JavierSegoviaCordoba opened this issue Dec 24, 2022 · 10 comments
Open

Generate Kotlin builders for external types #373

JavierSegoviaCordoba opened this issue Dec 24, 2022 · 10 comments
Assignees

Comments

@JavierSegoviaCordoba
Copy link

I am using date.proto from Google types but I need to use Date.newBuilder().... Is it possible to generate a date { } builder?

@jamesward
Copy link
Collaborator

I believe that you can have dependencies on libraries that contain protos and the wrappers (Kotlin Protos for example) will be created around them. So I think this should work. Maybe I can build a little sample for this if I get the chance.

@jamesward jamesward self-assigned this Jan 4, 2023
@JavierSegoviaCordoba
Copy link
Author

Yeah, a sample should be great 😄 Thank you @jamesward!

@jamesward
Copy link
Collaborator

Here is the sample:
https://github.com/jamesward/grpc-kotlin-sample-deps

Date Kotlin Builders part:
https://github.com/jamesward/grpc-kotlin-sample-deps/blob/main/src/main/kotlin/io/grpc/examples/helloworld/HelloWorldServer.kt#L55-L60

The trick is to add a dep to the protobuf configuration, like:

protobuf("com.google.api.grpc:proto-google-common-protos:2.11.0")

That causes the protobuf Gradle plugin to treat it as a proto dependency and generate the sources for protos in the jar. Details:
https://github.com/google/protobuf-gradle-plugin#protos-in-dependencies

There is probably another way you could configure the Gradle plugin to only generate the Kotlin wrappers for that dependency (and not re-generate the Java wrappers that are already in the jar).

Hopefully that helps!

@JavierSegoviaCordoba
Copy link
Author

JavierSegoviaCordoba commented Jan 4, 2023

@jamesward I think I have the problem in that I have an intermediate protos module

// protos/build.gradle.kts
// it is using google protos
plugins {
    `java-library`
}

java {
    sourceSets.getByName("main").resources.srcDir("src/main/proto")
}

And in the server

// server/build.gradle.kts
dependencies {
    protobuf(project(projects.protos.dependencyProject.path))
}

protos module is going to be consumed from clients and servers, so it shouldn't generate anything. How would you setup this?

@jamesward
Copy link
Collaborator

@JavierSegoviaCordoba
Copy link
Author

Indeed I copied that setup, I will review it again to check if I am missing anything 🤔.

@jamesward
Copy link
Collaborator

jamesward commented Jan 4, 2023

The structure overview is something like:

  • protos module contains protos
  • stub module depends on protos & other library via protobuf(project(":protos")) and generates sources & compiles classes
  • server & client modules depend on stub classes via implementation(project(":stub"))

@jamesward
Copy link
Collaborator

The one part that might be hard to setup is a dependency in protos on the other library since that project doesn't have the protobuf configuration. Probably a way to make that work but a bit tricky.

@JavierSegoviaCordoba
Copy link
Author

@jamesward but stub generates client + server code? Or only the code related to proto messages?

@jamesward
Copy link
Collaborator

I have it setup so that stub generates the proto messages and the gRPC wrappers. But you could definitely split that out into two separate modules. I don't think there is a way to split out the client & server gRPC parts though (that I know of).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants