Wire 3: gRPC meets Kotlin | Cash App Code Blog
source link: https://cashapp.github.io/2019-10-07/wire3
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Wire 3: gRPC meets Kotlin
Wire is our open source implementation of Protocol Buffers. Today’s release supports Kotlin, gRPC, and Gradle.
Kotlin
In addition to Java, Wire now generates Kotlin:
class Dinosaur(
/** Common name of this dinosaur, like "Stegosaurus". */
val name: String? = null,
/** URLs with images of this dinosaur. */
val picture_urls: List<String> = emptyList(),
val period: Period? = null,
unknownFields: ByteString = ByteString.EMPTY
) : Message<Dinosaur, Nothing>(ADAPTER, unknownFields) {
// equals(), hashCode(), toString(), and copy()
}
Wire generates concise, idiomatic Kotlin, though we don’t generate data classes for technical reasons. Wire can also generate Java-interoperable Kotlin for an easy upgrade from Java.
gRPC is a widely-adopted spec for API calls over HTTP/2. The shared schemas make it safer and easier than REST for client/server communication. Streaming APIs introduce expressive new ways to communicate. Wire 3 generates ergonomic APIs for gRPC clients and includes a small runtime to support them.
val grpcClient = GrpcClient.Builder()
.client(okHttpClient)
.baseUrl(url)
.build()
val routeGuideService =
grpcClient.create(RouteGuideClient::class)
val (requestChannel, responseChannel) =
routeGuideService.RouteChat().execute()
requestChannel.send(RouteNote(message = "marco"))
responseChannel.receive()
requestChannel.send(RouteNote(message = "rené"))
requestChannel.close()
responseChannel.receive()
Gradle
Our new Gradle plugin makes it easy to start using Wire and protocol buffers:
apply plugin: 'com.squareup.wire'
wire {
// Keeps only 'Dinosaur#name' as the root of the object
// graph
roots 'squareup.dinosaurs.Dinosaur#name'
// Keeps all fields, except 'name', in 'Dinosaur'
prunes 'squareup.dinosaurs.Dinosaur#name'
kotlin {
javaInterop true
out "${buildDir}/generated/custom"
}
}
The plugin can generate both Java and Kotlin together. It can also prune your schema to keep your code small and your builds fast.
Get it
Today we are releasing Wire 3. Check out Wire on Github to get started.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK