Last active
January 21, 2020 03:43
-
-
Save x0a1b/5e2c6e0cbeb99295a08e50f772da3672 to your computer and use it in GitHub Desktop.
Greeter service snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val person = Person.newBuilder() | |
.setName("Hello Kitty") | |
.setAddress( | |
Address.newBuilder() | |
.setNumber(123) | |
.setStreet("Hello Kitty Street") | |
.setPostcode("N304SD") | |
.build()) | |
.build() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}" | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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