Skip to content

Instantly share code, notes, and snippets.

@x0a1b
Last active January 21, 2020 03:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save x0a1b/5e2c6e0cbeb99295a08e50f772da3672 to your computer and use it in GitHub Desktop.
Save x0a1b/5e2c6e0cbeb99295a08e50f772da3672 to your computer and use it in GitHub Desktop.
Greeter service snippets
val person = Person.newBuilder()
.setName("Hello Kitty")
.setAddress(
Address.newBuilder()
.setNumber(123)
.setStreet("Hello Kitty Street")
.setPostcode("N304SD")
.build())
.build()
syntax = "proto3";
option java_multiple_files = true;
option java_package = "io.grpc.examples.helloworld";
option java_outer_classname = "HelloWorldProto";
option objc_class_prefix = "HLW";
package helloworld;
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply);
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}
class GreeterServiceImpl : GreeterGrpc.GreeterImplBase() {
override fun sayHello(request: HelloRequest, responseObserver: StreamObserver<HelloReply>) {
if (request.name.contains("voldemort")) {
throw Status.INVALID_ARGUMENT
.withDescription("You are not welcome here ${request.name}")
.asRuntimeException()
}
// Notice the painful object builder
val reply = HelloReply.newBuilder().apply {
message = "Hello ${request.name}"
}.build()
responseObserver.onNext(reply)
responseObserver.onCompleted()
}
}
class BetterGreeterServiceImpl : Greeter {
override fun sayHello(request: HelloRequest): HelloReply {
if (request.name.contains("voldemort")) {
throw Status.INVALID_ARGUMENT
.withDescription("You are not welcome here ${request.name}")
.asRuntimeException()
}
return newHelloReply {
message = "Hello ${request.name}"
}
}
}
syntax = "proto3";
message Person {
string name = 1;
Address address = 2;
}
message Address {
int32 number = 1;
string street = 2;
string postCode = 3;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment