Bump flatbuffers to v24.3.25 version (#6968)
This commit is contained in:
committed by
GitHub
parent
7436835244
commit
825132eec7
+14
-3
@@ -22,10 +22,21 @@ the GRPC libraries for this to compile. This test will build using the
|
||||
5. `cmake -DFLATBUFFERS_BUILD_GRPCTEST=ON -DGRPC_INSTALL_PATH=${GRPC_INSTALL_PATH} -DPROTOBUF_DOWNLOAD_PATH=${PROTOBUF_DOWNLOAD_PATH} ..`
|
||||
6. `make`
|
||||
|
||||
For Bazel users:
|
||||
|
||||
```shell
|
||||
$bazel test src/compiler/...
|
||||
```
|
||||
|
||||
## Running FlatBuffer gRPC tests
|
||||
|
||||
### Linux
|
||||
|
||||
1. `ln -s ${GRPC_INSTALL_PATH}/lib/libgrpc++_unsecure.so.6 ${GRPC_INSTALL_PATH}/lib/libgrpc++_unsecure.so.1`
|
||||
2. `export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${GRPC_INSTALL_PATH}/lib`
|
||||
3. `make test ARGS=-V`
|
||||
1. `export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${GRPC_INSTALL_PATH}/lib`
|
||||
2. `make test ARGS=-V`
|
||||
|
||||
For Bazel users:
|
||||
|
||||
```shell
|
||||
$bazel test tests/...
|
||||
```
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
diff --git a/CMakeLists.txt b/CMakeLists.txt
|
||||
index 1645a264a..12f8ca999 100644
|
||||
--- a/CMakeLists.txt
|
||||
+++ b/CMakeLists.txt
|
||||
@@ -635,6 +635,8 @@ add_library(
|
||||
src/ssl/tls_record.cc
|
||||
)
|
||||
|
||||
+target_link_libraries(ssl crypto)
|
||||
+
|
||||
add_executable(
|
||||
bssl
|
||||
|
||||
+10
-7
@@ -1,17 +1,20 @@
|
||||
#!/bin/bash
|
||||
|
||||
grpc_1_15_1_githash=1a60e6971f428323245a930031ad267bb3142ba4
|
||||
grpc_1_39_0_githash=58602e20a3f3e48f24a4114c757099b25b947f7b
|
||||
|
||||
function build_grpc () {
|
||||
git clone https://github.com/grpc/grpc.git google/grpc
|
||||
cd google/grpc
|
||||
git checkout ${grpc_1_15_1_githash}
|
||||
git checkout ${grpc_1_39_0_githash}
|
||||
git submodule update --init
|
||||
make
|
||||
make install prefix=`pwd`/install
|
||||
if [ ! -f ${GRPC_INSTALL_PATH}/lib/libgrpc++_unsecure.so.1 ]; then
|
||||
ln -s ${GRPC_INSTALL_PATH}/lib/libgrpc++_unsecure.so.6 ${GRPC_INSTALL_PATH}/lib/libgrpc++_unsecure.so.1
|
||||
fi
|
||||
# Apply boringssl build patch
|
||||
cd third_party/boringssl-with-bazel
|
||||
git apply ../../../../grpc/boringssl.patch
|
||||
cd ../..
|
||||
mkdir ../grpc_build
|
||||
cd ../grpc_build
|
||||
cmake ../grpc -DgRPC_INSTALL=ON -DgRPC_BUILD_TESTS=OFF -DABSL_ENABLE_INSTALL=ON -DBUILD_SHARED_LIBS=ON -DCMAKE_INSTALL_PREFIX=`pwd`/../grpc/install
|
||||
cmake --build . --target install ${JOBS:+-j$JOBS}
|
||||
cd ../..
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
diff --git a/bazel/copts.bzl b/bazel/copts.bzl
|
||||
index 10be944f25..879518b92f 100644
|
||||
--- a/bazel/copts.bzl
|
||||
+++ b/bazel/copts.bzl
|
||||
@@ -59,4 +59,4 @@ GRPC_LLVM_WARNING_FLAGS = [
|
||||
GRPC_DEFAULT_COPTS = select({
|
||||
"//:use_strict_warning": GRPC_LLVM_WARNING_FLAGS + ["-DUSE_STRICT_WARNING=1"],
|
||||
"//conditions:default": [],
|
||||
-})
|
||||
+}) + ["-std=c++14"]
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
## Languages known issues
|
||||
|
||||
### Python
|
||||
|
||||
- Assert the type required in your server/client since python is able to receive `Bytes array` or `utf8 strings`.
|
||||
|
||||
```python
|
||||
def SayHello(self, request, context):
|
||||
# request might be a byte array or a utf8 string
|
||||
|
||||
r = HelloRequest.HelloRequest().GetRootAs(request, 0)
|
||||
reply = "Unknown"
|
||||
if r.Name():
|
||||
reply = r.Name()
|
||||
# Issues might happen if type checking isnt present.
|
||||
# thus encoding it as a `reply.decode('UTF-8')`
|
||||
return build_reply("welcome " + reply.decode('UTF-8'))
|
||||
|
||||
```
|
||||
|
||||
This can be prevented by making sure all the requests coming to/from python are `Bytes array`
|
||||
|
||||
```python
|
||||
def say_hello(stub, builder):
|
||||
hello_request = bytes(builder.Output())
|
||||
reply = stub.SayHello(hello_request)
|
||||
r = HelloReply.HelloReply.GetRootAs(reply)
|
||||
print(r.Message())
|
||||
```
|
||||
|
||||
### Go
|
||||
|
||||
- Always requires the `content-type` of the payload to be set to `application/grpc+flatbuffers`
|
||||
|
||||
example: `.SayHello(ctx, b, grpc.CallContentSubtype("flatbuffers"))`
|
||||
@@ -0,0 +1,36 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# Copyright 2021 Google Inc. All rights reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
set -e
|
||||
|
||||
|
||||
format_greeter() {
|
||||
cd greeter
|
||||
|
||||
# Format client
|
||||
cd client
|
||||
gofmt -w .
|
||||
cd ..
|
||||
|
||||
# Format server
|
||||
cd server
|
||||
gofmt -w .
|
||||
cd ..
|
||||
|
||||
cd ..
|
||||
}
|
||||
|
||||
format_greeter
|
||||
@@ -0,0 +1,2 @@
|
||||
**/server
|
||||
**/client
|
||||
@@ -0,0 +1,25 @@
|
||||
# Go Greeter example
|
||||
|
||||
## Project Structure
|
||||
|
||||
.
|
||||
├── server # Server module
|
||||
├── client # Client module
|
||||
├── models # Flatbuffers models & main grpc code.
|
||||
└── README.md
|
||||
|
||||
## How to run Server:
|
||||
|
||||
- `cd server`
|
||||
|
||||
- `go clean`
|
||||
|
||||
- `go run main.go`
|
||||
|
||||
## How to run Client:
|
||||
|
||||
- `cd client`
|
||||
|
||||
- `go clean`
|
||||
|
||||
- `go run main.go --name NAME`
|
||||
@@ -0,0 +1,11 @@
|
||||
module github.com/google/flatbuffers/grpc/examples/go/greeter/client
|
||||
|
||||
go 1.15
|
||||
|
||||
replace github.com/google/flatbuffers/grpc/examples/go/greeter/models v0.0.0 => ../models
|
||||
|
||||
require (
|
||||
github.com/google/flatbuffers v2.0.8+incompatible
|
||||
github.com/google/flatbuffers/grpc/examples/go/greeter/models v0.0.0
|
||||
google.golang.org/grpc v1.56.3
|
||||
)
|
||||
@@ -0,0 +1,78 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
flatbuffers "github.com/google/flatbuffers/go"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/credentials/insecure"
|
||||
|
||||
models "github.com/google/flatbuffers/grpc/examples/go/greeter/models"
|
||||
)
|
||||
|
||||
var (
|
||||
addr = "3000"
|
||||
name = flag.String("name", "Flatbuffers", "name to be sent to server :D")
|
||||
)
|
||||
|
||||
func printSayHello(client models.GreeterClient, name string) {
|
||||
log.Printf("Name to be sent (%s)", name)
|
||||
b := flatbuffers.NewBuilder(0)
|
||||
i := b.CreateString(name)
|
||||
models.HelloRequestStart(b)
|
||||
models.HelloRequestAddName(b, i)
|
||||
b.Finish(models.HelloRequestEnd(b))
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
request, err := client.SayHello(ctx, b, grpc.CallContentSubtype("flatbuffers"))
|
||||
if err != nil {
|
||||
log.Fatalf("%v.SayHello(_) = _, %v: ", client, err)
|
||||
}
|
||||
log.Printf("server said %q", request.Message())
|
||||
}
|
||||
|
||||
func printSayManyHello(client models.GreeterClient, name string) {
|
||||
log.Printf("Name to be sent (%s)", name)
|
||||
b := flatbuffers.NewBuilder(0)
|
||||
i := b.CreateString(name)
|
||||
models.HelloRequestStart(b)
|
||||
models.HelloRequestAddName(b, i)
|
||||
b.Finish(models.HelloRequestEnd(b))
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
stream, err := client.SayManyHellos(ctx, b, grpc.CallContentSubtype("flatbuffers"))
|
||||
if err != nil {
|
||||
log.Fatalf("%v.SayManyHellos(_) = _, %v", client, err)
|
||||
}
|
||||
for {
|
||||
request, err := stream.Recv()
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
log.Fatalf("%v.SayManyHellos(_) = _, %v", client, err)
|
||||
}
|
||||
log.Printf("server said %q", request.Message())
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
conn, err := grpc.Dial(fmt.Sprintf("localhost:%d", 3000),
|
||||
grpc.WithTransportCredentials(insecure.NewCredentials()),
|
||||
grpc.WithDefaultCallOptions(grpc.ForceCodec(flatbuffers.FlatbuffersCodec{})))
|
||||
if err != nil {
|
||||
log.Fatalf("fail to dial: %v", err)
|
||||
}
|
||||
defer conn.Close()
|
||||
client := models.NewGreeterClient(conn)
|
||||
printSayHello(client, *name)
|
||||
printSayManyHello(client, *name)
|
||||
}
|
||||
@@ -0,0 +1,158 @@
|
||||
//Generated by gRPC Go plugin
|
||||
//If you make any local changes, they will be lost
|
||||
//source: greeter
|
||||
|
||||
package models
|
||||
|
||||
import (
|
||||
context "context"
|
||||
flatbuffers "github.com/google/flatbuffers/go"
|
||||
grpc "google.golang.org/grpc"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
// Client API for Greeter service
|
||||
type GreeterClient interface {
|
||||
SayHello(ctx context.Context, in *flatbuffers.Builder,
|
||||
opts ...grpc.CallOption) (*HelloReply, error)
|
||||
SayManyHellos(ctx context.Context, in *flatbuffers.Builder,
|
||||
opts ...grpc.CallOption) (Greeter_SayManyHellosClient, error)
|
||||
}
|
||||
|
||||
type greeterClient struct {
|
||||
cc grpc.ClientConnInterface
|
||||
}
|
||||
|
||||
func NewGreeterClient(cc grpc.ClientConnInterface) GreeterClient {
|
||||
return &greeterClient{cc}
|
||||
}
|
||||
|
||||
func (c *greeterClient) SayHello(ctx context.Context, in *flatbuffers.Builder,
|
||||
opts ...grpc.CallOption) (*HelloReply, error) {
|
||||
out := new(HelloReply)
|
||||
err := c.cc.Invoke(ctx, "/models.Greeter/SayHello", in, out, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *greeterClient) SayManyHellos(ctx context.Context, in *flatbuffers.Builder,
|
||||
opts ...grpc.CallOption) (Greeter_SayManyHellosClient, error) {
|
||||
stream, err := c.cc.NewStream(ctx, &_Greeter_serviceDesc.Streams[0], "/models.Greeter/SayManyHellos", opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
x := &greeterSayManyHellosClient{stream}
|
||||
if err := x.ClientStream.SendMsg(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := x.ClientStream.CloseSend(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return x, nil
|
||||
}
|
||||
|
||||
type Greeter_SayManyHellosClient interface {
|
||||
Recv() (*HelloReply, error)
|
||||
grpc.ClientStream
|
||||
}
|
||||
|
||||
type greeterSayManyHellosClient struct {
|
||||
grpc.ClientStream
|
||||
}
|
||||
|
||||
func (x *greeterSayManyHellosClient) Recv() (*HelloReply, error) {
|
||||
m := new(HelloReply)
|
||||
if err := x.ClientStream.RecvMsg(m); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// Server API for Greeter service
|
||||
type GreeterServer interface {
|
||||
SayHello(context.Context, *HelloRequest) (*flatbuffers.Builder, error)
|
||||
SayManyHellos(*HelloRequest, Greeter_SayManyHellosServer) error
|
||||
mustEmbedUnimplementedGreeterServer()
|
||||
}
|
||||
|
||||
type UnimplementedGreeterServer struct {
|
||||
}
|
||||
|
||||
func (UnimplementedGreeterServer) SayHello(context.Context, *HelloRequest) (*flatbuffers.Builder, error) {
|
||||
return nil, status.Errorf(codes.Unimplemented, "method SayHello not implemented")
|
||||
}
|
||||
|
||||
func (UnimplementedGreeterServer) SayManyHellos(*HelloRequest, Greeter_SayManyHellosServer) error {
|
||||
return status.Errorf(codes.Unimplemented, "method SayManyHellos not implemented")
|
||||
}
|
||||
|
||||
func (UnimplementedGreeterServer) mustEmbedUnimplementedGreeterServer() {}
|
||||
|
||||
type UnsafeGreeterServer interface {
|
||||
mustEmbedUnimplementedGreeterServer()
|
||||
}
|
||||
|
||||
func RegisterGreeterServer(s grpc.ServiceRegistrar, srv GreeterServer) {
|
||||
s.RegisterService(&_Greeter_serviceDesc, srv)
|
||||
}
|
||||
|
||||
func _Greeter_SayHello_Handler(srv interface{}, ctx context.Context,
|
||||
dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(HelloRequest)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(GreeterServer).SayHello(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: "/models.Greeter/SayHello",
|
||||
}
|
||||
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(GreeterServer).SayHello(ctx, req.(*HelloRequest))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
func _Greeter_SayManyHellos_Handler(srv interface{}, stream grpc.ServerStream) error {
|
||||
m := new(HelloRequest)
|
||||
if err := stream.RecvMsg(m); err != nil {
|
||||
return err
|
||||
}
|
||||
return srv.(GreeterServer).SayManyHellos(m, &greeterSayManyHellosServer{stream})
|
||||
}
|
||||
|
||||
type Greeter_SayManyHellosServer interface {
|
||||
Send(*flatbuffers.Builder) error
|
||||
grpc.ServerStream
|
||||
}
|
||||
|
||||
type greeterSayManyHellosServer struct {
|
||||
grpc.ServerStream
|
||||
}
|
||||
|
||||
func (x *greeterSayManyHellosServer) Send(m *flatbuffers.Builder) error {
|
||||
return x.ServerStream.SendMsg(m)
|
||||
}
|
||||
|
||||
var _Greeter_serviceDesc = grpc.ServiceDesc{
|
||||
ServiceName: "models.Greeter",
|
||||
HandlerType: (*GreeterServer)(nil),
|
||||
Methods: []grpc.MethodDesc{
|
||||
{
|
||||
MethodName: "SayHello",
|
||||
Handler: _Greeter_SayHello_Handler,
|
||||
},
|
||||
},
|
||||
Streams: []grpc.StreamDesc{
|
||||
{
|
||||
StreamName: "SayManyHellos",
|
||||
Handler: _Greeter_SayManyHellos_Handler,
|
||||
ServerStreams: true,
|
||||
},
|
||||
},
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
// Code generated by the FlatBuffers compiler. DO NOT EDIT.
|
||||
|
||||
package models
|
||||
|
||||
import (
|
||||
flatbuffers "github.com/google/flatbuffers/go"
|
||||
)
|
||||
|
||||
type HelloReply struct {
|
||||
_tab flatbuffers.Table
|
||||
}
|
||||
|
||||
func GetRootAsHelloReply(buf []byte, offset flatbuffers.UOffsetT) *HelloReply {
|
||||
n := flatbuffers.GetUOffsetT(buf[offset:])
|
||||
x := &HelloReply{}
|
||||
x.Init(buf, n+offset)
|
||||
return x
|
||||
}
|
||||
|
||||
func FinishHelloReplyBuffer(builder *flatbuffers.Builder, offset flatbuffers.UOffsetT) {
|
||||
builder.Finish(offset)
|
||||
}
|
||||
|
||||
func GetSizePrefixedRootAsHelloReply(buf []byte, offset flatbuffers.UOffsetT) *HelloReply {
|
||||
n := flatbuffers.GetUOffsetT(buf[offset+flatbuffers.SizeUint32:])
|
||||
x := &HelloReply{}
|
||||
x.Init(buf, n+offset+flatbuffers.SizeUint32)
|
||||
return x
|
||||
}
|
||||
|
||||
func FinishSizePrefixedHelloReplyBuffer(builder *flatbuffers.Builder, offset flatbuffers.UOffsetT) {
|
||||
builder.FinishSizePrefixed(offset)
|
||||
}
|
||||
|
||||
func (rcv *HelloReply) Init(buf []byte, i flatbuffers.UOffsetT) {
|
||||
rcv._tab.Bytes = buf
|
||||
rcv._tab.Pos = i
|
||||
}
|
||||
|
||||
func (rcv *HelloReply) Table() flatbuffers.Table {
|
||||
return rcv._tab
|
||||
}
|
||||
|
||||
func (rcv *HelloReply) Message() []byte {
|
||||
o := flatbuffers.UOffsetT(rcv._tab.Offset(4))
|
||||
if o != 0 {
|
||||
return rcv._tab.ByteVector(o + rcv._tab.Pos)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func HelloReplyStart(builder *flatbuffers.Builder) {
|
||||
builder.StartObject(1)
|
||||
}
|
||||
func HelloReplyAddMessage(builder *flatbuffers.Builder, message flatbuffers.UOffsetT) {
|
||||
builder.PrependUOffsetTSlot(0, flatbuffers.UOffsetT(message), 0)
|
||||
}
|
||||
func HelloReplyEnd(builder *flatbuffers.Builder) flatbuffers.UOffsetT {
|
||||
return builder.EndObject()
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
// Code generated by the FlatBuffers compiler. DO NOT EDIT.
|
||||
|
||||
package models
|
||||
|
||||
import (
|
||||
flatbuffers "github.com/google/flatbuffers/go"
|
||||
)
|
||||
|
||||
type HelloRequest struct {
|
||||
_tab flatbuffers.Table
|
||||
}
|
||||
|
||||
func GetRootAsHelloRequest(buf []byte, offset flatbuffers.UOffsetT) *HelloRequest {
|
||||
n := flatbuffers.GetUOffsetT(buf[offset:])
|
||||
x := &HelloRequest{}
|
||||
x.Init(buf, n+offset)
|
||||
return x
|
||||
}
|
||||
|
||||
func FinishHelloRequestBuffer(builder *flatbuffers.Builder, offset flatbuffers.UOffsetT) {
|
||||
builder.Finish(offset)
|
||||
}
|
||||
|
||||
func GetSizePrefixedRootAsHelloRequest(buf []byte, offset flatbuffers.UOffsetT) *HelloRequest {
|
||||
n := flatbuffers.GetUOffsetT(buf[offset+flatbuffers.SizeUint32:])
|
||||
x := &HelloRequest{}
|
||||
x.Init(buf, n+offset+flatbuffers.SizeUint32)
|
||||
return x
|
||||
}
|
||||
|
||||
func FinishSizePrefixedHelloRequestBuffer(builder *flatbuffers.Builder, offset flatbuffers.UOffsetT) {
|
||||
builder.FinishSizePrefixed(offset)
|
||||
}
|
||||
|
||||
func (rcv *HelloRequest) Init(buf []byte, i flatbuffers.UOffsetT) {
|
||||
rcv._tab.Bytes = buf
|
||||
rcv._tab.Pos = i
|
||||
}
|
||||
|
||||
func (rcv *HelloRequest) Table() flatbuffers.Table {
|
||||
return rcv._tab
|
||||
}
|
||||
|
||||
func (rcv *HelloRequest) Name() []byte {
|
||||
o := flatbuffers.UOffsetT(rcv._tab.Offset(4))
|
||||
if o != 0 {
|
||||
return rcv._tab.ByteVector(o + rcv._tab.Pos)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func HelloRequestStart(builder *flatbuffers.Builder) {
|
||||
builder.StartObject(1)
|
||||
}
|
||||
func HelloRequestAddName(builder *flatbuffers.Builder, name flatbuffers.UOffsetT) {
|
||||
builder.PrependUOffsetTSlot(0, flatbuffers.UOffsetT(name), 0)
|
||||
}
|
||||
func HelloRequestEnd(builder *flatbuffers.Builder) flatbuffers.UOffsetT {
|
||||
return builder.EndObject()
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
module github.com/google/flatbuffers/grpc/examples/go/greeter/models
|
||||
|
||||
go 1.15
|
||||
|
||||
require (
|
||||
github.com/google/flatbuffers v2.0.8+incompatible
|
||||
google.golang.org/grpc v1.56.3
|
||||
)
|
||||
@@ -0,0 +1,11 @@
|
||||
module github.com/google/flatbuffers/grpc/examples/go/greeter/server
|
||||
|
||||
go 1.15
|
||||
|
||||
replace github.com/google/flatbuffers/grpc/examples/go/greeter/models v0.0.0 => ../models
|
||||
|
||||
require (
|
||||
github.com/google/flatbuffers v2.0.8+incompatible
|
||||
github.com/google/flatbuffers/grpc/examples/go/greeter/models v0.0.0
|
||||
google.golang.org/grpc v1.56.3
|
||||
)
|
||||
@@ -0,0 +1,77 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
|
||||
flatbuffers "github.com/google/flatbuffers/go"
|
||||
models "github.com/google/flatbuffers/grpc/examples/go/greeter/models"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
var (
|
||||
greetings = [...]string{"Hi", "Hallo", "Ciao"}
|
||||
)
|
||||
|
||||
type greeterServer struct {
|
||||
models.UnimplementedGreeterServer
|
||||
}
|
||||
|
||||
func (s *greeterServer) SayHello(ctx context.Context, request *models.HelloRequest) (*flatbuffers.Builder, error) {
|
||||
v := request.Name()
|
||||
var m string
|
||||
if v == nil {
|
||||
m = "Unknown"
|
||||
} else {
|
||||
m = string(v)
|
||||
}
|
||||
b := flatbuffers.NewBuilder(0)
|
||||
idx := b.CreateString("welcome " + m)
|
||||
models.HelloReplyStart(b)
|
||||
models.HelloReplyAddMessage(b, idx)
|
||||
b.Finish(models.HelloReplyEnd(b))
|
||||
return b, nil
|
||||
}
|
||||
|
||||
func (s *greeterServer) SayManyHellos(request *models.HelloRequest, stream models.Greeter_SayManyHellosServer) error {
|
||||
v := request.Name()
|
||||
var m string
|
||||
if v == nil {
|
||||
m = "Unknown"
|
||||
} else {
|
||||
m = string(v)
|
||||
}
|
||||
b := flatbuffers.NewBuilder(0)
|
||||
|
||||
for _, greeting := range greetings {
|
||||
idx := b.CreateString(greeting + " " + m)
|
||||
models.HelloReplyStart(b)
|
||||
models.HelloReplyAddMessage(b, idx)
|
||||
b.Finish(models.HelloReplyEnd(b))
|
||||
if err := stream.Send(b); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func newServer() *greeterServer {
|
||||
s := &greeterServer{}
|
||||
return s
|
||||
}
|
||||
|
||||
func main() {
|
||||
lis, err := net.Listen("tcp", fmt.Sprintf("localhost:%d", 3000))
|
||||
if err != nil {
|
||||
log.Fatalf("failed to listen: %v", err)
|
||||
}
|
||||
codec := &flatbuffers.FlatbuffersCodec{}
|
||||
grpcServer := grpc.NewServer(grpc.ForceServerCodec(codec))
|
||||
models.RegisterGreeterServer(grpcServer, newServer())
|
||||
if err := grpcServer.Serve(lis); err != nil {
|
||||
fmt.Print(err)
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
namespace models;
|
||||
|
||||
table HelloReply {
|
||||
message:string;
|
||||
}
|
||||
|
||||
table HelloRequest {
|
||||
name:string;
|
||||
}
|
||||
|
||||
rpc_service Greeter {
|
||||
SayHello(HelloRequest):HelloReply;
|
||||
SayManyHellos(HelloRequest):HelloReply (streaming: "server");
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
# Python Greeter example
|
||||
|
||||
## Prerequisite
|
||||
|
||||
- You need to have grpc python installed on your device `pip install grpcio`
|
||||
## How to run Server:
|
||||
|
||||
- `python server.py ${PORT}`
|
||||
|
||||
## How to run Client:
|
||||
|
||||
- `python client.py ${PORT} ${NAME}`
|
||||
@@ -0,0 +1,40 @@
|
||||
import sys
|
||||
import argparse
|
||||
import grpc
|
||||
|
||||
sys.path.insert(0, '../../../../../flatbuffers/python')
|
||||
|
||||
import flatbuffers
|
||||
from models import HelloReply, HelloRequest, greeter_grpc_fb
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("port", help="server port to connect to", default=3000)
|
||||
parser.add_argument("name", help="name to be sent to server", default="flatbuffers")
|
||||
|
||||
def say_hello(stub, hello_request):
|
||||
reply = stub.SayHello(hello_request)
|
||||
r = HelloReply.HelloReply.GetRootAs(reply)
|
||||
print(r.Message())
|
||||
|
||||
def say_many_hellos(stub, hello_request):
|
||||
greetings = stub.SayManyHellos(hello_request)
|
||||
for greeting in greetings:
|
||||
r = HelloReply.HelloReply.GetRootAs(greeting)
|
||||
print(r.Message())
|
||||
|
||||
def main():
|
||||
args = parser.parse_args()
|
||||
|
||||
with grpc.insecure_channel('localhost:' + args.port) as channel:
|
||||
builder = flatbuffers.Builder()
|
||||
ind = builder.CreateString(args.name)
|
||||
HelloRequest.HelloRequestStart(builder)
|
||||
HelloRequest.HelloRequestAddName(builder, ind)
|
||||
root = HelloRequest.HelloRequestEnd(builder)
|
||||
builder.Finish(root)
|
||||
output = bytes(builder.Output())
|
||||
stub = greeter_grpc_fb.GreeterStub(channel)
|
||||
say_hello(stub, output)
|
||||
say_many_hellos(stub, output)
|
||||
|
||||
main()
|
||||
@@ -0,0 +1,50 @@
|
||||
# automatically generated by the FlatBuffers compiler, do not modify
|
||||
|
||||
# namespace: models
|
||||
|
||||
import flatbuffers
|
||||
from flatbuffers.compat import import_numpy
|
||||
np = import_numpy()
|
||||
|
||||
class HelloReply(object):
|
||||
__slots__ = ['_tab']
|
||||
|
||||
@classmethod
|
||||
def GetRootAs(cls, buf, offset=0):
|
||||
n = flatbuffers.encode.Get(flatbuffers.packer.uoffset, buf, offset)
|
||||
x = HelloReply()
|
||||
x.Init(buf, n + offset)
|
||||
return x
|
||||
|
||||
@classmethod
|
||||
def GetRootAsHelloReply(cls, buf, offset=0):
|
||||
"""This method is deprecated. Please switch to GetRootAs."""
|
||||
return cls.GetRootAs(buf, offset)
|
||||
# HelloReply
|
||||
def Init(self, buf, pos):
|
||||
self._tab = flatbuffers.table.Table(buf, pos)
|
||||
|
||||
# HelloReply
|
||||
def Message(self):
|
||||
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(4))
|
||||
if o != 0:
|
||||
return self._tab.String(o + self._tab.Pos)
|
||||
return None
|
||||
|
||||
def HelloReplyStart(builder):
|
||||
builder.StartObject(1)
|
||||
|
||||
def Start(builder):
|
||||
HelloReplyStart(builder)
|
||||
|
||||
def HelloReplyAddMessage(builder, message):
|
||||
builder.PrependUOffsetTRelativeSlot(0, flatbuffers.number_types.UOffsetTFlags.py_type(message), 0)
|
||||
|
||||
def AddMessage(builder, message):
|
||||
HelloReplyAddMessage(builder, message)
|
||||
|
||||
def HelloReplyEnd(builder):
|
||||
return builder.EndObject()
|
||||
|
||||
def End(builder):
|
||||
return HelloReplyEnd(builder)
|
||||
@@ -0,0 +1,50 @@
|
||||
# automatically generated by the FlatBuffers compiler, do not modify
|
||||
|
||||
# namespace: models
|
||||
|
||||
import flatbuffers
|
||||
from flatbuffers.compat import import_numpy
|
||||
np = import_numpy()
|
||||
|
||||
class HelloRequest(object):
|
||||
__slots__ = ['_tab']
|
||||
|
||||
@classmethod
|
||||
def GetRootAs(cls, buf, offset=0):
|
||||
n = flatbuffers.encode.Get(flatbuffers.packer.uoffset, buf, offset)
|
||||
x = HelloRequest()
|
||||
x.Init(buf, n + offset)
|
||||
return x
|
||||
|
||||
@classmethod
|
||||
def GetRootAsHelloRequest(cls, buf, offset=0):
|
||||
"""This method is deprecated. Please switch to GetRootAs."""
|
||||
return cls.GetRootAs(buf, offset)
|
||||
# HelloRequest
|
||||
def Init(self, buf, pos):
|
||||
self._tab = flatbuffers.table.Table(buf, pos)
|
||||
|
||||
# HelloRequest
|
||||
def Name(self):
|
||||
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(4))
|
||||
if o != 0:
|
||||
return self._tab.String(o + self._tab.Pos)
|
||||
return None
|
||||
|
||||
def HelloRequestStart(builder):
|
||||
builder.StartObject(1)
|
||||
|
||||
def Start(builder):
|
||||
HelloRequestStart(builder)
|
||||
|
||||
def HelloRequestAddName(builder, name):
|
||||
builder.PrependUOffsetTRelativeSlot(0, flatbuffers.number_types.UOffsetTFlags.py_type(name), 0)
|
||||
|
||||
def AddName(builder, name):
|
||||
HelloRequestAddName(builder, name)
|
||||
|
||||
def HelloRequestEnd(builder):
|
||||
return builder.EndObject()
|
||||
|
||||
def End(builder):
|
||||
return HelloRequestEnd(builder)
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
# Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT!
|
||||
|
||||
import grpc
|
||||
|
||||
class GreeterStub(object):
|
||||
""" Interface exported by the server. """
|
||||
|
||||
def __init__(self, channel):
|
||||
""" Constructor.
|
||||
|
||||
Args:
|
||||
channel: A grpc.Channel.
|
||||
"""
|
||||
|
||||
self.SayHello = channel.unary_unary(
|
||||
"/models.Greeter/SayHello"
|
||||
)
|
||||
|
||||
self.SayManyHellos = channel.unary_stream(
|
||||
"/models.Greeter/SayManyHellos"
|
||||
)
|
||||
|
||||
|
||||
class GreeterServicer(object):
|
||||
""" Interface exported by the server. """
|
||||
|
||||
def SayHello(self, request, context):
|
||||
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
|
||||
context.set_details('Method not implemented!')
|
||||
raise NotImplementedError('Method not implemented!')
|
||||
|
||||
|
||||
def SayManyHellos(self, request, context):
|
||||
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
|
||||
context.set_details('Method not implemented!')
|
||||
raise NotImplementedError('Method not implemented!')
|
||||
|
||||
|
||||
|
||||
def add_GreeterServicer_to_server(servicer, server):
|
||||
rpc_method_handlers = {
|
||||
'SayHello': grpc.unary_unary_rpc_method_handler(
|
||||
servicer.SayHello
|
||||
),
|
||||
'SayManyHellos': grpc.unary_stream_rpc_method_handler(
|
||||
servicer.SayManyHellos
|
||||
),
|
||||
}
|
||||
generic_handler = grpc.method_handlers_generic_handler(
|
||||
'models.Greeter', rpc_method_handlers)
|
||||
server.add_generic_rpc_handlers((generic_handler,))
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
from concurrent import futures
|
||||
import sys
|
||||
import argparse
|
||||
import grpc
|
||||
|
||||
sys.path.insert(0, '../../../../../flatbuffers/python')
|
||||
|
||||
import flatbuffers
|
||||
from models import HelloReply, HelloRequest, greeter_grpc_fb
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("port", help="server on port", default=3000)
|
||||
|
||||
def build_reply(message):
|
||||
builder = flatbuffers.Builder()
|
||||
ind = builder.CreateString(message)
|
||||
HelloReply.HelloReplyStart(builder)
|
||||
HelloReply.HelloReplyAddMessage(builder, ind)
|
||||
root = HelloReply.HelloReplyEnd(builder)
|
||||
builder.Finish(root)
|
||||
return bytes(builder.Output())
|
||||
|
||||
class GreeterServicer(greeter_grpc_fb.GreeterServicer):
|
||||
|
||||
def __init__(self):
|
||||
self.greetings = ["Hi", "Hallo", "Ciao"]
|
||||
|
||||
def SayHello(self, request, context):
|
||||
r = HelloRequest.HelloRequest().GetRootAs(request, 0)
|
||||
reply = "Unknown"
|
||||
if r.Name():
|
||||
reply = r.Name()
|
||||
return build_reply("welcome " + reply.decode('UTF-8'))
|
||||
|
||||
def SayManyHellos(self, request, context):
|
||||
r = HelloRequest.HelloRequest().GetRootAs(request, 0)
|
||||
reply = "Unknown"
|
||||
if r.Name():
|
||||
reply = r.Name()
|
||||
|
||||
for greeting in self.greetings:
|
||||
print(type(reply))
|
||||
yield build_reply(greeting + " " + reply.decode('UTF-8'))
|
||||
|
||||
|
||||
def serve():
|
||||
args = parser.parse_args()
|
||||
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
|
||||
greeter_grpc_fb.add_GreeterServicer_to_server(
|
||||
GreeterServicer(), server
|
||||
)
|
||||
server.add_insecure_port('[::]:' + args.port)
|
||||
server.start()
|
||||
server.wait_for_termination()
|
||||
|
||||
if __name__ == '__main__':
|
||||
serve()
|
||||
@@ -0,0 +1,58 @@
|
||||
// swift-tools-version:5.1
|
||||
/*
|
||||
* Copyright 2020 Google Inc. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import PackageDescription
|
||||
|
||||
let package = Package(
|
||||
name: "Greeter",
|
||||
platforms: [
|
||||
.iOS(.v11),
|
||||
.macOS(.v10_14),
|
||||
],
|
||||
dependencies: [
|
||||
.package(path: "../../../../swift"),
|
||||
.package(url: "https://github.com/grpc/grpc-swift.git", from: "1.0.0"),
|
||||
],
|
||||
targets: [
|
||||
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
|
||||
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
|
||||
.target(
|
||||
name: "Model",
|
||||
dependencies: [
|
||||
"GRPC",
|
||||
"FlatBuffers",
|
||||
],
|
||||
path: "Sources/Model"),
|
||||
|
||||
// Client for the Greeter example
|
||||
.target(
|
||||
name: "Client",
|
||||
dependencies: [
|
||||
"GRPC",
|
||||
"Model",
|
||||
],
|
||||
path: "Sources/client"),
|
||||
|
||||
// Server for the Greeter example
|
||||
.target(
|
||||
name: "Server",
|
||||
dependencies: [
|
||||
"GRPC",
|
||||
"Model",
|
||||
],
|
||||
path: "Sources/server"),
|
||||
])
|
||||
@@ -0,0 +1,7 @@
|
||||
# FlatBuffers.GRPC.Swift
|
||||
|
||||
The following is Swift example on how GRPC would be with Swift Flatbuffers, you can simply run the following commands:
|
||||
|
||||
`swift run Server`
|
||||
|
||||
`swift run Client {port} {name}`
|
||||
+145
@@ -0,0 +1,145 @@
|
||||
// Generated GRPC code for FlatBuffers swift!
|
||||
/// The following code is generated by the Flatbuffers library which might not be in sync with grpc-swift
|
||||
/// in case of an issue please open github issue, though it would be maintained
|
||||
|
||||
// swiftlint:disable all
|
||||
// swiftformat:disable all
|
||||
|
||||
import Foundation
|
||||
import GRPC
|
||||
import NIO
|
||||
import NIOHTTP1
|
||||
import FlatBuffers
|
||||
|
||||
public protocol GRPCFlatBufPayload: GRPCPayload, FlatBufferGRPCMessage {}
|
||||
public extension GRPCFlatBufPayload {
|
||||
init(serializedByteBuffer: inout NIO.ByteBuffer) throws {
|
||||
self.init(byteBuffer: FlatBuffers.ByteBuffer(contiguousBytes: serializedByteBuffer.readableBytesView, count: serializedByteBuffer.readableBytes))
|
||||
}
|
||||
func serialize(into buffer: inout NIO.ByteBuffer) throws {
|
||||
let buf = UnsafeRawBufferPointer(start: self.rawPointer, count: Int(self.size))
|
||||
buffer.writeBytes(buf)
|
||||
}
|
||||
}
|
||||
extension Message: GRPCFlatBufPayload {}
|
||||
|
||||
/// Usage: instantiate models_GreeterServiceClient, then call methods of this protocol to make API calls.
|
||||
public protocol models_GreeterClientProtocol: GRPCClient {
|
||||
|
||||
var serviceName: String { get }
|
||||
|
||||
var interceptors: models_GreeterClientInterceptorFactoryProtocol? { get }
|
||||
|
||||
func SayHello(
|
||||
_ request: Message<models_HelloRequest>
|
||||
, callOptions: CallOptions?
|
||||
) -> UnaryCall<Message<models_HelloRequest>, Message<models_HelloReply>>
|
||||
|
||||
func SayManyHellos(
|
||||
_ request: Message<models_HelloRequest>
|
||||
, callOptions: CallOptions?,
|
||||
handler: @escaping (Message<models_HelloReply>) -> Void
|
||||
) -> ServerStreamingCall<Message<models_HelloRequest>, Message<models_HelloReply>>
|
||||
|
||||
}
|
||||
|
||||
extension models_GreeterClientProtocol {
|
||||
|
||||
public var serviceName: String { "models.Greeter" }
|
||||
|
||||
public func SayHello(
|
||||
_ request: Message<models_HelloRequest>
|
||||
, callOptions: CallOptions? = nil
|
||||
) -> UnaryCall<Message<models_HelloRequest>, Message<models_HelloReply>> {
|
||||
return self.makeUnaryCall(
|
||||
path: "/models.Greeter/SayHello",
|
||||
request: request,
|
||||
callOptions: callOptions ?? self.defaultCallOptions,
|
||||
interceptors: self.interceptors?.makeSayHelloInterceptors() ?? []
|
||||
)
|
||||
}
|
||||
|
||||
public func SayManyHellos(
|
||||
_ request: Message<models_HelloRequest>
|
||||
, callOptions: CallOptions? = nil,
|
||||
handler: @escaping (Message<models_HelloReply>) -> Void
|
||||
) -> ServerStreamingCall<Message<models_HelloRequest>, Message<models_HelloReply>> {
|
||||
return self.makeServerStreamingCall(
|
||||
path: "/models.Greeter/SayManyHellos",
|
||||
request: request,
|
||||
callOptions: callOptions ?? self.defaultCallOptions,
|
||||
interceptors: self.interceptors?.makeSayManyHellosInterceptors() ?? [],
|
||||
handler: handler
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
public protocol models_GreeterClientInterceptorFactoryProtocol {
|
||||
/// - Returns: Interceptors to use when invoking 'SayHello'.
|
||||
func makeSayHelloInterceptors() -> [ClientInterceptor<Message<models_HelloRequest>, Message<models_HelloReply>>]
|
||||
|
||||
/// - Returns: Interceptors to use when invoking 'SayManyHellos'.
|
||||
func makeSayManyHellosInterceptors() -> [ClientInterceptor<Message<models_HelloRequest>, Message<models_HelloReply>>]
|
||||
|
||||
}
|
||||
|
||||
public final class models_GreeterServiceClient: models_GreeterClientProtocol {
|
||||
public let channel: GRPCChannel
|
||||
public var defaultCallOptions: CallOptions
|
||||
public var interceptors: models_GreeterClientInterceptorFactoryProtocol?
|
||||
|
||||
public init(
|
||||
channel: GRPCChannel,
|
||||
defaultCallOptions: CallOptions = CallOptions(),
|
||||
interceptors: models_GreeterClientInterceptorFactoryProtocol? = nil
|
||||
) {
|
||||
self.channel = channel
|
||||
self.defaultCallOptions = defaultCallOptions
|
||||
self.interceptors = interceptors
|
||||
}
|
||||
}
|
||||
|
||||
public protocol models_GreeterProvider: CallHandlerProvider {
|
||||
var interceptors: models_GreeterServerInterceptorFactoryProtocol? { get }
|
||||
func SayHello(request: Message<models_HelloRequest>, context: StatusOnlyCallContext) -> EventLoopFuture<Message<models_HelloReply>>
|
||||
func SayManyHellos(request: Message<models_HelloRequest>, context: StreamingResponseCallContext<Message<models_HelloReply>>) -> EventLoopFuture<GRPCStatus>
|
||||
}
|
||||
|
||||
public extension models_GreeterProvider {
|
||||
|
||||
var serviceName: Substring { return "models.Greeter" }
|
||||
|
||||
func handle(method name: Substring, context: CallHandlerContext) -> GRPCServerHandlerProtocol? {
|
||||
switch name {
|
||||
case "SayHello":
|
||||
return UnaryServerHandler(
|
||||
context: context,
|
||||
requestDeserializer: GRPCPayloadDeserializer<Message<models_HelloRequest>>(),
|
||||
responseSerializer: GRPCPayloadSerializer<Message<models_HelloReply>>(),
|
||||
interceptors: self.interceptors?.makeSayHelloInterceptors() ?? [],
|
||||
userFunction: self.SayHello(request:context:))
|
||||
|
||||
case "SayManyHellos":
|
||||
return ServerStreamingServerHandler(
|
||||
context: context,
|
||||
requestDeserializer: GRPCPayloadDeserializer<Message<models_HelloRequest>>(),
|
||||
responseSerializer: GRPCPayloadSerializer<Message<models_HelloReply>>(),
|
||||
interceptors: self.interceptors?.makeSayManyHellosInterceptors() ?? [],
|
||||
userFunction: self.SayManyHellos(request:context:))
|
||||
|
||||
default: return nil;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public protocol models_GreeterServerInterceptorFactoryProtocol {
|
||||
/// - Returns: Interceptors to use when handling 'SayHello'.
|
||||
/// Defaults to calling `self.makeInterceptors()`.
|
||||
func makeSayHelloInterceptors() -> [ServerInterceptor<Message<models_HelloRequest>, Message<models_HelloReply>>]
|
||||
|
||||
/// - Returns: Interceptors to use when handling 'SayManyHellos'.
|
||||
/// Defaults to calling `self.makeInterceptors()`.
|
||||
func makeSayManyHellosInterceptors() -> [ServerInterceptor<Message<models_HelloRequest>, Message<models_HelloReply>>]
|
||||
|
||||
}
|
||||
+100
@@ -0,0 +1,100 @@
|
||||
// automatically generated by the FlatBuffers compiler, do not modify
|
||||
// swiftlint:disable all
|
||||
// swiftformat:disable all
|
||||
|
||||
import FlatBuffers
|
||||
|
||||
public struct models_HelloReply: FlatBufferObject, Verifiable {
|
||||
|
||||
static func validateVersion() { FlatBuffersVersion_24_3_25() }
|
||||
public var __buffer: ByteBuffer! { return _accessor.bb }
|
||||
private var _accessor: Table
|
||||
|
||||
private init(_ t: Table) { _accessor = t }
|
||||
public init(_ bb: ByteBuffer, o: Int32) { _accessor = Table(bb: bb, position: o) }
|
||||
|
||||
private enum VTOFFSET: VOffset {
|
||||
case message = 4
|
||||
var v: Int32 { Int32(self.rawValue) }
|
||||
var p: VOffset { self.rawValue }
|
||||
}
|
||||
|
||||
public var message: String? { let o = _accessor.offset(VTOFFSET.message.v); return o == 0 ? nil : _accessor.string(at: o) }
|
||||
public var messageSegmentArray: [UInt8]? { return _accessor.getVector(at: VTOFFSET.message.v) }
|
||||
public static func startHelloReply(_ fbb: inout FlatBufferBuilder) -> UOffset { fbb.startTable(with: 1) }
|
||||
public static func add(message: Offset, _ fbb: inout FlatBufferBuilder) { fbb.add(offset: message, at: VTOFFSET.message.p) }
|
||||
public static func endHelloReply(_ fbb: inout FlatBufferBuilder, start: UOffset) -> Offset { let end = Offset(offset: fbb.endTable(at: start)); return end }
|
||||
public static func createHelloReply(
|
||||
_ fbb: inout FlatBufferBuilder,
|
||||
messageOffset message: Offset = Offset()
|
||||
) -> Offset {
|
||||
let __start = models_HelloReply.startHelloReply(&fbb)
|
||||
models_HelloReply.add(message: message, &fbb)
|
||||
return models_HelloReply.endHelloReply(&fbb, start: __start)
|
||||
}
|
||||
|
||||
public static func verify<T>(_ verifier: inout Verifier, at position: Int, of type: T.Type) throws where T: Verifiable {
|
||||
var _v = try verifier.visitTable(at: position)
|
||||
try _v.visit(field: VTOFFSET.message.p, fieldName: "message", required: false, type: ForwardOffset<String>.self)
|
||||
_v.finish()
|
||||
}
|
||||
}
|
||||
|
||||
extension models_HelloReply: Encodable {
|
||||
|
||||
enum CodingKeys: String, CodingKey {
|
||||
case message = "message"
|
||||
}
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.container(keyedBy: CodingKeys.self)
|
||||
try container.encodeIfPresent(message, forKey: .message)
|
||||
}
|
||||
}
|
||||
|
||||
public struct models_HelloRequest: FlatBufferObject, Verifiable {
|
||||
|
||||
static func validateVersion() { FlatBuffersVersion_24_3_25() }
|
||||
public var __buffer: ByteBuffer! { return _accessor.bb }
|
||||
private var _accessor: Table
|
||||
|
||||
private init(_ t: Table) { _accessor = t }
|
||||
public init(_ bb: ByteBuffer, o: Int32) { _accessor = Table(bb: bb, position: o) }
|
||||
|
||||
private enum VTOFFSET: VOffset {
|
||||
case name = 4
|
||||
var v: Int32 { Int32(self.rawValue) }
|
||||
var p: VOffset { self.rawValue }
|
||||
}
|
||||
|
||||
public var name: String? { let o = _accessor.offset(VTOFFSET.name.v); return o == 0 ? nil : _accessor.string(at: o) }
|
||||
public var nameSegmentArray: [UInt8]? { return _accessor.getVector(at: VTOFFSET.name.v) }
|
||||
public static func startHelloRequest(_ fbb: inout FlatBufferBuilder) -> UOffset { fbb.startTable(with: 1) }
|
||||
public static func add(name: Offset, _ fbb: inout FlatBufferBuilder) { fbb.add(offset: name, at: VTOFFSET.name.p) }
|
||||
public static func endHelloRequest(_ fbb: inout FlatBufferBuilder, start: UOffset) -> Offset { let end = Offset(offset: fbb.endTable(at: start)); return end }
|
||||
public static func createHelloRequest(
|
||||
_ fbb: inout FlatBufferBuilder,
|
||||
nameOffset name: Offset = Offset()
|
||||
) -> Offset {
|
||||
let __start = models_HelloRequest.startHelloRequest(&fbb)
|
||||
models_HelloRequest.add(name: name, &fbb)
|
||||
return models_HelloRequest.endHelloRequest(&fbb, start: __start)
|
||||
}
|
||||
|
||||
public static func verify<T>(_ verifier: inout Verifier, at position: Int, of type: T.Type) throws where T: Verifiable {
|
||||
var _v = try verifier.visitTable(at: position)
|
||||
try _v.visit(field: VTOFFSET.name.p, fieldName: "name", required: false, type: ForwardOffset<String>.self)
|
||||
_v.finish()
|
||||
}
|
||||
}
|
||||
|
||||
extension models_HelloRequest: Encodable {
|
||||
|
||||
enum CodingKeys: String, CodingKey {
|
||||
case name = "name"
|
||||
}
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.container(keyedBy: CodingKeys.self)
|
||||
try container.encodeIfPresent(name, forKey: .name)
|
||||
}
|
||||
}
|
||||
|
||||
+107
@@ -0,0 +1,107 @@
|
||||
/*
|
||||
* Copyright 2023 Google Inc. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import FlatBuffers
|
||||
import GRPC
|
||||
import Logging
|
||||
import Model
|
||||
import NIO
|
||||
|
||||
// Quieten the logs.
|
||||
LoggingSystem.bootstrap {
|
||||
var handler = StreamLogHandler.standardOutput(label: $0)
|
||||
handler.logLevel = .critical
|
||||
return handler
|
||||
}
|
||||
|
||||
func greet(name: String, client greeter: models_GreeterServiceClient) {
|
||||
// Form the request with the name, if one was provided.
|
||||
var builder = FlatBufferBuilder()
|
||||
let nameOff = builder.create(string: name)
|
||||
let root = models_HelloRequest.createHelloRequest(
|
||||
&builder,
|
||||
nameOffset: nameOff)
|
||||
builder.finish(offset: root)
|
||||
|
||||
// Make the RPC call to the server.
|
||||
let sayHello = greeter
|
||||
.SayHello(Message<models_HelloRequest>(builder: &builder))
|
||||
|
||||
// wait() on the response to stop the program from exiting before the response is received.
|
||||
do {
|
||||
let response = try sayHello.response.wait()
|
||||
print("Greeter SayHello received: \(response.object.message ?? "Unknown")")
|
||||
} catch {
|
||||
print("Greeter failed: \(error)")
|
||||
}
|
||||
|
||||
let surname = builder.create(string: name)
|
||||
let manyRoot = models_HelloRequest.createHelloRequest(
|
||||
&builder,
|
||||
nameOffset: surname)
|
||||
builder.finish(offset: manyRoot)
|
||||
|
||||
let call = greeter.SayManyHellos(Message(builder: &builder)) { message in
|
||||
print(
|
||||
"Greeter SayManyHellos received: \(message.object.message ?? "Unknown")")
|
||||
}
|
||||
|
||||
let status = try! call.status.recover { _ in .processingError }.wait()
|
||||
if status.code != .ok {
|
||||
print("RPC failed: \(status)")
|
||||
}
|
||||
}
|
||||
|
||||
func main(args: [String]) {
|
||||
// arg0 (dropped) is the program name. We expect arg1 to be the port, and arg2 (optional) to be
|
||||
// the name sent in the request.
|
||||
let arg1 = args.dropFirst(1).first
|
||||
let arg2 = args.dropFirst(2).first
|
||||
|
||||
switch (arg1.flatMap(Int.init), arg2) {
|
||||
case (.none, _):
|
||||
print("Usage: PORT [NAME]")
|
||||
exit(1)
|
||||
|
||||
case let (.some(port), name):
|
||||
// Setup an `EventLoopGroup` for the connection to run on.
|
||||
//
|
||||
// See: https://github.com/apple/swift-nio#eventloops-and-eventloopgroups
|
||||
let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
|
||||
|
||||
// Make sure the group is shutdown when we're done with it.
|
||||
defer {
|
||||
try! group.syncShutdownGracefully()
|
||||
}
|
||||
|
||||
// Configure the channel, we're not using TLS so the connection is `insecure`.
|
||||
let channel = ClientConnection.insecure(group: group)
|
||||
.connect(host: "localhost", port: port)
|
||||
|
||||
// Close the connection when we're done with it.
|
||||
defer {
|
||||
try! channel.close().wait()
|
||||
}
|
||||
|
||||
// Provide the connection to the generated client.
|
||||
let greeter = models_GreeterServiceClient(channel: channel)
|
||||
|
||||
// Do the greeting.
|
||||
greet(name: name ?? "FlatBuffers!", client: greeter)
|
||||
}
|
||||
}
|
||||
|
||||
main(args: CommandLine.arguments)
|
||||
+96
@@ -0,0 +1,96 @@
|
||||
/*
|
||||
* Copyright 2023 Google Inc. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import FlatBuffers
|
||||
import GRPC
|
||||
import Logging
|
||||
import Model
|
||||
import NIO
|
||||
|
||||
class Greeter: models_GreeterProvider {
|
||||
|
||||
var interceptors: models_GreeterServerInterceptorFactoryProtocol?
|
||||
|
||||
let greetings: [String]
|
||||
|
||||
init() {
|
||||
greetings = ["Hi", "Hallo", "Ciao"]
|
||||
}
|
||||
|
||||
func SayHello(
|
||||
request: Message<models_HelloRequest>,
|
||||
context: StatusOnlyCallContext)
|
||||
-> EventLoopFuture<Message<models_HelloReply>>
|
||||
{
|
||||
let recipient = request.object.name ?? "Stranger"
|
||||
|
||||
var builder = FlatBufferBuilder()
|
||||
let off = builder.create(string: "Hello \(recipient)")
|
||||
let root = models_HelloReply.createHelloReply(&builder, messageOffset: off)
|
||||
builder.finish(offset: root)
|
||||
return context.eventLoop
|
||||
.makeSucceededFuture(Message<models_HelloReply>(builder: &builder))
|
||||
}
|
||||
|
||||
func SayManyHellos(
|
||||
request: Message<models_HelloRequest>,
|
||||
context: StreamingResponseCallContext<Message<models_HelloReply>>)
|
||||
-> EventLoopFuture<GRPCStatus>
|
||||
{
|
||||
for name in greetings {
|
||||
var builder = FlatBufferBuilder()
|
||||
let off = builder
|
||||
.create(string: "\(name) \(request.object.name ?? "Unknown")")
|
||||
let root = models_HelloReply.createHelloReply(
|
||||
&builder,
|
||||
messageOffset: off)
|
||||
builder.finish(offset: root)
|
||||
_ = context.sendResponse(Message<models_HelloReply>(builder: &builder))
|
||||
}
|
||||
return context.eventLoop.makeSucceededFuture(.ok)
|
||||
}
|
||||
}
|
||||
|
||||
// Quieten the logs.
|
||||
LoggingSystem.bootstrap {
|
||||
var handler = StreamLogHandler.standardOutput(label: $0)
|
||||
handler.logLevel = .critical
|
||||
return handler
|
||||
}
|
||||
|
||||
let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
|
||||
defer {
|
||||
try! group.syncShutdownGracefully()
|
||||
}
|
||||
|
||||
// Create some configuration for the server:
|
||||
let configuration = Server.Configuration(
|
||||
target: .hostAndPort("localhost", 0),
|
||||
eventLoopGroup: group,
|
||||
serviceProviders: [Greeter()])
|
||||
|
||||
// Start the server and print its address once it has started.
|
||||
let server = Server.start(configuration: configuration)
|
||||
server.map {
|
||||
$0.channel.localAddress
|
||||
}.whenSuccess { address in
|
||||
print("server started on port \(address!.port!)")
|
||||
}
|
||||
|
||||
// Wait on the server's `onClose` future to stop the program from exiting.
|
||||
_ = try server.flatMap {
|
||||
$0.onClose
|
||||
}.wait()
|
||||
@@ -0,0 +1,13 @@
|
||||
# TS Greeter example
|
||||
|
||||
The following is an example on how to run the TS grpc server. Make sure that you have `Typescript` installed
|
||||
|
||||
you would need to run `npm run build` or simply use `npm install && tsc`
|
||||
|
||||
## How to run Server:
|
||||
|
||||
- `npm run server`
|
||||
|
||||
## How to run Client:
|
||||
|
||||
- `npm run client 3000`
|
||||
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"name": "flatbuffers-js-grpc",
|
||||
"version": "1.0.0",
|
||||
"author": "mustii@mmk.one",
|
||||
"scripts": {
|
||||
"build": "npm install && tsc",
|
||||
"client": "node dist/client.js",
|
||||
"server": "node dist/server.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"@grpc/grpc-js": "^1.3.2",
|
||||
"flatbuffers": "^2.0.0"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
import * as grpc from '@grpc/grpc-js';
|
||||
import * as flatbuffers from 'flatbuffers';
|
||||
import { HelloReply } from './models/hello-reply';
|
||||
import { HelloRequest } from './models/hello-request';
|
||||
import { GreeterClient } from './greeter_grpc';
|
||||
|
||||
async function main(PORT: Number, name: string) {
|
||||
const client = new GreeterClient(`localhost:${PORT}`, grpc.credentials.createInsecure());
|
||||
const builder = new flatbuffers.Builder();
|
||||
const offset = builder.createString(name);
|
||||
const root = HelloRequest.createHelloRequest(builder, offset);
|
||||
builder.finish(root);
|
||||
const buffer = HelloRequest.getRootAsHelloRequest(new flatbuffers.ByteBuffer(builder.asUint8Array()));
|
||||
|
||||
client.SayHello(buffer, (err, response) => {
|
||||
console.log(response.message());
|
||||
});
|
||||
|
||||
const data = client.SayManyHellos(buffer, null);
|
||||
|
||||
data.on('data', (data) => {
|
||||
console.log(data.message());
|
||||
});
|
||||
}
|
||||
|
||||
const args = process.argv.slice(2)
|
||||
const PORT = Number(args[0]);
|
||||
const name: string = args[1] ?? "flatbuffers";
|
||||
|
||||
if (PORT) {
|
||||
main(PORT, name);
|
||||
} else {
|
||||
throw new Error("Requires a valid port number.")
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
// automatically generated by the FlatBuffers compiler, do not modify
|
||||
|
||||
/* eslint-disable @typescript-eslint/no-unused-vars, @typescript-eslint/no-explicit-any, @typescript-eslint/no-non-null-assertion */
|
||||
|
||||
export * as models from './models.js';
|
||||
@@ -0,0 +1,4 @@
|
||||
// automatically generated by the FlatBuffers compiler, do not modify
|
||||
|
||||
export { HelloReply } from './models/hello-reply.js';
|
||||
export { HelloRequest } from './models/hello-request.js';
|
||||
@@ -0,0 +1,56 @@
|
||||
// Generated GRPC code for FlatBuffers TS *** DO NOT EDIT ***
|
||||
import * as flatbuffers from 'flatbuffers';
|
||||
import { HelloReply as models_HelloReply } from './models/hello-reply';
|
||||
import { HelloRequest as models_HelloRequest } from './models/hello-request';
|
||||
|
||||
import * as grpc from '@grpc/grpc-js';
|
||||
|
||||
interface IGreeterService extends grpc.ServiceDefinition<grpc.UntypedServiceImplementation> {
|
||||
SayHello: IGreeterService_ISayHello;
|
||||
SayManyHellos: IGreeterService_ISayManyHellos;
|
||||
}
|
||||
interface IGreeterService_ISayHello extends grpc.MethodDefinition<models_HelloRequest, models_HelloReply> {
|
||||
path: string; // /models.Greeter/SayHello
|
||||
requestStream: boolean; // false
|
||||
responseStream: boolean; // false
|
||||
requestSerialize: grpc.serialize<models_HelloRequest>;
|
||||
requestDeserialize: grpc.deserialize<models_HelloRequest>;
|
||||
responseSerialize: grpc.serialize<models_HelloReply>;
|
||||
responseDeserialize: grpc.deserialize<models_HelloReply>;
|
||||
}
|
||||
|
||||
interface IGreeterService_ISayManyHellos extends grpc.MethodDefinition<models_HelloRequest, models_HelloReply> {
|
||||
path: string; // /models.Greeter/SayManyHellos
|
||||
requestStream: boolean; // false
|
||||
responseStream: boolean; // true
|
||||
requestSerialize: grpc.serialize<models_HelloRequest>;
|
||||
requestDeserialize: grpc.deserialize<models_HelloRequest>;
|
||||
responseSerialize: grpc.serialize<models_HelloReply>;
|
||||
responseDeserialize: grpc.deserialize<models_HelloReply>;
|
||||
}
|
||||
|
||||
|
||||
export const GreeterService: IGreeterService;
|
||||
|
||||
export interface IGreeterServer extends grpc.UntypedServiceImplementation {
|
||||
SayHello: grpc.handleUnaryCall<models_HelloRequest, models_HelloReply>;
|
||||
SayManyHellos: grpc.handleServerStreamingCall<models_HelloRequest, models_HelloReply>;
|
||||
}
|
||||
|
||||
export interface IGreeterClient {
|
||||
SayHello(request: models_HelloRequest, callback: (error: grpc.ServiceError | null, response: models_HelloReply) => void): grpc.ClientUnaryCall;
|
||||
SayHello(request: models_HelloRequest, metadata: grpc.Metadata, callback: (error: grpc.ServiceError | null, response: models_HelloReply) => void): grpc.ClientUnaryCall;
|
||||
SayHello(request: models_HelloRequest, metadata: grpc.Metadata, options: Partial<grpc.CallOptions>, callback: (error: grpc.ServiceError | null, response: models_HelloReply) => void): grpc.ClientUnaryCall;
|
||||
SayManyHellos(request: models_HelloRequest, metadata: grpc.Metadata): grpc.ClientReadableStream<models_HelloReply>;
|
||||
SayManyHellos(request: models_HelloRequest, options: Partial<grpc.CallOptions>): grpc.ClientReadableStream<models_HelloReply>;
|
||||
}
|
||||
|
||||
export class GreeterClient extends grpc.Client implements IGreeterClient {
|
||||
constructor(address: string, credentials: grpc.ChannelCredentials, options?: object);
|
||||
public SayHello(request: models_HelloRequest, callback: (error: grpc.ServiceError | null, response: models_HelloReply) => void): grpc.ClientUnaryCall;
|
||||
public SayHello(request: models_HelloRequest, metadata: grpc.Metadata, callback: (error: grpc.ServiceError | null, response: models_HelloReply) => void): grpc.ClientUnaryCall;
|
||||
public SayHello(request: models_HelloRequest, metadata: grpc.Metadata, options: Partial<grpc.CallOptions>, callback: (error: grpc.ServiceError | null, response: models_HelloReply) => void): grpc.ClientUnaryCall;
|
||||
public SayManyHellos(request: models_HelloRequest, metadata: grpc.Metadata): grpc.ClientReadableStream<models_HelloReply>;
|
||||
public SayManyHellos(request: models_HelloRequest, options: Partial<grpc.CallOptions>): grpc.ClientReadableStream<models_HelloReply>;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
// Generated GRPC code for FlatBuffers TS *** DO NOT EDIT ***
|
||||
import * as flatbuffers from 'flatbuffers';
|
||||
import { HelloReply as models_HelloReply } from './models/hello-reply';
|
||||
import { HelloRequest as models_HelloRequest } from './models/hello-request';
|
||||
|
||||
var grpc = require('@grpc/grpc-js');
|
||||
|
||||
function serialize_models_HelloReply(buffer_args) {
|
||||
if (!(buffer_args instanceof models_HelloReply)) {
|
||||
throw new Error('Expected argument of type HelloReply');
|
||||
}
|
||||
return Buffer.from(buffer_args.serialize());
|
||||
}
|
||||
|
||||
function deserialize_models_HelloReply(buffer) {
|
||||
return models_HelloReply.getRootAsHelloReply(new flatbuffers.ByteBuffer(buffer))
|
||||
}
|
||||
|
||||
|
||||
function serialize_models_HelloRequest(buffer_args) {
|
||||
if (!(buffer_args instanceof models_HelloRequest)) {
|
||||
throw new Error('Expected argument of type HelloRequest');
|
||||
}
|
||||
return Buffer.from(buffer_args.serialize());
|
||||
}
|
||||
|
||||
function deserialize_models_HelloRequest(buffer) {
|
||||
return models_HelloRequest.getRootAsHelloRequest(new flatbuffers.ByteBuffer(buffer))
|
||||
}
|
||||
|
||||
|
||||
var GreeterService = exports.GreeterService = {
|
||||
SayHello: {
|
||||
path: '/models.Greeter/SayHello',
|
||||
requestStream: false,
|
||||
responseStream: false,
|
||||
requestType: flatbuffers.ByteBuffer,
|
||||
responseType: models_HelloReply,
|
||||
requestSerialize: serialize_models_HelloRequest,
|
||||
requestDeserialize: deserialize_models_HelloRequest,
|
||||
responseSerialize: serialize_models_HelloReply,
|
||||
responseDeserialize: deserialize_models_HelloReply,
|
||||
},
|
||||
SayManyHellos: {
|
||||
path: '/models.Greeter/SayManyHellos',
|
||||
requestStream: false,
|
||||
responseStream: true,
|
||||
requestType: flatbuffers.ByteBuffer,
|
||||
responseType: models_HelloReply,
|
||||
requestSerialize: serialize_models_HelloRequest,
|
||||
requestDeserialize: deserialize_models_HelloRequest,
|
||||
responseSerialize: serialize_models_HelloReply,
|
||||
responseDeserialize: deserialize_models_HelloReply,
|
||||
},
|
||||
};
|
||||
exports.GreeterClient = grpc.makeGenericClientConstructor(GreeterService);
|
||||
@@ -0,0 +1,6 @@
|
||||
// automatically generated by the FlatBuffers compiler, do not modify
|
||||
|
||||
/* eslint-disable @typescript-eslint/no-unused-vars, @typescript-eslint/no-explicit-any, @typescript-eslint/no-non-null-assertion */
|
||||
|
||||
export { HelloReply } from './models/hello-reply.js';
|
||||
export { HelloRequest } from './models/hello-request.js';
|
||||
@@ -0,0 +1,60 @@
|
||||
// automatically generated by the FlatBuffers compiler, do not modify
|
||||
|
||||
/* eslint-disable @typescript-eslint/no-unused-vars, @typescript-eslint/no-explicit-any, @typescript-eslint/no-non-null-assertion */
|
||||
|
||||
import * as flatbuffers from 'flatbuffers';
|
||||
|
||||
|
||||
|
||||
export class HelloReply {
|
||||
bb: flatbuffers.ByteBuffer|null = null;
|
||||
bb_pos = 0;
|
||||
__init(i:number, bb:flatbuffers.ByteBuffer):HelloReply {
|
||||
this.bb_pos = i;
|
||||
this.bb = bb;
|
||||
return this;
|
||||
}
|
||||
|
||||
static getRootAsHelloReply(bb:flatbuffers.ByteBuffer, obj?:HelloReply):HelloReply {
|
||||
return (obj || new HelloReply()).__init(bb.readInt32(bb.position()) + bb.position(), bb);
|
||||
}
|
||||
|
||||
static getSizePrefixedRootAsHelloReply(bb:flatbuffers.ByteBuffer, obj?:HelloReply):HelloReply {
|
||||
bb.setPosition(bb.position() + flatbuffers.SIZE_PREFIX_LENGTH);
|
||||
return (obj || new HelloReply()).__init(bb.readInt32(bb.position()) + bb.position(), bb);
|
||||
}
|
||||
|
||||
message():string|null
|
||||
message(optionalEncoding:flatbuffers.Encoding):string|Uint8Array|null
|
||||
message(optionalEncoding?:any):string|Uint8Array|null {
|
||||
const offset = this.bb!.__offset(this.bb_pos, 4);
|
||||
return offset ? this.bb!.__string(this.bb_pos + offset, optionalEncoding) : null;
|
||||
}
|
||||
|
||||
static startHelloReply(builder:flatbuffers.Builder) {
|
||||
builder.startObject(1);
|
||||
}
|
||||
|
||||
static addMessage(builder:flatbuffers.Builder, messageOffset:flatbuffers.Offset) {
|
||||
builder.addFieldOffset(0, messageOffset, 0);
|
||||
}
|
||||
|
||||
static endHelloReply(builder:flatbuffers.Builder):flatbuffers.Offset {
|
||||
const offset = builder.endObject();
|
||||
return offset;
|
||||
}
|
||||
|
||||
static createHelloReply(builder:flatbuffers.Builder, messageOffset:flatbuffers.Offset):flatbuffers.Offset {
|
||||
HelloReply.startHelloReply(builder);
|
||||
HelloReply.addMessage(builder, messageOffset);
|
||||
return HelloReply.endHelloReply(builder);
|
||||
}
|
||||
|
||||
serialize():Uint8Array {
|
||||
return this.bb!.bytes();
|
||||
}
|
||||
|
||||
static deserialize(buffer: Uint8Array):HelloReply {
|
||||
return HelloReply.getRootAsHelloReply(new flatbuffers.ByteBuffer(buffer))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
// automatically generated by the FlatBuffers compiler, do not modify
|
||||
|
||||
/* eslint-disable @typescript-eslint/no-unused-vars, @typescript-eslint/no-explicit-any, @typescript-eslint/no-non-null-assertion */
|
||||
|
||||
import * as flatbuffers from 'flatbuffers';
|
||||
|
||||
|
||||
|
||||
export class HelloRequest {
|
||||
bb: flatbuffers.ByteBuffer|null = null;
|
||||
bb_pos = 0;
|
||||
__init(i:number, bb:flatbuffers.ByteBuffer):HelloRequest {
|
||||
this.bb_pos = i;
|
||||
this.bb = bb;
|
||||
return this;
|
||||
}
|
||||
|
||||
static getRootAsHelloRequest(bb:flatbuffers.ByteBuffer, obj?:HelloRequest):HelloRequest {
|
||||
return (obj || new HelloRequest()).__init(bb.readInt32(bb.position()) + bb.position(), bb);
|
||||
}
|
||||
|
||||
static getSizePrefixedRootAsHelloRequest(bb:flatbuffers.ByteBuffer, obj?:HelloRequest):HelloRequest {
|
||||
bb.setPosition(bb.position() + flatbuffers.SIZE_PREFIX_LENGTH);
|
||||
return (obj || new HelloRequest()).__init(bb.readInt32(bb.position()) + bb.position(), bb);
|
||||
}
|
||||
|
||||
name():string|null
|
||||
name(optionalEncoding:flatbuffers.Encoding):string|Uint8Array|null
|
||||
name(optionalEncoding?:any):string|Uint8Array|null {
|
||||
const offset = this.bb!.__offset(this.bb_pos, 4);
|
||||
return offset ? this.bb!.__string(this.bb_pos + offset, optionalEncoding) : null;
|
||||
}
|
||||
|
||||
static startHelloRequest(builder:flatbuffers.Builder) {
|
||||
builder.startObject(1);
|
||||
}
|
||||
|
||||
static addName(builder:flatbuffers.Builder, nameOffset:flatbuffers.Offset) {
|
||||
builder.addFieldOffset(0, nameOffset, 0);
|
||||
}
|
||||
|
||||
static endHelloRequest(builder:flatbuffers.Builder):flatbuffers.Offset {
|
||||
const offset = builder.endObject();
|
||||
return offset;
|
||||
}
|
||||
|
||||
static createHelloRequest(builder:flatbuffers.Builder, nameOffset:flatbuffers.Offset):flatbuffers.Offset {
|
||||
HelloRequest.startHelloRequest(builder);
|
||||
HelloRequest.addName(builder, nameOffset);
|
||||
return HelloRequest.endHelloRequest(builder);
|
||||
}
|
||||
|
||||
serialize():Uint8Array {
|
||||
return this.bb!.bytes();
|
||||
}
|
||||
|
||||
static deserialize(buffer: Uint8Array):HelloRequest {
|
||||
return HelloRequest.getRootAsHelloRequest(new flatbuffers.ByteBuffer(buffer))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
import * as grpc from '@grpc/grpc-js';
|
||||
import * as flatbuffers from 'flatbuffers';
|
||||
import { HelloReply } from './models/hello-reply';
|
||||
import { HelloRequest } from './models/hello-request';
|
||||
import { IGreeterServer, GreeterService } from './greeter_grpc';
|
||||
|
||||
const greeter: IGreeterServer = {
|
||||
SayHello(call: grpc.ServerUnaryCall<HelloRequest, HelloReply>, callback: grpc.sendUnaryData<HelloReply>): void {
|
||||
console.log(`SayHello ${call.request.name()}`);
|
||||
const builder = new flatbuffers.Builder();
|
||||
const offset = builder.createString(`welcome ${call.request.name()}`);
|
||||
const root = HelloReply.createHelloReply(builder, offset);
|
||||
builder.finish(root);
|
||||
callback(null, HelloReply.getRootAsHelloReply(new flatbuffers.ByteBuffer(builder.asUint8Array())));
|
||||
},
|
||||
async SayManyHellos(call: grpc.ServerWritableStream<HelloRequest, HelloReply>): Promise<void> {
|
||||
const name = call.request.name();
|
||||
console.log(`${call.request.name()} saying hi in different langagues`);
|
||||
['Hi', 'Hallo', 'Ciao'].forEach(element => {
|
||||
const builder = new flatbuffers.Builder();
|
||||
const offset = builder.createString(`${element} ${name}`);
|
||||
const root = HelloReply.createHelloReply(builder, offset);
|
||||
builder.finish(root);
|
||||
call.write(HelloReply.getRootAsHelloReply(new flatbuffers.ByteBuffer(builder.asUint8Array())))
|
||||
});
|
||||
call.end();
|
||||
}
|
||||
}
|
||||
|
||||
function serve(): void {
|
||||
const PORT = 3000;
|
||||
const server = new grpc.Server();
|
||||
server.addService(GreeterService, greeter);
|
||||
console.log(`Listening on ${PORT}`);
|
||||
server.bindAsync(
|
||||
`localhost:${PORT}`,
|
||||
grpc.ServerCredentials.createInsecure(),
|
||||
(err: Error | null, port: number) => {
|
||||
if (err) {
|
||||
console.error(`Server error: ${err.message}`);
|
||||
} else {
|
||||
console.log(`Server bound on port: ${port}`);
|
||||
server.start();
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
serve();
|
||||
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "es5",
|
||||
"module": "commonjs",
|
||||
"outDir": "./dist",
|
||||
"allowJs": true,
|
||||
"sourceMap": true,
|
||||
"strict": true,
|
||||
"noImplicitAny": false,
|
||||
"strictNullChecks": false,
|
||||
"esModuleInterop": true,
|
||||
"baseUrl": "./",
|
||||
"typeRoots": ["node_modules/@types"],
|
||||
"skipLibCheck": true,
|
||||
"forceConsistentCasingInFileNames": true
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,7 @@
|
||||
<parent>
|
||||
<groupId>com.google.flatbuffers</groupId>
|
||||
<artifactId>flatbuffers-parent</artifactId>
|
||||
<version>1.11.1</version>
|
||||
<version>2.0.3</version>
|
||||
</parent>
|
||||
<artifactId>flatbuffers-java-grpc</artifactId>
|
||||
<name>${project.artifactId}</name>
|
||||
@@ -24,7 +24,7 @@
|
||||
</developer>
|
||||
</developers>
|
||||
<properties>
|
||||
<gRPC.version>1.11.1</gRPC.version>
|
||||
<gRPC.version>1.36.0</gRPC.version>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
|
||||
Vendored
+8
-2
@@ -4,7 +4,7 @@
|
||||
<groupId>com.google.flatbuffers</groupId>
|
||||
<artifactId>flatbuffers-parent</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
<version>1.11.1</version>
|
||||
<version>2.0.3</version>
|
||||
<name>flatbuffers-parent</name>
|
||||
<description>parent pom for flatbuffers java artifacts</description>
|
||||
<properties>
|
||||
@@ -52,7 +52,7 @@
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<version>4.13.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
@@ -185,6 +185,12 @@
|
||||
<goals>
|
||||
<goal>sign</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<gpgArguments>
|
||||
<arg>--pinentry-mode</arg>
|
||||
<arg>loopback</arg>
|
||||
</gpgArguments>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
CXXFLAGS ?= -I../../../include
|
||||
LDFLAGS ?=
|
||||
|
||||
.PHONY: all
|
||||
all: server client
|
||||
|
||||
greeter_generated: greeter.fbs
|
||||
flatc --grpc --cpp $<
|
||||
|
||||
server: greeter_generated server.cpp greeter.grpc.fb.cc greeter.grpc.fb.h
|
||||
g++ -std=c++11 -O2 $(CXXFLAGS) $(LDFLAGS) -lgpr -lgrpc -lgrpc++ server.cpp greeter.grpc.fb.cc -o $@
|
||||
|
||||
client: greeter_generated client.cpp greeter.grpc.fb.cc greeter.grpc.fb.h
|
||||
g++ -std=c++11 -O2 $(CXXFLAGS) $(LDFLAGS) -lgpr -lgrpc -lgrpc++ client.cpp greeter.grpc.fb.cc -o $@
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
#include "greeter.grpc.fb.h"
|
||||
#include "greeter_generated.h"
|
||||
|
||||
#include <grpc++/grpc++.h>
|
||||
#include <grpcpp/grpcpp.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
|
||||
+3
-2
@@ -1,7 +1,7 @@
|
||||
#include "greeter.grpc.fb.h"
|
||||
#include "greeter_generated.h"
|
||||
|
||||
#include <grpc++/grpc++.h>
|
||||
#include <grpcpp/grpcpp.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
@@ -12,7 +12,8 @@ class GreeterServiceImpl final : public Greeter::Service {
|
||||
grpc::ServerContext *context,
|
||||
const flatbuffers::grpc::Message<HelloRequest> *request_msg,
|
||||
flatbuffers::grpc::Message<HelloReply> *response_msg) override {
|
||||
// flatbuffers::grpc::MessageBuilder mb_;
|
||||
flatbuffers::grpc::MessageBuilder mb_;
|
||||
|
||||
// We call GetRoot to "parse" the message. Verification is already
|
||||
// performed by default. See the notes below for more details.
|
||||
const HelloRequest *request = request_msg->GetRoot();
|
||||
|
||||
@@ -0,0 +1,131 @@
|
||||
load("@rules_cc//cc:defs.bzl", "cc_library")
|
||||
|
||||
package(
|
||||
default_visibility = ["//visibility:public"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "distribution",
|
||||
srcs = [
|
||||
"BUILD.bazel",
|
||||
] + glob([
|
||||
"*.cc",
|
||||
"*.h",
|
||||
]),
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "common_headers",
|
||||
srcs = [
|
||||
"schema_interface.h",
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "cpp_generator",
|
||||
srcs = [
|
||||
"cpp_generator.cc",
|
||||
],
|
||||
hdrs = [
|
||||
"cpp_generator.h",
|
||||
":common_headers",
|
||||
],
|
||||
include_prefix = "src/compiler",
|
||||
strip_include_prefix = "/grpc/src/compiler",
|
||||
deps = [
|
||||
"//:flatbuffers",
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "go_generator",
|
||||
srcs = [
|
||||
"go_generator.cc",
|
||||
],
|
||||
hdrs = [
|
||||
"go_generator.h",
|
||||
":common_headers",
|
||||
],
|
||||
include_prefix = "src/compiler",
|
||||
strip_include_prefix = "/grpc/src/compiler",
|
||||
deps = [
|
||||
"//:flatbuffers",
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "java_generator",
|
||||
srcs = [
|
||||
"java_generator.cc",
|
||||
],
|
||||
hdrs = [
|
||||
"java_generator.h",
|
||||
":common_headers",
|
||||
],
|
||||
include_prefix = "src/compiler",
|
||||
strip_include_prefix = "/grpc/src/compiler",
|
||||
deps = [
|
||||
"//:flatbuffers",
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "python_generator",
|
||||
hdrs = [
|
||||
"python_generator.h",
|
||||
],
|
||||
include_prefix = "src/compiler",
|
||||
strip_include_prefix = "/grpc/src/compiler",
|
||||
deps = [
|
||||
":python_generator_private",
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "python_generator_private",
|
||||
srcs = [
|
||||
"python_generator.cc",
|
||||
],
|
||||
hdrs = [
|
||||
"python_generator.h",
|
||||
":common_headers",
|
||||
],
|
||||
include_prefix = "src/compiler",
|
||||
strip_include_prefix = "/grpc/src/compiler",
|
||||
visibility = ["//visibility:private"],
|
||||
deps = [
|
||||
"//:flatbuffers",
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "swift_generator",
|
||||
srcs = [
|
||||
"swift_generator.cc",
|
||||
],
|
||||
hdrs = [
|
||||
"swift_generator.h",
|
||||
":common_headers",
|
||||
],
|
||||
include_prefix = "src/compiler",
|
||||
strip_include_prefix = "/grpc/src/compiler",
|
||||
deps = [
|
||||
"//:flatbuffers",
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "ts_generator",
|
||||
srcs = [
|
||||
"ts_generator.cc",
|
||||
],
|
||||
hdrs = [
|
||||
"ts_generator.h",
|
||||
":common_headers",
|
||||
],
|
||||
include_prefix = "src/compiler",
|
||||
strip_include_prefix = "/grpc/src/compiler",
|
||||
deps = [
|
||||
"//:flatbuffers",
|
||||
],
|
||||
)
|
||||
@@ -1,40 +0,0 @@
|
||||
/*
|
||||
*
|
||||
* Copyright 2015, Google Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are
|
||||
* met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above
|
||||
* copyright notice, this list of conditions and the following disclaimer
|
||||
* in the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
* * Neither the name of Google Inc. nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from
|
||||
* this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef SRC_COMPILER_CONFIG_H
|
||||
#define SRC_COMPILER_CONFIG_H
|
||||
|
||||
// This file is here only because schema_interface.h, which is copied from gRPC,
|
||||
// includes it. There is nothing for Flatbuffers to configure.
|
||||
|
||||
#endif // SRC_COMPILER_CONFIG_H
|
||||
+204
-218
@@ -1,65 +1,31 @@
|
||||
/*
|
||||
*
|
||||
* Copyright 2015, Google Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are
|
||||
* met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above
|
||||
* copyright notice, this list of conditions and the following disclaimer
|
||||
* in the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
* * Neither the name of Google Inc. nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from
|
||||
* this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
#include "src/compiler/cpp_generator.h"
|
||||
|
||||
#include <map>
|
||||
|
||||
#include "src/compiler/cpp_generator.h"
|
||||
#include "flatbuffers/util.h"
|
||||
|
||||
#include <sstream>
|
||||
|
||||
#include "flatbuffers/util.h"
|
||||
|
||||
namespace grpc_cpp_generator {
|
||||
namespace {
|
||||
|
||||
grpc::string message_header_ext() { return "_generated.h"; }
|
||||
grpc::string service_header_ext() { return ".grpc.fb.h"; }
|
||||
static grpc::string service_header_ext() { return ".grpc.fb.h"; }
|
||||
|
||||
template <class T>
|
||||
grpc::string as_string(T x) {
|
||||
template<class T>
|
||||
static grpc::string as_string(T x) {
|
||||
std::ostringstream out;
|
||||
out << x;
|
||||
return out.str();
|
||||
}
|
||||
|
||||
inline bool ClientOnlyStreaming(const grpc_generator::Method *method) {
|
||||
static inline bool ClientOnlyStreaming(const grpc_generator::Method *method) {
|
||||
return method->ClientStreaming() && !method->ServerStreaming();
|
||||
}
|
||||
|
||||
inline bool ServerOnlyStreaming(const grpc_generator::Method *method) {
|
||||
static inline bool ServerOnlyStreaming(const grpc_generator::Method *method) {
|
||||
return !method->ClientStreaming() && method->ServerStreaming();
|
||||
}
|
||||
|
||||
grpc::string FilenameIdentifier(const grpc::string &filename) {
|
||||
static grpc::string FilenameIdentifier(const grpc::string &filename) {
|
||||
grpc::string result;
|
||||
for (unsigned i = 0; i < filename.size(); i++) {
|
||||
char c = filename[i];
|
||||
@@ -74,14 +40,11 @@ grpc::string FilenameIdentifier(const grpc::string &filename) {
|
||||
}
|
||||
return result;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
template <class T, size_t N>
|
||||
T *array_end(T (&array)[N]) {
|
||||
return array + N;
|
||||
}
|
||||
template<class T, size_t N>
|
||||
static T *array_end(T (&array)[N]) { return array + N; }
|
||||
|
||||
void PrintIncludes(grpc_generator::Printer *printer,
|
||||
static void PrintIncludes(grpc_generator::Printer *printer,
|
||||
const std::vector<grpc::string> &headers,
|
||||
const Parameters ¶ms) {
|
||||
std::map<grpc::string, grpc::string> vars;
|
||||
@@ -92,9 +55,7 @@ void PrintIncludes(grpc_generator::Printer *printer,
|
||||
auto &s = params.grpc_search_path;
|
||||
if (!s.empty()) {
|
||||
vars["l"] += s;
|
||||
if (s[s.size() - 1] != '/') {
|
||||
vars["l"] += '/';
|
||||
}
|
||||
if (s[s.size() - 1] != '/') { vars["l"] += '/'; }
|
||||
}
|
||||
|
||||
for (auto i = headers.begin(); i != headers.end(); i++) {
|
||||
@@ -103,8 +64,10 @@ void PrintIncludes(grpc_generator::Printer *printer,
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
grpc::string GetHeaderPrologue(grpc_generator::File *file,
|
||||
const Parameters & /*params*/) {
|
||||
const Parameters ¶ms) {
|
||||
grpc::string output;
|
||||
{
|
||||
// Scope the output stream so it closes and finalizes output to the string.
|
||||
@@ -114,7 +77,7 @@ grpc::string GetHeaderPrologue(grpc_generator::File *file,
|
||||
vars["filename"] = file->filename();
|
||||
vars["filename_identifier"] = FilenameIdentifier(file->filename());
|
||||
vars["filename_base"] = file->filename_without_ext();
|
||||
vars["message_header_ext"] = message_header_ext();
|
||||
vars["message_header_ext"] = params.message_header_extension;
|
||||
|
||||
printer->Print(vars, "// Generated by the gRPC C++ plugin.\n");
|
||||
printer->Print(vars,
|
||||
@@ -144,15 +107,16 @@ grpc::string GetHeaderIncludes(grpc_generator::File *file,
|
||||
std::map<grpc::string, grpc::string> vars;
|
||||
|
||||
static const char *headers_strs[] = {
|
||||
"grpc++/impl/codegen/async_stream.h",
|
||||
"grpc++/impl/codegen/async_unary_call.h",
|
||||
"grpc++/impl/codegen/method_handler_impl.h",
|
||||
"grpc++/impl/codegen/proto_utils.h",
|
||||
"grpc++/impl/codegen/rpc_method.h",
|
||||
"grpc++/impl/codegen/service_type.h",
|
||||
"grpc++/impl/codegen/status.h",
|
||||
"grpc++/impl/codegen/stub_options.h",
|
||||
"grpc++/impl/codegen/sync_stream.h"};
|
||||
"grpcpp/impl/codegen/async_stream.h",
|
||||
"grpcpp/impl/codegen/async_unary_call.h",
|
||||
"grpcpp/impl/codegen/method_handler.h",
|
||||
"grpcpp/impl/codegen/proto_utils.h",
|
||||
"grpcpp/impl/codegen/rpc_method.h",
|
||||
"grpcpp/impl/codegen/service_type.h",
|
||||
"grpcpp/impl/codegen/status.h",
|
||||
"grpcpp/impl/codegen/stub_options.h",
|
||||
"grpcpp/impl/codegen/sync_stream.h"
|
||||
};
|
||||
std::vector<grpc::string> headers(headers_strs, array_end(headers_strs));
|
||||
PrintIncludes(printer.get(), headers, params);
|
||||
printer->Print(vars, "\n");
|
||||
@@ -176,7 +140,10 @@ grpc::string GetHeaderIncludes(grpc_generator::File *file,
|
||||
return output;
|
||||
}
|
||||
|
||||
void PrintHeaderClientMethodInterfaces(
|
||||
|
||||
namespace {
|
||||
|
||||
static void PrintHeaderClientMethodInterfaces(
|
||||
grpc_generator::Printer *printer, const grpc_generator::Method *method,
|
||||
std::map<grpc::string, grpc::string> *vars, bool is_public) {
|
||||
(*vars)["Method"] = method->name();
|
||||
@@ -187,8 +154,8 @@ void PrintHeaderClientMethodInterfaces(
|
||||
grpc::string prefix;
|
||||
grpc::string method_params; // extra arguments to method
|
||||
grpc::string raw_args; // extra arguments to raw version of method
|
||||
} async_prefixes[] = {{"Async", ", void* tag", ", tag"},
|
||||
{"PrepareAsync", "", ""}};
|
||||
} async_prefixes[] = { { "Async", ", void* tag", ", tag" },
|
||||
{ "PrepareAsync", "", "" } };
|
||||
|
||||
if (is_public) {
|
||||
if (method->NoStreaming()) {
|
||||
@@ -196,8 +163,9 @@ void PrintHeaderClientMethodInterfaces(
|
||||
*vars,
|
||||
"virtual ::grpc::Status $Method$(::grpc::ClientContext* context, "
|
||||
"const $Request$& request, $Response$* response) = 0;\n");
|
||||
for (size_t i = 0; i < sizeof(async_prefixes)/sizeof(async_prefixes[0]); i ++) {
|
||||
auto& async_prefix = async_prefixes[i];
|
||||
for (size_t i = 0; i < sizeof(async_prefixes) / sizeof(async_prefixes[0]);
|
||||
i++) {
|
||||
auto &async_prefix = async_prefixes[i];
|
||||
(*vars)["AsyncPrefix"] = async_prefix.prefix;
|
||||
printer->Print(
|
||||
*vars,
|
||||
@@ -228,8 +196,9 @@ void PrintHeaderClientMethodInterfaces(
|
||||
"($Method$Raw(context, response));\n");
|
||||
printer->Outdent();
|
||||
printer->Print("}\n");
|
||||
for (size_t i = 0; i < sizeof(async_prefixes)/sizeof(async_prefixes[0]); i ++) {
|
||||
auto& async_prefix = async_prefixes[i];
|
||||
for (size_t i = 0; i < sizeof(async_prefixes) / sizeof(async_prefixes[0]);
|
||||
i++) {
|
||||
auto &async_prefix = async_prefixes[i];
|
||||
(*vars)["AsyncPrefix"] = async_prefix.prefix;
|
||||
(*vars)["AsyncMethodParams"] = async_prefix.method_params;
|
||||
(*vars)["AsyncRawArgs"] = async_prefix.raw_args;
|
||||
@@ -262,8 +231,9 @@ void PrintHeaderClientMethodInterfaces(
|
||||
"($Method$Raw(context, request));\n");
|
||||
printer->Outdent();
|
||||
printer->Print("}\n");
|
||||
for (size_t i = 0; i < sizeof(async_prefixes)/sizeof(async_prefixes[0]); i ++) {
|
||||
auto& async_prefix = async_prefixes[i];
|
||||
for (size_t i = 0; i < sizeof(async_prefixes) / sizeof(async_prefixes[0]);
|
||||
i++) {
|
||||
auto &async_prefix = async_prefixes[i];
|
||||
(*vars)["AsyncPrefix"] = async_prefix.prefix;
|
||||
(*vars)["AsyncMethodParams"] = async_prefix.method_params;
|
||||
(*vars)["AsyncRawArgs"] = async_prefix.raw_args;
|
||||
@@ -295,8 +265,9 @@ void PrintHeaderClientMethodInterfaces(
|
||||
"$Method$Raw(context));\n");
|
||||
printer->Outdent();
|
||||
printer->Print("}\n");
|
||||
for (size_t i = 0; i < sizeof(async_prefixes)/sizeof(async_prefixes[0]); i ++) {
|
||||
auto& async_prefix = async_prefixes[i];
|
||||
for (size_t i = 0; i < sizeof(async_prefixes) / sizeof(async_prefixes[0]);
|
||||
i++) {
|
||||
auto &async_prefix = async_prefixes[i];
|
||||
(*vars)["AsyncPrefix"] = async_prefix.prefix;
|
||||
(*vars)["AsyncMethodParams"] = async_prefix.method_params;
|
||||
(*vars)["AsyncRawArgs"] = async_prefix.raw_args;
|
||||
@@ -318,8 +289,9 @@ void PrintHeaderClientMethodInterfaces(
|
||||
}
|
||||
} else {
|
||||
if (method->NoStreaming()) {
|
||||
for (size_t i = 0; i < sizeof(async_prefixes)/sizeof(async_prefixes[0]); i ++) {
|
||||
auto& async_prefix = async_prefixes[i];
|
||||
for (size_t i = 0; i < sizeof(async_prefixes) / sizeof(async_prefixes[0]);
|
||||
i++) {
|
||||
auto &async_prefix = async_prefixes[i];
|
||||
(*vars)["AsyncPrefix"] = async_prefix.prefix;
|
||||
printer->Print(
|
||||
*vars,
|
||||
@@ -334,8 +306,9 @@ void PrintHeaderClientMethodInterfaces(
|
||||
"virtual ::grpc::ClientWriterInterface< $Request$>*"
|
||||
" $Method$Raw("
|
||||
"::grpc::ClientContext* context, $Response$* response) = 0;\n");
|
||||
for (size_t i = 0; i < sizeof(async_prefixes)/sizeof(async_prefixes[0]); i ++) {
|
||||
auto& async_prefix = async_prefixes[i];
|
||||
for (size_t i = 0; i < sizeof(async_prefixes) / sizeof(async_prefixes[0]);
|
||||
i++) {
|
||||
auto &async_prefix = async_prefixes[i];
|
||||
(*vars)["AsyncPrefix"] = async_prefix.prefix;
|
||||
(*vars)["AsyncMethodParams"] = async_prefix.method_params;
|
||||
printer->Print(
|
||||
@@ -351,8 +324,9 @@ void PrintHeaderClientMethodInterfaces(
|
||||
"virtual ::grpc::ClientReaderInterface< $Response$>* "
|
||||
"$Method$Raw("
|
||||
"::grpc::ClientContext* context, const $Request$& request) = 0;\n");
|
||||
for (size_t i = 0; i < sizeof(async_prefixes)/sizeof(async_prefixes[0]); i ++) {
|
||||
auto& async_prefix = async_prefixes[i];
|
||||
for (size_t i = 0; i < sizeof(async_prefixes) / sizeof(async_prefixes[0]);
|
||||
i++) {
|
||||
auto &async_prefix = async_prefixes[i];
|
||||
(*vars)["AsyncPrefix"] = async_prefix.prefix;
|
||||
(*vars)["AsyncMethodParams"] = async_prefix.method_params;
|
||||
printer->Print(
|
||||
@@ -367,8 +341,9 @@ void PrintHeaderClientMethodInterfaces(
|
||||
"virtual ::grpc::ClientReaderWriterInterface< $Request$, "
|
||||
"$Response$>* "
|
||||
"$Method$Raw(::grpc::ClientContext* context) = 0;\n");
|
||||
for (size_t i = 0; i < sizeof(async_prefixes)/sizeof(async_prefixes[0]); i ++) {
|
||||
auto& async_prefix = async_prefixes[i];
|
||||
for (size_t i = 0; i < sizeof(async_prefixes) / sizeof(async_prefixes[0]);
|
||||
i++) {
|
||||
auto &async_prefix = async_prefixes[i];
|
||||
(*vars)["AsyncPrefix"] = async_prefix.prefix;
|
||||
(*vars)["AsyncMethodParams"] = async_prefix.method_params;
|
||||
printer->Print(
|
||||
@@ -382,7 +357,9 @@ void PrintHeaderClientMethodInterfaces(
|
||||
}
|
||||
}
|
||||
|
||||
void PrintHeaderClientMethod(grpc_generator::Printer *printer,
|
||||
|
||||
|
||||
static void PrintHeaderClientMethod(grpc_generator::Printer *printer,
|
||||
const grpc_generator::Method *method,
|
||||
std::map<grpc::string, grpc::string> *vars,
|
||||
bool is_public) {
|
||||
@@ -393,8 +370,8 @@ void PrintHeaderClientMethod(grpc_generator::Printer *printer,
|
||||
grpc::string prefix;
|
||||
grpc::string method_params; // extra arguments to method
|
||||
grpc::string raw_args; // extra arguments to raw version of method
|
||||
} async_prefixes[] = {{"Async", ", void* tag", ", tag"},
|
||||
{"PrepareAsync", "", ""}};
|
||||
} async_prefixes[] = { { "Async", ", void* tag", ", tag" },
|
||||
{ "PrepareAsync", "", "" } };
|
||||
|
||||
if (is_public) {
|
||||
if (method->NoStreaming()) {
|
||||
@@ -402,8 +379,9 @@ void PrintHeaderClientMethod(grpc_generator::Printer *printer,
|
||||
*vars,
|
||||
"::grpc::Status $Method$(::grpc::ClientContext* context, "
|
||||
"const $Request$& request, $Response$* response) override;\n");
|
||||
for (size_t i = 0; i < sizeof(async_prefixes)/sizeof(async_prefixes[0]); i ++) {
|
||||
auto& async_prefix = async_prefixes[i];
|
||||
for (size_t i = 0; i < sizeof(async_prefixes) / sizeof(async_prefixes[0]);
|
||||
i++) {
|
||||
auto &async_prefix = async_prefixes[i];
|
||||
(*vars)["AsyncPrefix"] = async_prefix.prefix;
|
||||
printer->Print(
|
||||
*vars,
|
||||
@@ -431,8 +409,9 @@ void PrintHeaderClientMethod(grpc_generator::Printer *printer,
|
||||
"($Method$Raw(context, response));\n");
|
||||
printer->Outdent();
|
||||
printer->Print("}\n");
|
||||
for (size_t i = 0; i < sizeof(async_prefixes)/sizeof(async_prefixes[0]); i ++) {
|
||||
auto& async_prefix = async_prefixes[i];
|
||||
for (size_t i = 0; i < sizeof(async_prefixes) / sizeof(async_prefixes[0]);
|
||||
i++) {
|
||||
auto &async_prefix = async_prefixes[i];
|
||||
(*vars)["AsyncPrefix"] = async_prefix.prefix;
|
||||
(*vars)["AsyncMethodParams"] = async_prefix.method_params;
|
||||
(*vars)["AsyncRawArgs"] = async_prefix.raw_args;
|
||||
@@ -463,8 +442,9 @@ void PrintHeaderClientMethod(grpc_generator::Printer *printer,
|
||||
"($Method$Raw(context, request));\n");
|
||||
printer->Outdent();
|
||||
printer->Print("}\n");
|
||||
for (size_t i = 0; i < sizeof(async_prefixes)/sizeof(async_prefixes[0]); i ++) {
|
||||
auto& async_prefix = async_prefixes[i];
|
||||
for (size_t i = 0; i < sizeof(async_prefixes) / sizeof(async_prefixes[0]);
|
||||
i++) {
|
||||
auto &async_prefix = async_prefixes[i];
|
||||
(*vars)["AsyncPrefix"] = async_prefix.prefix;
|
||||
(*vars)["AsyncMethodParams"] = async_prefix.method_params;
|
||||
(*vars)["AsyncRawArgs"] = async_prefix.raw_args;
|
||||
@@ -494,8 +474,9 @@ void PrintHeaderClientMethod(grpc_generator::Printer *printer,
|
||||
"$Method$Raw(context));\n");
|
||||
printer->Outdent();
|
||||
printer->Print("}\n");
|
||||
for (size_t i = 0; i < sizeof(async_prefixes)/sizeof(async_prefixes[0]); i ++) {
|
||||
auto& async_prefix = async_prefixes[i];
|
||||
for (size_t i = 0; i < sizeof(async_prefixes) / sizeof(async_prefixes[0]);
|
||||
i++) {
|
||||
auto &async_prefix = async_prefixes[i];
|
||||
(*vars)["AsyncPrefix"] = async_prefix.prefix;
|
||||
(*vars)["AsyncMethodParams"] = async_prefix.method_params;
|
||||
(*vars)["AsyncRawArgs"] = async_prefix.raw_args;
|
||||
@@ -516,8 +497,9 @@ void PrintHeaderClientMethod(grpc_generator::Printer *printer,
|
||||
}
|
||||
} else {
|
||||
if (method->NoStreaming()) {
|
||||
for (size_t i = 0; i < sizeof(async_prefixes)/sizeof(async_prefixes[0]); i ++) {
|
||||
auto& async_prefix = async_prefixes[i];
|
||||
for (size_t i = 0; i < sizeof(async_prefixes) / sizeof(async_prefixes[0]);
|
||||
i++) {
|
||||
auto &async_prefix = async_prefixes[i];
|
||||
(*vars)["AsyncPrefix"] = async_prefix.prefix;
|
||||
printer->Print(
|
||||
*vars,
|
||||
@@ -531,8 +513,9 @@ void PrintHeaderClientMethod(grpc_generator::Printer *printer,
|
||||
"::grpc::ClientWriter< $Request$>* $Method$Raw("
|
||||
"::grpc::ClientContext* context, $Response$* response) "
|
||||
"override;\n");
|
||||
for (size_t i = 0; i < sizeof(async_prefixes)/sizeof(async_prefixes[0]); i ++) {
|
||||
auto& async_prefix = async_prefixes[i];
|
||||
for (size_t i = 0; i < sizeof(async_prefixes) / sizeof(async_prefixes[0]);
|
||||
i++) {
|
||||
auto &async_prefix = async_prefixes[i];
|
||||
(*vars)["AsyncPrefix"] = async_prefix.prefix;
|
||||
(*vars)["AsyncMethodParams"] = async_prefix.method_params;
|
||||
(*vars)["AsyncRawArgs"] = async_prefix.raw_args;
|
||||
@@ -547,8 +530,9 @@ void PrintHeaderClientMethod(grpc_generator::Printer *printer,
|
||||
"::grpc::ClientReader< $Response$>* $Method$Raw("
|
||||
"::grpc::ClientContext* context, const $Request$& request)"
|
||||
" override;\n");
|
||||
for (size_t i = 0; i < sizeof(async_prefixes)/sizeof(async_prefixes[0]); i ++) {
|
||||
auto& async_prefix = async_prefixes[i];
|
||||
for (size_t i = 0; i < sizeof(async_prefixes) / sizeof(async_prefixes[0]);
|
||||
i++) {
|
||||
auto &async_prefix = async_prefixes[i];
|
||||
(*vars)["AsyncPrefix"] = async_prefix.prefix;
|
||||
(*vars)["AsyncMethodParams"] = async_prefix.method_params;
|
||||
(*vars)["AsyncRawArgs"] = async_prefix.raw_args;
|
||||
@@ -562,8 +546,9 @@ void PrintHeaderClientMethod(grpc_generator::Printer *printer,
|
||||
printer->Print(*vars,
|
||||
"::grpc::ClientReaderWriter< $Request$, $Response$>* "
|
||||
"$Method$Raw(::grpc::ClientContext* context) override;\n");
|
||||
for (size_t i = 0; i < sizeof(async_prefixes)/sizeof(async_prefixes[0]); i ++) {
|
||||
auto& async_prefix = async_prefixes[i];
|
||||
for (size_t i = 0; i < sizeof(async_prefixes) / sizeof(async_prefixes[0]);
|
||||
i++) {
|
||||
auto &async_prefix = async_prefixes[i];
|
||||
(*vars)["AsyncPrefix"] = async_prefix.prefix;
|
||||
(*vars)["AsyncMethodParams"] = async_prefix.method_params;
|
||||
(*vars)["AsyncRawArgs"] = async_prefix.raw_args;
|
||||
@@ -577,7 +562,7 @@ void PrintHeaderClientMethod(grpc_generator::Printer *printer,
|
||||
}
|
||||
}
|
||||
|
||||
void PrintHeaderClientMethodData(grpc_generator::Printer *printer,
|
||||
static void PrintHeaderClientMethodData(grpc_generator::Printer *printer,
|
||||
const grpc_generator::Method *method,
|
||||
std::map<grpc::string, grpc::string> *vars) {
|
||||
(*vars)["Method"] = method->name();
|
||||
@@ -585,7 +570,7 @@ void PrintHeaderClientMethodData(grpc_generator::Printer *printer,
|
||||
"const ::grpc::internal::RpcMethod rpcmethod_$Method$_;\n");
|
||||
}
|
||||
|
||||
void PrintHeaderServerMethodSync(grpc_generator::Printer *printer,
|
||||
static void PrintHeaderServerMethodSync(grpc_generator::Printer *printer,
|
||||
const grpc_generator::Method *method,
|
||||
std::map<grpc::string, grpc::string> *vars) {
|
||||
(*vars)["Method"] = method->name();
|
||||
@@ -619,7 +604,7 @@ void PrintHeaderServerMethodSync(grpc_generator::Printer *printer,
|
||||
printer->Print(method->GetTrailingComments("//").c_str());
|
||||
}
|
||||
|
||||
void PrintHeaderServerMethodAsync(grpc_generator::Printer *printer,
|
||||
static void PrintHeaderServerMethodAsync(grpc_generator::Printer *printer,
|
||||
const grpc_generator::Method *method,
|
||||
std::map<grpc::string, grpc::string> *vars) {
|
||||
(*vars)["Method"] = method->name();
|
||||
@@ -630,7 +615,8 @@ void PrintHeaderServerMethodAsync(grpc_generator::Printer *printer,
|
||||
"class WithAsyncMethod_$Method$ : public BaseClass {\n");
|
||||
printer->Print(
|
||||
" private:\n"
|
||||
" void BaseClassMustBeDerivedFromService(const Service *service) {}\n");
|
||||
" void BaseClassMustBeDerivedFromService(const Service */*service*/) "
|
||||
"{}\n");
|
||||
printer->Print(" public:\n");
|
||||
printer->Indent();
|
||||
printer->Print(*vars,
|
||||
@@ -646,8 +632,8 @@ void PrintHeaderServerMethodAsync(grpc_generator::Printer *printer,
|
||||
*vars,
|
||||
"// disable synchronous version of this method\n"
|
||||
"::grpc::Status $Method$("
|
||||
"::grpc::ServerContext* context, const $Request$* request, "
|
||||
"$Response$* response) final override {\n"
|
||||
"::grpc::ServerContext* /*context*/, const $Request$* /*request*/, "
|
||||
"$Response$* /*response*/) final override {\n"
|
||||
" abort();\n"
|
||||
" return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, \"\");\n"
|
||||
"}\n");
|
||||
@@ -667,9 +653,9 @@ void PrintHeaderServerMethodAsync(grpc_generator::Printer *printer,
|
||||
*vars,
|
||||
"// disable synchronous version of this method\n"
|
||||
"::grpc::Status $Method$("
|
||||
"::grpc::ServerContext* context, "
|
||||
"::grpc::ServerReader< $Request$>* reader, "
|
||||
"$Response$* response) final override {\n"
|
||||
"::grpc::ServerContext* /*context*/, "
|
||||
"::grpc::ServerReader< $Request$>* /*reader*/, "
|
||||
"$Response$* /*response*/) final override {\n"
|
||||
" abort();\n"
|
||||
" return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, \"\");\n"
|
||||
"}\n");
|
||||
@@ -689,8 +675,8 @@ void PrintHeaderServerMethodAsync(grpc_generator::Printer *printer,
|
||||
*vars,
|
||||
"// disable synchronous version of this method\n"
|
||||
"::grpc::Status $Method$("
|
||||
"::grpc::ServerContext* context, const $Request$* request, "
|
||||
"::grpc::ServerWriter< $Response$>* writer) final override "
|
||||
"::grpc::ServerContext* /*context*/, const $Request$* /*request*/, "
|
||||
"::grpc::ServerWriter< $Response$>* /*writer*/) final override "
|
||||
"{\n"
|
||||
" abort();\n"
|
||||
" return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, \"\");\n"
|
||||
@@ -712,8 +698,8 @@ void PrintHeaderServerMethodAsync(grpc_generator::Printer *printer,
|
||||
*vars,
|
||||
"// disable synchronous version of this method\n"
|
||||
"::grpc::Status $Method$("
|
||||
"::grpc::ServerContext* context, "
|
||||
"::grpc::ServerReaderWriter< $Response$, $Request$>* stream) "
|
||||
"::grpc::ServerContext* /*context*/, "
|
||||
"::grpc::ServerReaderWriter< $Response$, $Request$>* /*stream*/) "
|
||||
"final override {\n"
|
||||
" abort();\n"
|
||||
" return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, \"\");\n"
|
||||
@@ -734,7 +720,7 @@ void PrintHeaderServerMethodAsync(grpc_generator::Printer *printer,
|
||||
printer->Print(*vars, "};\n");
|
||||
}
|
||||
|
||||
void PrintHeaderServerMethodStreamedUnary(
|
||||
static void PrintHeaderServerMethodStreamedUnary(
|
||||
grpc_generator::Printer *printer, const grpc_generator::Method *method,
|
||||
std::map<grpc::string, grpc::string> *vars) {
|
||||
(*vars)["Method"] = method->name();
|
||||
@@ -747,7 +733,7 @@ void PrintHeaderServerMethodStreamedUnary(
|
||||
"public BaseClass {\n");
|
||||
printer->Print(
|
||||
" private:\n"
|
||||
" void BaseClassMustBeDerivedFromService(const Service *service) "
|
||||
" void BaseClassMustBeDerivedFromService(const Service */*service*/) "
|
||||
"{}\n");
|
||||
printer->Print(" public:\n");
|
||||
printer->Indent();
|
||||
@@ -768,8 +754,8 @@ void PrintHeaderServerMethodStreamedUnary(
|
||||
*vars,
|
||||
"// disable regular version of this method\n"
|
||||
"::grpc::Status $Method$("
|
||||
"::grpc::ServerContext* context, const $Request$* request, "
|
||||
"$Response$* response) final override {\n"
|
||||
"::grpc::ServerContext* /*context*/, const $Request$* /*request*/, "
|
||||
"$Response$* /*response*/) final override {\n"
|
||||
" abort();\n"
|
||||
" return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, \"\");\n"
|
||||
"}\n");
|
||||
@@ -785,7 +771,7 @@ void PrintHeaderServerMethodStreamedUnary(
|
||||
}
|
||||
}
|
||||
|
||||
void PrintHeaderServerMethodSplitStreaming(
|
||||
static void PrintHeaderServerMethodSplitStreaming(
|
||||
grpc_generator::Printer *printer, const grpc_generator::Method *method,
|
||||
std::map<grpc::string, grpc::string> *vars) {
|
||||
(*vars)["Method"] = method->name();
|
||||
@@ -798,8 +784,8 @@ void PrintHeaderServerMethodSplitStreaming(
|
||||
"public BaseClass {\n");
|
||||
printer->Print(
|
||||
" private:\n"
|
||||
" void BaseClassMustBeDerivedFromService(const Service *service) "
|
||||
"{}\n");
|
||||
" void BaseClassMustBeDerivedFromService(const Service */*service*/) "
|
||||
"{ }\n");
|
||||
printer->Print(" public:\n");
|
||||
printer->Indent();
|
||||
printer->Print(
|
||||
@@ -820,8 +806,8 @@ void PrintHeaderServerMethodSplitStreaming(
|
||||
*vars,
|
||||
"// disable regular version of this method\n"
|
||||
"::grpc::Status $Method$("
|
||||
"::grpc::ServerContext* context, const $Request$* request, "
|
||||
"::grpc::ServerWriter< $Response$>* writer) final override "
|
||||
"::grpc::ServerContext* /*context*/, const $Request$* /*request*/, "
|
||||
"::grpc::ServerWriter< $Response$>* /*writer*/) final override "
|
||||
"{\n"
|
||||
" abort();\n"
|
||||
" return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, \"\");\n"
|
||||
@@ -838,7 +824,7 @@ void PrintHeaderServerMethodSplitStreaming(
|
||||
}
|
||||
}
|
||||
|
||||
void PrintHeaderServerMethodGeneric(
|
||||
static void PrintHeaderServerMethodGeneric(
|
||||
grpc_generator::Printer *printer, const grpc_generator::Method *method,
|
||||
std::map<grpc::string, grpc::string> *vars) {
|
||||
(*vars)["Method"] = method->name();
|
||||
@@ -849,7 +835,8 @@ void PrintHeaderServerMethodGeneric(
|
||||
"class WithGenericMethod_$Method$ : public BaseClass {\n");
|
||||
printer->Print(
|
||||
" private:\n"
|
||||
" void BaseClassMustBeDerivedFromService(const Service *service) {}\n");
|
||||
" void BaseClassMustBeDerivedFromService(const Service */*service*/) "
|
||||
"{}\n");
|
||||
printer->Print(" public:\n");
|
||||
printer->Indent();
|
||||
printer->Print(*vars,
|
||||
@@ -865,8 +852,8 @@ void PrintHeaderServerMethodGeneric(
|
||||
*vars,
|
||||
"// disable synchronous version of this method\n"
|
||||
"::grpc::Status $Method$("
|
||||
"::grpc::ServerContext* context, const $Request$* request, "
|
||||
"$Response$* response) final override {\n"
|
||||
"::grpc::ServerContext* /*context*/, const $Request$* /*request*/, "
|
||||
"$Response$* /*response*/) final override {\n"
|
||||
" abort();\n"
|
||||
" return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, \"\");\n"
|
||||
"}\n");
|
||||
@@ -875,9 +862,9 @@ void PrintHeaderServerMethodGeneric(
|
||||
*vars,
|
||||
"// disable synchronous version of this method\n"
|
||||
"::grpc::Status $Method$("
|
||||
"::grpc::ServerContext* context, "
|
||||
"::grpc::ServerReader< $Request$>* reader, "
|
||||
"$Response$* response) final override {\n"
|
||||
"::grpc::ServerContext* /*context*/, "
|
||||
"::grpc::ServerReader< $Request$>* /*reader*/, "
|
||||
"$Response$* /*response*/) final override {\n"
|
||||
" abort();\n"
|
||||
" return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, \"\");\n"
|
||||
"}\n");
|
||||
@@ -886,8 +873,8 @@ void PrintHeaderServerMethodGeneric(
|
||||
*vars,
|
||||
"// disable synchronous version of this method\n"
|
||||
"::grpc::Status $Method$("
|
||||
"::grpc::ServerContext* context, const $Request$* request, "
|
||||
"::grpc::ServerWriter< $Response$>* writer) final override "
|
||||
"::grpc::ServerContext* /*context*/, const $Request$* /*request*/, "
|
||||
"::grpc::ServerWriter< $Response$>* /*writer*/) final override "
|
||||
"{\n"
|
||||
" abort();\n"
|
||||
" return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, \"\");\n"
|
||||
@@ -897,8 +884,8 @@ void PrintHeaderServerMethodGeneric(
|
||||
*vars,
|
||||
"// disable synchronous version of this method\n"
|
||||
"::grpc::Status $Method$("
|
||||
"::grpc::ServerContext* context, "
|
||||
"::grpc::ServerReaderWriter< $Response$, $Request$>* stream) "
|
||||
"::grpc::ServerContext* /*context*/, "
|
||||
"::grpc::ServerReaderWriter< $Response$, $Request$>* /*stream*/) "
|
||||
"final override {\n"
|
||||
" abort();\n"
|
||||
" return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, \"\");\n"
|
||||
@@ -908,7 +895,7 @@ void PrintHeaderServerMethodGeneric(
|
||||
printer->Print(*vars, "};\n");
|
||||
}
|
||||
|
||||
void PrintHeaderService(grpc_generator::Printer *printer,
|
||||
static void PrintHeaderService(grpc_generator::Printer *printer,
|
||||
const grpc_generator::Service *service,
|
||||
std::map<grpc::string, grpc::string> *vars) {
|
||||
(*vars)["Service"] = service->name();
|
||||
@@ -1001,9 +988,7 @@ void PrintHeaderService(grpc_generator::Printer *printer,
|
||||
printer->Print(*vars, "WithAsyncMethod_$method_name$<");
|
||||
}
|
||||
printer->Print("Service");
|
||||
for (int i = 0; i < service->method_count(); ++i) {
|
||||
printer->Print(" >");
|
||||
}
|
||||
for (int i = 0; i < service->method_count(); ++i) { printer->Print(" >"); }
|
||||
printer->Print(" AsyncService;\n");
|
||||
|
||||
// Server side - Generic
|
||||
@@ -1028,9 +1013,7 @@ void PrintHeaderService(grpc_generator::Printer *printer,
|
||||
}
|
||||
printer->Print("Service");
|
||||
for (int i = 0; i < service->method_count(); ++i) {
|
||||
if (service->method(i)->NoStreaming()) {
|
||||
printer->Print(" >");
|
||||
}
|
||||
if (service->method(i)->NoStreaming()) { printer->Print(" >"); }
|
||||
}
|
||||
printer->Print(" StreamedUnaryService;\n");
|
||||
|
||||
@@ -1052,9 +1035,7 @@ void PrintHeaderService(grpc_generator::Printer *printer,
|
||||
printer->Print("Service");
|
||||
for (int i = 0; i < service->method_count(); ++i) {
|
||||
auto method = service->method(i);
|
||||
if (ServerOnlyStreaming(method.get())) {
|
||||
printer->Print(" >");
|
||||
}
|
||||
if (ServerOnlyStreaming(method.get())) { printer->Print(" >"); }
|
||||
}
|
||||
printer->Print(" SplitStreamedService;\n");
|
||||
|
||||
@@ -1085,6 +1066,8 @@ void PrintHeaderService(grpc_generator::Printer *printer,
|
||||
printer->Print(service->GetTrailingComments("//").c_str());
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
grpc::string GetHeaderServices(grpc_generator::File *file,
|
||||
const Parameters ¶ms) {
|
||||
grpc::string output;
|
||||
@@ -1095,9 +1078,7 @@ grpc::string GetHeaderServices(grpc_generator::File *file,
|
||||
// Package string is empty or ends with a dot. It is used to fully qualify
|
||||
// method names.
|
||||
vars["Package"] = file->package();
|
||||
if (!file->package().empty()) {
|
||||
vars["Package"].append(".");
|
||||
}
|
||||
if (!file->package().empty()) { vars["Package"].append("."); }
|
||||
|
||||
if (!params.services_namespace.empty()) {
|
||||
vars["services_namespace"] = params.services_namespace;
|
||||
@@ -1146,7 +1127,7 @@ grpc::string GetHeaderEpilogue(grpc_generator::File *file,
|
||||
}
|
||||
|
||||
grpc::string GetSourcePrologue(grpc_generator::File *file,
|
||||
const Parameters & /*params*/) {
|
||||
const Parameters ¶ms) {
|
||||
grpc::string output;
|
||||
{
|
||||
// Scope the output stream so it closes and finalizes output to the string.
|
||||
@@ -1155,7 +1136,7 @@ grpc::string GetSourcePrologue(grpc_generator::File *file,
|
||||
|
||||
vars["filename"] = file->filename();
|
||||
vars["filename_base"] = file->filename_without_ext();
|
||||
vars["message_header_ext"] = message_header_ext();
|
||||
vars["message_header_ext"] = params.message_header_extension;
|
||||
vars["service_header_ext"] = service_header_ext();
|
||||
|
||||
printer->Print(vars, "// Generated by the gRPC C++ plugin.\n");
|
||||
@@ -1179,14 +1160,15 @@ grpc::string GetSourceIncludes(grpc_generator::File *file,
|
||||
std::map<grpc::string, grpc::string> vars;
|
||||
|
||||
static const char *headers_strs[] = {
|
||||
"grpc++/impl/codegen/async_stream.h",
|
||||
"grpc++/impl/codegen/async_unary_call.h",
|
||||
"grpc++/impl/codegen/channel_interface.h",
|
||||
"grpc++/impl/codegen/client_unary_call.h",
|
||||
"grpc++/impl/codegen/method_handler_impl.h",
|
||||
"grpc++/impl/codegen/rpc_service_method.h",
|
||||
"grpc++/impl/codegen/service_type.h",
|
||||
"grpc++/impl/codegen/sync_stream.h"};
|
||||
"grpcpp/impl/codegen/async_stream.h",
|
||||
"grpcpp/impl/codegen/async_unary_call.h",
|
||||
"grpcpp/impl/codegen/channel_interface.h",
|
||||
"grpcpp/impl/codegen/client_unary_call.h",
|
||||
"grpcpp/impl/codegen/method_handler.h",
|
||||
"grpcpp/impl/codegen/rpc_service_method.h",
|
||||
"grpcpp/impl/codegen/service_type.h",
|
||||
"grpcpp/impl/codegen/sync_stream.h"
|
||||
};
|
||||
std::vector<grpc::string> headers(headers_strs, array_end(headers_strs));
|
||||
PrintIncludes(printer.get(), headers, params);
|
||||
|
||||
@@ -1204,7 +1186,10 @@ grpc::string GetSourceIncludes(grpc_generator::File *file,
|
||||
return output;
|
||||
}
|
||||
|
||||
void PrintSourceClientMethod(grpc_generator::Printer *printer,
|
||||
|
||||
namespace {
|
||||
|
||||
static void PrintSourceClientMethod(grpc_generator::Printer *printer,
|
||||
const grpc_generator::Method *method,
|
||||
std::map<grpc::string, grpc::string> *vars) {
|
||||
(*vars)["Method"] = method->name();
|
||||
@@ -1215,8 +1200,8 @@ void PrintSourceClientMethod(grpc_generator::Printer *printer,
|
||||
grpc::string start; // bool literal expressed as string
|
||||
grpc::string method_params; // extra arguments to method
|
||||
grpc::string create_args; // extra arguments to creator
|
||||
} async_prefixes[] = {{"Async", "true", ", void* tag", ", tag"},
|
||||
{"PrepareAsync", "false", "", ", nullptr"}};
|
||||
} async_prefixes[] = { { "Async", "true", ", void* tag", ", tag" },
|
||||
{ "PrepareAsync", "false", "", ", nullptr" } };
|
||||
if (method->NoStreaming()) {
|
||||
printer->Print(*vars,
|
||||
"::grpc::Status $ns$$Service$::Stub::$Method$("
|
||||
@@ -1226,8 +1211,9 @@ void PrintSourceClientMethod(grpc_generator::Printer *printer,
|
||||
" return ::grpc::internal::BlockingUnaryCall"
|
||||
"(channel_.get(), rpcmethod_$Method$_, "
|
||||
"context, request, response);\n}\n\n");
|
||||
for (size_t i = 0; i < sizeof(async_prefixes)/sizeof(async_prefixes[0]); i ++) {
|
||||
auto& async_prefix = async_prefixes[i];
|
||||
for (size_t i = 0; i < sizeof(async_prefixes) / sizeof(async_prefixes[0]);
|
||||
i++) {
|
||||
auto &async_prefix = async_prefixes[i];
|
||||
(*vars)["AsyncPrefix"] = async_prefix.prefix;
|
||||
(*vars)["AsyncStart"] = async_prefix.start;
|
||||
printer->Print(*vars,
|
||||
@@ -1257,8 +1243,9 @@ void PrintSourceClientMethod(grpc_generator::Printer *printer,
|
||||
"rpcmethod_$Method$_, "
|
||||
"context, response);\n"
|
||||
"}\n\n");
|
||||
for (size_t i = 0; i < sizeof(async_prefixes)/sizeof(async_prefixes[0]); i ++) {
|
||||
auto& async_prefix = async_prefixes[i];
|
||||
for (size_t i = 0; i < sizeof(async_prefixes) / sizeof(async_prefixes[0]);
|
||||
i++) {
|
||||
auto &async_prefix = async_prefixes[i];
|
||||
(*vars)["AsyncPrefix"] = async_prefix.prefix;
|
||||
(*vars)["AsyncStart"] = async_prefix.start;
|
||||
(*vars)["AsyncMethodParams"] = async_prefix.method_params;
|
||||
@@ -1289,8 +1276,9 @@ void PrintSourceClientMethod(grpc_generator::Printer *printer,
|
||||
"rpcmethod_$Method$_, "
|
||||
"context, request);\n"
|
||||
"}\n\n");
|
||||
for (size_t i = 0; i < sizeof(async_prefixes)/sizeof(async_prefixes[0]); i ++) {
|
||||
auto& async_prefix = async_prefixes[i];
|
||||
for (size_t i = 0; i < sizeof(async_prefixes) / sizeof(async_prefixes[0]);
|
||||
i++) {
|
||||
auto &async_prefix = async_prefixes[i];
|
||||
(*vars)["AsyncPrefix"] = async_prefix.prefix;
|
||||
(*vars)["AsyncStart"] = async_prefix.start;
|
||||
(*vars)["AsyncMethodParams"] = async_prefix.method_params;
|
||||
@@ -1321,8 +1309,9 @@ void PrintSourceClientMethod(grpc_generator::Printer *printer,
|
||||
"rpcmethod_$Method$_, "
|
||||
"context);\n"
|
||||
"}\n\n");
|
||||
for (size_t i = 0; i < sizeof(async_prefixes)/sizeof(async_prefixes[0]); i ++) {
|
||||
auto& async_prefix = async_prefixes[i];
|
||||
for (size_t i = 0; i < sizeof(async_prefixes) / sizeof(async_prefixes[0]);
|
||||
i++) {
|
||||
auto &async_prefix = async_prefixes[i];
|
||||
(*vars)["AsyncPrefix"] = async_prefix.prefix;
|
||||
(*vars)["AsyncStart"] = async_prefix.start;
|
||||
(*vars)["AsyncMethodParams"] = async_prefix.method_params;
|
||||
@@ -1344,20 +1333,18 @@ void PrintSourceClientMethod(grpc_generator::Printer *printer,
|
||||
}
|
||||
}
|
||||
|
||||
void PrintSourceServerMethod(grpc_generator::Printer *printer,
|
||||
static void PrintSourceServerMethod(grpc_generator::Printer *printer,
|
||||
const grpc_generator::Method *method,
|
||||
std::map<grpc::string, grpc::string> *vars) {
|
||||
(*vars)["Method"] = method->name();
|
||||
(*vars)["Request"] = method->input_type_name();
|
||||
(*vars)["Response"] = method->output_type_name();
|
||||
if (method->NoStreaming()) {
|
||||
printer->Print(*vars,
|
||||
"::grpc::Status $ns$$Service$::Service::$Method$("
|
||||
"::grpc::ServerContext* context, "
|
||||
"const $Request$* request, $Response$* response) {\n");
|
||||
printer->Print(" (void) context;\n");
|
||||
printer->Print(" (void) request;\n");
|
||||
printer->Print(" (void) response;\n");
|
||||
printer->Print(
|
||||
*vars,
|
||||
"::grpc::Status $ns$$Service$::Service::$Method$("
|
||||
"::grpc::ServerContext* /*context*/, "
|
||||
"const $Request$* /*request*/, $Response$* /*response*/) {\n");
|
||||
printer->Print(
|
||||
" return ::grpc::Status("
|
||||
"::grpc::StatusCode::UNIMPLEMENTED, \"\");\n");
|
||||
@@ -1365,12 +1352,9 @@ void PrintSourceServerMethod(grpc_generator::Printer *printer,
|
||||
} else if (ClientOnlyStreaming(method)) {
|
||||
printer->Print(*vars,
|
||||
"::grpc::Status $ns$$Service$::Service::$Method$("
|
||||
"::grpc::ServerContext* context, "
|
||||
"::grpc::ServerReader< $Request$>* reader, "
|
||||
"$Response$* response) {\n");
|
||||
printer->Print(" (void) context;\n");
|
||||
printer->Print(" (void) reader;\n");
|
||||
printer->Print(" (void) response;\n");
|
||||
"::grpc::ServerContext* /*context*/, "
|
||||
"::grpc::ServerReader< $Request$>* /*reader*/, "
|
||||
"$Response$* /*response*/) {\n");
|
||||
printer->Print(
|
||||
" return ::grpc::Status("
|
||||
"::grpc::StatusCode::UNIMPLEMENTED, \"\");\n");
|
||||
@@ -1378,12 +1362,9 @@ void PrintSourceServerMethod(grpc_generator::Printer *printer,
|
||||
} else if (ServerOnlyStreaming(method)) {
|
||||
printer->Print(*vars,
|
||||
"::grpc::Status $ns$$Service$::Service::$Method$("
|
||||
"::grpc::ServerContext* context, "
|
||||
"const $Request$* request, "
|
||||
"::grpc::ServerWriter< $Response$>* writer) {\n");
|
||||
printer->Print(" (void) context;\n");
|
||||
printer->Print(" (void) request;\n");
|
||||
printer->Print(" (void) writer;\n");
|
||||
"::grpc::ServerContext* /*context*/, "
|
||||
"const $Request$* /*request*/, "
|
||||
"::grpc::ServerWriter< $Response$>* /*writer*/) {\n");
|
||||
printer->Print(
|
||||
" return ::grpc::Status("
|
||||
"::grpc::StatusCode::UNIMPLEMENTED, \"\");\n");
|
||||
@@ -1391,11 +1372,9 @@ void PrintSourceServerMethod(grpc_generator::Printer *printer,
|
||||
} else if (method->BidiStreaming()) {
|
||||
printer->Print(*vars,
|
||||
"::grpc::Status $ns$$Service$::Service::$Method$("
|
||||
"::grpc::ServerContext* context, "
|
||||
"::grpc::ServerContext* /*context*/, "
|
||||
"::grpc::ServerReaderWriter< $Response$, $Request$>* "
|
||||
"stream) {\n");
|
||||
printer->Print(" (void) context;\n");
|
||||
printer->Print(" (void) stream;\n");
|
||||
"/*stream*/) {\n");
|
||||
printer->Print(
|
||||
" return ::grpc::Status("
|
||||
"::grpc::StatusCode::UNIMPLEMENTED, \"\");\n");
|
||||
@@ -1403,7 +1382,7 @@ void PrintSourceServerMethod(grpc_generator::Printer *printer,
|
||||
}
|
||||
}
|
||||
|
||||
void PrintSourceService(grpc_generator::Printer *printer,
|
||||
static void PrintSourceService(grpc_generator::Printer *printer,
|
||||
const grpc_generator::Service *service,
|
||||
std::map<grpc::string, grpc::string> *vars) {
|
||||
(*vars)["Service"] = service->name();
|
||||
@@ -1421,7 +1400,7 @@ void PrintSourceService(grpc_generator::Printer *printer,
|
||||
printer->Print(*vars,
|
||||
"std::unique_ptr< $ns$$Service$::Stub> $ns$$Service$::NewStub("
|
||||
"const std::shared_ptr< ::grpc::ChannelInterface>& channel, "
|
||||
"const ::grpc::StubOptions& options) {\n"
|
||||
"const ::grpc::StubOptions& /*options*/) {\n"
|
||||
" std::unique_ptr< $ns$$Service$::Stub> stub(new "
|
||||
"$ns$$Service$::Stub(channel));\n"
|
||||
" return stub;\n"
|
||||
@@ -1520,6 +1499,8 @@ void PrintSourceService(grpc_generator::Printer *printer,
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
grpc::string GetSourceServices(grpc_generator::File *file,
|
||||
const Parameters ¶ms) {
|
||||
grpc::string output;
|
||||
@@ -1530,9 +1511,7 @@ grpc::string GetSourceServices(grpc_generator::File *file,
|
||||
// Package string is empty or ends with a dot. It is used to fully qualify
|
||||
// method names.
|
||||
vars["Package"] = file->package();
|
||||
if (!file->package().empty()) {
|
||||
vars["Package"].append(".");
|
||||
}
|
||||
if (!file->package().empty()) { vars["Package"].append("."); }
|
||||
if (!params.services_namespace.empty()) {
|
||||
vars["ns"] = params.services_namespace + "::";
|
||||
vars["prefix"] = params.services_namespace;
|
||||
@@ -1567,9 +1546,8 @@ grpc::string GetSourceEpilogue(grpc_generator::File *file,
|
||||
return temp;
|
||||
}
|
||||
|
||||
// TODO(mmukhi): Make sure we need parameters or not.
|
||||
grpc::string GetMockPrologue(grpc_generator::File *file,
|
||||
const Parameters & /*params*/) {
|
||||
const Parameters ¶ms) {
|
||||
grpc::string output;
|
||||
{
|
||||
// Scope the output stream so it closes and finalizes output to the string.
|
||||
@@ -1578,7 +1556,7 @@ grpc::string GetMockPrologue(grpc_generator::File *file,
|
||||
|
||||
vars["filename"] = file->filename();
|
||||
vars["filename_base"] = file->filename_without_ext();
|
||||
vars["message_header_ext"] = message_header_ext();
|
||||
vars["message_header_ext"] = params.message_header_extension;
|
||||
vars["service_header_ext"] = service_header_ext();
|
||||
|
||||
printer->Print(vars, "// Generated by the gRPC C++ plugin.\n");
|
||||
@@ -1604,9 +1582,9 @@ grpc::string GetMockIncludes(grpc_generator::File *file,
|
||||
std::map<grpc::string, grpc::string> vars;
|
||||
|
||||
static const char *headers_strs[] = {
|
||||
"grpc++/impl/codegen/async_stream.h",
|
||||
"grpc++/impl/codegen/sync_stream.h",
|
||||
"gmock/gmock.h",
|
||||
"grpcpp/impl/codegen/async_stream.h",
|
||||
"grpcpp/impl/codegen/sync_stream.h",
|
||||
"gmock/gmock.h",
|
||||
};
|
||||
std::vector<grpc::string> headers(headers_strs, array_end(headers_strs));
|
||||
PrintIncludes(printer.get(), headers, params);
|
||||
@@ -1625,7 +1603,10 @@ grpc::string GetMockIncludes(grpc_generator::File *file,
|
||||
return output;
|
||||
}
|
||||
|
||||
void PrintMockClientMethods(grpc_generator::Printer *printer,
|
||||
|
||||
namespace {
|
||||
|
||||
static void PrintMockClientMethods(grpc_generator::Printer *printer,
|
||||
const grpc_generator::Method *method,
|
||||
std::map<grpc::string, grpc::string> *vars) {
|
||||
(*vars)["Method"] = method->name();
|
||||
@@ -1636,15 +1617,17 @@ void PrintMockClientMethods(grpc_generator::Printer *printer,
|
||||
grpc::string prefix;
|
||||
grpc::string method_params; // extra arguments to method
|
||||
int extra_method_param_count;
|
||||
} async_prefixes[] = {{"Async", ", void* tag", 1}, {"PrepareAsync", "", 0}};
|
||||
} async_prefixes[] = { { "Async", ", void* tag", 1 },
|
||||
{ "PrepareAsync", "", 0 } };
|
||||
|
||||
if (method->NoStreaming()) {
|
||||
printer->Print(
|
||||
*vars,
|
||||
"MOCK_METHOD3($Method$, ::grpc::Status(::grpc::ClientContext* context, "
|
||||
"const $Request$& request, $Response$* response));\n");
|
||||
for (size_t i = 0; i < sizeof(async_prefixes)/sizeof(async_prefixes[0]); i ++) {
|
||||
auto& async_prefix = async_prefixes[i];
|
||||
for (size_t i = 0; i < sizeof(async_prefixes) / sizeof(async_prefixes[0]);
|
||||
i++) {
|
||||
auto &async_prefix = async_prefixes[i];
|
||||
(*vars)["AsyncPrefix"] = async_prefix.prefix;
|
||||
printer->Print(
|
||||
*vars,
|
||||
@@ -1659,12 +1642,13 @@ void PrintMockClientMethods(grpc_generator::Printer *printer,
|
||||
"MOCK_METHOD2($Method$Raw, "
|
||||
"::grpc::ClientWriterInterface< $Request$>*"
|
||||
"(::grpc::ClientContext* context, $Response$* response));\n");
|
||||
for (size_t i = 0; i < sizeof(async_prefixes)/sizeof(async_prefixes[0]); i ++) {
|
||||
auto& async_prefix = async_prefixes[i];
|
||||
for (size_t i = 0; i < sizeof(async_prefixes) / sizeof(async_prefixes[0]);
|
||||
i++) {
|
||||
auto &async_prefix = async_prefixes[i];
|
||||
(*vars)["AsyncPrefix"] = async_prefix.prefix;
|
||||
(*vars)["AsyncMethodParams"] = async_prefix.method_params;
|
||||
(*vars)["MockArgs"] =
|
||||
flatbuffers::NumToString(3 + async_prefix.extra_method_param_count);
|
||||
flatbuffers::NumToString(3 + async_prefix.extra_method_param_count);
|
||||
printer->Print(*vars,
|
||||
"MOCK_METHOD$MockArgs$($AsyncPrefix$$Method$Raw, "
|
||||
"::grpc::ClientAsyncWriterInterface< $Request$>*"
|
||||
@@ -1677,8 +1661,9 @@ void PrintMockClientMethods(grpc_generator::Printer *printer,
|
||||
"MOCK_METHOD2($Method$Raw, "
|
||||
"::grpc::ClientReaderInterface< $Response$>*"
|
||||
"(::grpc::ClientContext* context, const $Request$& request));\n");
|
||||
for (size_t i = 0; i < sizeof(async_prefixes)/sizeof(async_prefixes[0]); i ++) {
|
||||
auto& async_prefix = async_prefixes[i];
|
||||
for (size_t i = 0; i < sizeof(async_prefixes) / sizeof(async_prefixes[0]);
|
||||
i++) {
|
||||
auto &async_prefix = async_prefixes[i];
|
||||
(*vars)["AsyncPrefix"] = async_prefix.prefix;
|
||||
(*vars)["AsyncMethodParams"] = async_prefix.method_params;
|
||||
(*vars)["MockArgs"] =
|
||||
@@ -1696,8 +1681,9 @@ void PrintMockClientMethods(grpc_generator::Printer *printer,
|
||||
"MOCK_METHOD1($Method$Raw, "
|
||||
"::grpc::ClientReaderWriterInterface< $Request$, $Response$>*"
|
||||
"(::grpc::ClientContext* context));\n");
|
||||
for (size_t i = 0; i < sizeof(async_prefixes)/sizeof(async_prefixes[0]); i ++) {
|
||||
auto& async_prefix = async_prefixes[i];
|
||||
for (size_t i = 0; i < sizeof(async_prefixes) / sizeof(async_prefixes[0]);
|
||||
i++) {
|
||||
auto &async_prefix = async_prefixes[i];
|
||||
(*vars)["AsyncPrefix"] = async_prefix.prefix;
|
||||
(*vars)["AsyncMethodParams"] = async_prefix.method_params;
|
||||
(*vars)["MockArgs"] =
|
||||
@@ -1712,7 +1698,7 @@ void PrintMockClientMethods(grpc_generator::Printer *printer,
|
||||
}
|
||||
}
|
||||
|
||||
void PrintMockService(grpc_generator::Printer *printer,
|
||||
static void PrintMockService(grpc_generator::Printer *printer,
|
||||
const grpc_generator::Service *service,
|
||||
std::map<grpc::string, grpc::string> *vars) {
|
||||
(*vars)["Service"] = service->name();
|
||||
@@ -1728,6 +1714,8 @@ void PrintMockService(grpc_generator::Printer *printer,
|
||||
printer->Print("};\n");
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
grpc::string GetMockServices(grpc_generator::File *file,
|
||||
const Parameters ¶ms) {
|
||||
grpc::string output;
|
||||
@@ -1738,9 +1726,7 @@ grpc::string GetMockServices(grpc_generator::File *file,
|
||||
// Package string is empty or ends with a dot. It is used to fully qualify
|
||||
// method names.
|
||||
vars["Package"] = file->package();
|
||||
if (!file->package().empty()) {
|
||||
vars["Package"].append(".");
|
||||
}
|
||||
if (!file->package().empty()) { vars["Package"].append("."); }
|
||||
|
||||
if (!params.services_namespace.empty()) {
|
||||
vars["services_namespace"] = params.services_namespace;
|
||||
|
||||
+4
-36
@@ -1,36 +1,3 @@
|
||||
/*
|
||||
*
|
||||
* Copyright 2015, Google Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are
|
||||
* met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above
|
||||
* copyright notice, this list of conditions and the following disclaimer
|
||||
* in the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
* * Neither the name of Google Inc. nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from
|
||||
* this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef GRPC_INTERNAL_COMPILER_CPP_GENERATOR_H
|
||||
#define GRPC_INTERNAL_COMPILER_CPP_GENERATOR_H
|
||||
|
||||
@@ -41,12 +8,11 @@
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include "src/compiler/config.h"
|
||||
#include "src/compiler/schema_interface.h"
|
||||
|
||||
#ifndef GRPC_CUSTOM_STRING
|
||||
#include <string>
|
||||
#define GRPC_CUSTOM_STRING std::string
|
||||
# include <string>
|
||||
# define GRPC_CUSTOM_STRING std::string
|
||||
#endif
|
||||
|
||||
namespace grpc {
|
||||
@@ -67,6 +33,8 @@ struct Parameters {
|
||||
grpc::string grpc_search_path;
|
||||
// Generate GMOCK code to facilitate unit testing.
|
||||
bool generate_mock_code;
|
||||
// By default, use "_generated.h"
|
||||
std::string message_header_extension;
|
||||
};
|
||||
|
||||
// Return the prologue of the generated header file.
|
||||
|
||||
+439
-378
@@ -1,47 +1,13 @@
|
||||
/*
|
||||
*
|
||||
* Copyright 2015, Google Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are
|
||||
* met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above
|
||||
* copyright notice, this list of conditions and the following disclaimer
|
||||
* in the documentation AN/or other materials provided with the
|
||||
* distribution.
|
||||
* * Neither the name of Google Inc. nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from
|
||||
* this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <map>
|
||||
#include <cctype>
|
||||
#include <sstream>
|
||||
|
||||
#include "src/compiler/go_generator.h"
|
||||
|
||||
template <class T>
|
||||
grpc::string as_string(T x) {
|
||||
std::ostringstream out;
|
||||
out << x;
|
||||
return out.str();
|
||||
#include <cctype>
|
||||
#include <map>
|
||||
#include <sstream>
|
||||
|
||||
template<class T> grpc::string as_string(T x) {
|
||||
std::ostringstream out;
|
||||
out << x;
|
||||
return out.str();
|
||||
}
|
||||
|
||||
inline bool ClientOnlyStreaming(const grpc_generator::Method *method) {
|
||||
@@ -53,394 +19,489 @@ inline bool ServerOnlyStreaming(const grpc_generator::Method *method) {
|
||||
}
|
||||
|
||||
namespace grpc_go_generator {
|
||||
namespace {
|
||||
|
||||
// Returns string with first letter to lowerCase
|
||||
grpc::string unexportName(grpc::string s) {
|
||||
if (s.empty())
|
||||
return s;
|
||||
s[0] = static_cast<char>(std::tolower(s[0]));
|
||||
return s;
|
||||
static grpc::string unexportName(grpc::string s) {
|
||||
if (s.empty()) return s;
|
||||
s[0] = static_cast<char>(std::tolower(s[0]));
|
||||
return s;
|
||||
}
|
||||
|
||||
// Returns string with first letter to uppercase
|
||||
grpc::string exportName(grpc::string s) {
|
||||
if (s.empty())
|
||||
return s;
|
||||
s[0] = static_cast<char>(std::toupper(s[0]));
|
||||
return s;
|
||||
static grpc::string exportName(grpc::string s) {
|
||||
if (s.empty()) return s;
|
||||
s[0] = static_cast<char>(std::toupper(s[0]));
|
||||
return s;
|
||||
}
|
||||
|
||||
static void GenerateError(grpc_generator::Printer *printer,
|
||||
std::map<grpc::string, grpc::string> vars,
|
||||
const bool multiple_return = true) {
|
||||
printer->Print(vars, "if $Error_Check$ {\n");
|
||||
printer->Indent();
|
||||
vars["Return"] = multiple_return ? "nil, err" : "err";
|
||||
printer->Print(vars, "return $Return$\n");
|
||||
printer->Outdent();
|
||||
printer->Print("}\n");
|
||||
}
|
||||
|
||||
// Generates imports for the service
|
||||
void GenerateImports(grpc_generator::File *file, grpc_generator::Printer *printer,
|
||||
static void GenerateImports(grpc_generator::File *file,
|
||||
grpc_generator::Printer *printer,
|
||||
std::map<grpc::string, grpc::string> vars) {
|
||||
vars["filename"] = file->filename();
|
||||
printer->Print("//Generated by gRPC Go plugin\n");
|
||||
printer->Print("//If you make any local changes, they will be lost\n");
|
||||
printer->Print(vars, "//source: $filename$\n\n");
|
||||
printer->Print(vars, "package $Package$\n\n");
|
||||
if (file->additional_headers() != "") {
|
||||
printer->Print(file->additional_headers().c_str());
|
||||
printer->Print("\n\n");
|
||||
}
|
||||
printer->Print("import (\n");
|
||||
printer->Indent();
|
||||
printer->Print(vars, "$context$ \"context\"\n");
|
||||
printer->Print(vars, "$grpc$ \"google.golang.org/grpc\"\n");
|
||||
printer->Outdent();
|
||||
printer->Print(")\n\n");
|
||||
vars["filename"] = file->filename();
|
||||
printer->Print("//Generated by gRPC Go plugin\n");
|
||||
printer->Print("//If you make any local changes, they will be lost\n");
|
||||
printer->Print(vars, "//source: $filename$\n\n");
|
||||
printer->Print(vars, "package $Package$\n\n");
|
||||
printer->Print("import (\n");
|
||||
printer->Indent();
|
||||
printer->Print(vars, "$context$ \"context\"\n");
|
||||
printer->Print("flatbuffers \"github.com/google/flatbuffers/go\"\n");
|
||||
printer->Print(vars, "$grpc$ \"google.golang.org/grpc\"\n");
|
||||
printer->Print("\"google.golang.org/grpc/codes\"\n");
|
||||
printer->Print("\"google.golang.org/grpc/status\"\n");
|
||||
printer->Outdent();
|
||||
printer->Print(")\n\n");
|
||||
}
|
||||
|
||||
// Generates Server method signature source
|
||||
void GenerateServerMethodSignature(const grpc_generator::Method *method, grpc_generator::Printer *printer,
|
||||
static void GenerateServerMethodSignature(const grpc_generator::Method *method,
|
||||
grpc_generator::Printer *printer,
|
||||
std::map<grpc::string, grpc::string> vars) {
|
||||
vars["Method"] = exportName(method->name());
|
||||
vars["Request"] = method->get_input_type_name();
|
||||
vars["Response"] = (vars["CustomMethodIO"] == "") ? method->get_output_type_name() : vars["CustomMethodIO"];
|
||||
if (method->NoStreaming()) {
|
||||
printer->Print(vars, "$Method$($context$.Context, *$Request$) (*$Response$, error)");
|
||||
} else if (ServerOnlyStreaming(method)) {
|
||||
printer->Print(vars, "$Method$(*$Request$, $Service$_$Method$Server) error");
|
||||
} else {
|
||||
printer->Print(vars, "$Method$($Service$_$Method$Server) error");
|
||||
}
|
||||
vars["Method"] = exportName(method->name());
|
||||
vars["Request"] = method->get_input_type_name();
|
||||
vars["Response"] = (vars["CustomMethodIO"] == "")
|
||||
? method->get_output_type_name()
|
||||
: vars["CustomMethodIO"];
|
||||
if (method->NoStreaming()) {
|
||||
printer->Print(
|
||||
vars,
|
||||
"$Method$($context$.Context, *$Request$) (*$Response$, error)$Ending$");
|
||||
} else if (ServerOnlyStreaming(method)) {
|
||||
printer->Print(
|
||||
vars, "$Method$(*$Request$, $Service$_$Method$Server) error$Ending$");
|
||||
} else {
|
||||
printer->Print(vars, "$Method$($Service$_$Method$Server) error$Ending$");
|
||||
}
|
||||
}
|
||||
|
||||
void GenerateServerMethod(const grpc_generator::Method *method, grpc_generator::Printer *printer,
|
||||
static void GenerateServerMethod(const grpc_generator::Method *method,
|
||||
grpc_generator::Printer *printer,
|
||||
std::map<grpc::string, grpc::string> vars) {
|
||||
vars["Method"] = exportName(method->name());
|
||||
vars["Request"] = method->get_input_type_name();
|
||||
vars["Response"] = (vars["CustomMethodIO"] == "") ? method->get_output_type_name() : vars["CustomMethodIO"];
|
||||
vars["FullMethodName"] = "/" + vars["ServicePrefix"] + "." + vars["Service"] + "/" + vars["Method"];
|
||||
vars["Handler"] = "_" + vars["Service"] + "_" + vars["Method"] + "_Handler";
|
||||
if (method->NoStreaming()) {
|
||||
printer->Print(vars, "func $Handler$(srv interface{}, ctx $context$.Context,\n\tdec func(interface{}) error, interceptor $grpc$.UnaryServerInterceptor) (interface{}, error) {\n");
|
||||
printer->Indent();
|
||||
printer->Print(vars, "in := new($Request$)\n");
|
||||
printer->Print("if err := dec(in); err != nil { return nil, err }\n");
|
||||
printer->Print(vars, "if interceptor == nil { return srv.($Service$Server).$Method$(ctx, in) }\n");
|
||||
printer->Print(vars, "info := &$grpc$.UnaryServerInfo{\n");
|
||||
printer->Indent();
|
||||
printer->Print("Server: srv,\n");
|
||||
printer->Print(vars, "FullMethod: \"$FullMethodName$\",\n");
|
||||
printer->Outdent();
|
||||
printer->Print("}\n\n");
|
||||
printer->Print(vars, "handler := func(ctx $context$.Context, req interface{}) (interface{}, error) {\n");
|
||||
printer->Indent();
|
||||
printer->Print(vars, "return srv.($Service$Server).$Method$(ctx, req.(* $Request$))\n");
|
||||
printer->Outdent();
|
||||
printer->Print("}\n");
|
||||
printer->Print("return interceptor(ctx, in, info, handler)\n");
|
||||
printer->Outdent();
|
||||
printer->Print("}\n\n");
|
||||
return;
|
||||
}
|
||||
vars["StreamType"] = vars["ServiceUnexported"] + vars["Method"] + "Server";
|
||||
printer->Print(vars, "func $Handler$(srv interface{}, stream $grpc$.ServerStream) error {\n");
|
||||
printer->Indent();
|
||||
if (ServerOnlyStreaming(method)) {
|
||||
printer->Print(vars, "m := new($Request$)\n");
|
||||
printer->Print(vars, "if err := stream.RecvMsg(m); err != nil { return err }\n");
|
||||
printer->Print(vars, "return srv.($Service$Server).$Method$(m, &$StreamType${stream})\n");
|
||||
} else {
|
||||
printer->Print(vars, "return srv.($Service$Server).$Method$(&$StreamType${stream})\n");
|
||||
}
|
||||
printer->Outdent();
|
||||
printer->Print("}\n\n");
|
||||
vars["Method"] = exportName(method->name());
|
||||
vars["Request"] = method->get_input_type_name();
|
||||
vars["Response"] = (vars["CustomMethodIO"] == "")
|
||||
? method->get_output_type_name()
|
||||
: vars["CustomMethodIO"];
|
||||
vars["FullMethodName"] =
|
||||
"/" + vars["ServicePrefix"] + vars["Service"] + "/" + vars["Method"];
|
||||
vars["Handler"] = "_" + vars["Service"] + "_" + vars["Method"] + "_Handler";
|
||||
if (method->NoStreaming()) {
|
||||
printer->Print(
|
||||
vars,
|
||||
"func $Handler$(srv interface{}, ctx $context$.Context,\n\tdec "
|
||||
"func(interface{}) error, interceptor $grpc$.UnaryServerInterceptor) "
|
||||
"(interface{}, error) {\n");
|
||||
printer->Indent();
|
||||
printer->Print(vars, "in := new($Request$)\n");
|
||||
vars["Error_Check"] = "err := dec(in); err != nil";
|
||||
GenerateError(printer, vars);
|
||||
printer->Print("if interceptor == nil {\n");
|
||||
printer->Indent();
|
||||
printer->Print(vars, "return srv.($Service$Server).$Method$(ctx, in)\n");
|
||||
printer->Outdent();
|
||||
printer->Print("}\n");
|
||||
printer->Print(vars, "info := &$grpc$.UnaryServerInfo{\n");
|
||||
printer->Indent();
|
||||
printer->Print("Server: srv,\n");
|
||||
printer->Print(vars, "FullMethod: \"$FullMethodName$\",\n");
|
||||
printer->Outdent();
|
||||
printer->Print("}\n");
|
||||
printer->Outdent();
|
||||
printer->Print("\n");
|
||||
printer->Indent();
|
||||
printer->Print(vars,
|
||||
"handler := func(ctx $context$.Context, req interface{}) "
|
||||
"(interface{}, error) {\n");
|
||||
printer->Indent();
|
||||
printer->Print(
|
||||
vars, "return srv.($Service$Server).$Method$(ctx, req.(*$Request$))\n");
|
||||
printer->Outdent();
|
||||
printer->Print("}\n");
|
||||
printer->Print("return interceptor(ctx, in, info, handler)\n");
|
||||
printer->Outdent();
|
||||
printer->Print("}\n");
|
||||
return;
|
||||
}
|
||||
vars["StreamType"] = vars["ServiceUnexported"] + vars["Method"] + "Server";
|
||||
printer->Print(
|
||||
vars,
|
||||
"func $Handler$(srv interface{}, stream $grpc$.ServerStream) error {\n");
|
||||
printer->Indent();
|
||||
if (ServerOnlyStreaming(method)) {
|
||||
printer->Print(vars, "m := new($Request$)\n");
|
||||
vars["Error_Check"] = "err := stream.RecvMsg(m); err != nil";
|
||||
GenerateError(printer, vars, false);
|
||||
printer->Print(
|
||||
vars,
|
||||
"return srv.($Service$Server).$Method$(m, &$StreamType${stream})\n");
|
||||
} else {
|
||||
printer->Print(
|
||||
vars, "return srv.($Service$Server).$Method$(&$StreamType${stream})\n");
|
||||
}
|
||||
printer->Outdent();
|
||||
printer->Print("}\n\n");
|
||||
|
||||
bool genSend = method->BidiStreaming() || ServerOnlyStreaming(method);
|
||||
bool genRecv = method->BidiStreaming() || ClientOnlyStreaming(method);
|
||||
bool genSendAndClose = ClientOnlyStreaming(method);
|
||||
bool genSend = method->BidiStreaming() || ServerOnlyStreaming(method);
|
||||
bool genRecv = method->BidiStreaming() || ClientOnlyStreaming(method);
|
||||
bool genSendAndClose = ClientOnlyStreaming(method);
|
||||
|
||||
printer->Print(vars, "type $Service$_$Method$Server interface { \n");
|
||||
printer->Indent();
|
||||
if (genSend) {
|
||||
printer->Print(vars, "Send(* $Response$) error\n");
|
||||
}
|
||||
if (genRecv) {
|
||||
printer->Print(vars, "Recv() (* $Request$, error)\n");
|
||||
}
|
||||
if (genSendAndClose) {
|
||||
printer->Print(vars, "SendAndClose(* $Response$) error\n");
|
||||
}
|
||||
printer->Print(vars, "$grpc$.ServerStream\n");
|
||||
printer->Outdent();
|
||||
printer->Print("}\n\n");
|
||||
printer->Print(vars, "type $Service$_$Method$Server interface {\n");
|
||||
printer->Indent();
|
||||
if (genSend) { printer->Print(vars, "Send(*$Response$) error\n"); }
|
||||
if (genRecv) { printer->Print(vars, "Recv() (*$Request$, error)\n"); }
|
||||
if (genSendAndClose) {
|
||||
printer->Print(vars, "SendAndClose(*$Response$) error\n");
|
||||
}
|
||||
printer->Print(vars, "$grpc$.ServerStream\n");
|
||||
printer->Outdent();
|
||||
printer->Print("}\n\n");
|
||||
|
||||
printer->Print(vars, "type $StreamType$ struct {\n");
|
||||
printer->Indent();
|
||||
printer->Print(vars, "$grpc$.ServerStream\n");
|
||||
printer->Outdent();
|
||||
printer->Print("}\n\n");
|
||||
|
||||
if (genSend) {
|
||||
printer->Print(vars, "func (x *$StreamType$) Send(m *$Response$) error {\n");
|
||||
printer->Indent();
|
||||
printer->Print("return x.ServerStream.SendMsg(m)\n");
|
||||
printer->Outdent();
|
||||
printer->Print("}\n\n");
|
||||
}
|
||||
if (genRecv) {
|
||||
printer->Print(vars, "func (x *$StreamType$) Recv() (*$Request$, error) {\n");
|
||||
printer->Indent();
|
||||
printer->Print(vars, "m := new($Request$)\n");
|
||||
printer->Print("if err := x.ServerStream.RecvMsg(m); err != nil { return nil, err }\n");
|
||||
printer->Print("return m, nil\n");
|
||||
printer->Outdent();
|
||||
printer->Print("}\n\n");
|
||||
}
|
||||
if (genSendAndClose) {
|
||||
printer->Print(vars, "func (x *$StreamType$) SendAndClose(m *$Response$) error {\n");
|
||||
printer->Indent();
|
||||
printer->Print("return x.ServerStream.SendMsg(m)\n");
|
||||
printer->Outdent();
|
||||
printer->Print("}\n\n");
|
||||
}
|
||||
printer->Print(vars, "type $StreamType$ struct {\n");
|
||||
printer->Indent();
|
||||
printer->Print(vars, "$grpc$.ServerStream\n");
|
||||
printer->Outdent();
|
||||
printer->Print("}\n\n");
|
||||
|
||||
if (genSend) {
|
||||
printer->Print(vars,
|
||||
"func (x *$StreamType$) Send(m *$Response$) error {\n");
|
||||
printer->Indent();
|
||||
printer->Print("return x.ServerStream.SendMsg(m)\n");
|
||||
printer->Outdent();
|
||||
printer->Print("}\n\n");
|
||||
}
|
||||
if (genRecv) {
|
||||
printer->Print(vars,
|
||||
"func (x *$StreamType$) Recv() (*$Request$, error) {\n");
|
||||
printer->Indent();
|
||||
printer->Print(vars, "m := new($Request$)\n");
|
||||
vars["Error_Check"] = "err := x.ServerStream.RecvMsg(m); err != nil";
|
||||
GenerateError(printer, vars);
|
||||
printer->Print("return m, nil\n");
|
||||
printer->Outdent();
|
||||
printer->Print("}\n\n");
|
||||
}
|
||||
if (genSendAndClose) {
|
||||
printer->Print(
|
||||
vars, "func (x *$StreamType$) SendAndClose(m *$Response$) error {\n");
|
||||
printer->Indent();
|
||||
printer->Print("return x.ServerStream.SendMsg(m)\n");
|
||||
printer->Outdent();
|
||||
printer->Print("}\n\n");
|
||||
}
|
||||
}
|
||||
|
||||
// Generates Client method signature source
|
||||
void GenerateClientMethodSignature(const grpc_generator::Method *method, grpc_generator::Printer *printer,
|
||||
static void GenerateClientMethodSignature(const grpc_generator::Method *method,
|
||||
grpc_generator::Printer *printer,
|
||||
std::map<grpc::string, grpc::string> vars) {
|
||||
vars["Method"] = exportName(method->name());
|
||||
vars["Request"] = ", in *" + ((vars["CustomMethodIO"] == "") ? method->get_input_type_name() : vars["CustomMethodIO"]);
|
||||
if (ClientOnlyStreaming(method) || method->BidiStreaming()) {
|
||||
vars["Request"] = "";
|
||||
}
|
||||
vars["Response"] = "* " + method->get_output_type_name();
|
||||
if (ClientOnlyStreaming(method) || method->BidiStreaming() || ServerOnlyStreaming(method)) {
|
||||
vars["Response"] = vars["Service"] + "_" + vars["Method"] + "Client" ;
|
||||
}
|
||||
printer->Print(vars, "$Method$(ctx $context$.Context$Request$, \n\topts... $grpc$.CallOption) ($Response$, error)");
|
||||
vars["Method"] = exportName(method->name());
|
||||
vars["Request"] =
|
||||
", in *" + ((vars["CustomMethodIO"] == "") ? method->get_input_type_name()
|
||||
: vars["CustomMethodIO"]);
|
||||
if (ClientOnlyStreaming(method) || method->BidiStreaming()) {
|
||||
vars["Request"] = "";
|
||||
}
|
||||
vars["Response"] = "*" + method->get_output_type_name();
|
||||
if (ClientOnlyStreaming(method) || method->BidiStreaming() ||
|
||||
ServerOnlyStreaming(method)) {
|
||||
vars["Response"] = vars["Service"] + "_" + vars["Method"] + "Client";
|
||||
}
|
||||
printer->Print(vars,
|
||||
"$Method$(ctx $context$.Context$Request$,\n\topts "
|
||||
"...$grpc$.CallOption) ($Response$, error)$Ending$");
|
||||
}
|
||||
|
||||
// Generates Client method source
|
||||
void GenerateClientMethod(const grpc_generator::Method *method, grpc_generator::Printer *printer,
|
||||
static void GenerateClientMethod(const grpc_generator::Method *method,
|
||||
grpc_generator::Printer *printer,
|
||||
std::map<grpc::string, grpc::string> vars) {
|
||||
printer->Print(vars, "func (c *$ServiceUnexported$Client) ");
|
||||
GenerateClientMethodSignature(method, printer, vars);
|
||||
printer->Print(" {\n");
|
||||
printer->Indent();
|
||||
vars["Method"] = exportName(method->name());
|
||||
vars["Request"] = (vars["CustomMethodIO"] == "") ? method->get_input_type_name() : vars["CustomMethodIO"];
|
||||
vars["Response"] = method->get_output_type_name();
|
||||
vars["FullMethodName"] = "/" + vars["ServicePrefix"] + "." + vars["Service"] + "/" + vars["Method"];
|
||||
if (method->NoStreaming()) {
|
||||
printer->Print(vars, "out := new($Response$)\n");
|
||||
printer->Print(vars, "err := $grpc$.Invoke(ctx, \"$FullMethodName$\", in, out, c.cc, opts...)\n");
|
||||
printer->Print("if err != nil { return nil, err }\n");
|
||||
printer->Print("return out, nil\n");
|
||||
printer->Outdent();
|
||||
printer->Print("}\n\n");
|
||||
return;
|
||||
}
|
||||
vars["StreamType"] = vars["ServiceUnexported"] + vars["Method"] + "Client";
|
||||
printer->Print(vars, "stream, err := $grpc$.NewClientStream(ctx, &$MethodDesc$, c.cc, \"$FullMethodName$\", opts...)\n");
|
||||
printer->Print("if err != nil { return nil, err }\n");
|
||||
printer->Print(vars, "func (c *$ServiceUnexported$Client) ");
|
||||
vars["Ending"] = " {\n";
|
||||
GenerateClientMethodSignature(method, printer, vars);
|
||||
printer->Indent();
|
||||
vars["Method"] = exportName(method->name());
|
||||
vars["Request"] = (vars["CustomMethodIO"] == "")
|
||||
? method->get_input_type_name()
|
||||
: vars["CustomMethodIO"];
|
||||
vars["Response"] = method->get_output_type_name();
|
||||
vars["FullMethodName"] =
|
||||
"/" + vars["ServicePrefix"] + vars["Service"] + "/" + vars["Method"];
|
||||
if (method->NoStreaming()) {
|
||||
printer->Print(vars, "out := new($Response$)\n");
|
||||
printer->Print(
|
||||
vars,
|
||||
"err := c.cc.Invoke(ctx, \"$FullMethodName$\", in, out, opts...)\n");
|
||||
vars["Error_Check"] = "err != nil";
|
||||
GenerateError(printer, vars);
|
||||
printer->Print("return out, nil\n");
|
||||
printer->Outdent();
|
||||
printer->Print("}\n\n");
|
||||
return;
|
||||
}
|
||||
vars["StreamType"] = vars["ServiceUnexported"] + vars["Method"] + "Client";
|
||||
printer->Print(vars,
|
||||
"stream, err := c.cc.NewStream(ctx, &$MethodDesc$, "
|
||||
"\"$FullMethodName$\", opts...)\n");
|
||||
vars["Error_Check"] = "err != nil";
|
||||
GenerateError(printer, vars);
|
||||
|
||||
printer->Print(vars, "x := &$StreamType${stream}\n");
|
||||
if (ServerOnlyStreaming(method)) {
|
||||
printer->Print("if err := x.ClientStream.SendMsg(in); err != nil { return nil, err }\n");
|
||||
printer->Print("if err := x.ClientStream.CloseSend(); err != nil { return nil, err }\n");
|
||||
}
|
||||
printer->Print("return x,nil\n");
|
||||
printer->Outdent();
|
||||
printer->Print("}\n\n");
|
||||
printer->Print(vars, "x := &$StreamType${stream}\n");
|
||||
if (ServerOnlyStreaming(method)) {
|
||||
vars["Error_Check"] = "err := x.ClientStream.SendMsg(in); err != nil";
|
||||
GenerateError(printer, vars);
|
||||
vars["Error_Check"] = "err := x.ClientStream.CloseSend(); err != nil";
|
||||
GenerateError(printer, vars);
|
||||
}
|
||||
printer->Print("return x, nil\n");
|
||||
printer->Outdent();
|
||||
printer->Print("}\n\n");
|
||||
|
||||
bool genSend = method->BidiStreaming() || ClientOnlyStreaming(method);
|
||||
bool genRecv = method->BidiStreaming() || ServerOnlyStreaming(method);
|
||||
bool genCloseAndRecv = ClientOnlyStreaming(method);
|
||||
bool genSend = method->BidiStreaming() || ClientOnlyStreaming(method);
|
||||
bool genRecv = method->BidiStreaming() || ServerOnlyStreaming(method);
|
||||
bool genCloseAndRecv = ClientOnlyStreaming(method);
|
||||
|
||||
//Stream interface
|
||||
printer->Print(vars, "type $Service$_$Method$Client interface {\n");
|
||||
printer->Indent();
|
||||
if (genSend) {
|
||||
printer->Print(vars, "Send(*$Request$) error\n");
|
||||
}
|
||||
if (genRecv) {
|
||||
printer->Print(vars, "Recv() (*$Response$, error)\n");
|
||||
}
|
||||
if (genCloseAndRecv) {
|
||||
printer->Print(vars, "CloseAndRecv() (*$Response$, error)\n");
|
||||
}
|
||||
printer->Print(vars, "$grpc$.ClientStream\n");
|
||||
printer->Outdent();
|
||||
printer->Print("}\n\n");
|
||||
// Stream interface
|
||||
printer->Print(vars, "type $Service$_$Method$Client interface {\n");
|
||||
printer->Indent();
|
||||
if (genSend) { printer->Print(vars, "Send(*$Request$) error\n"); }
|
||||
if (genRecv) { printer->Print(vars, "Recv() (*$Response$, error)\n"); }
|
||||
if (genCloseAndRecv) {
|
||||
printer->Print(vars, "CloseAndRecv() (*$Response$, error)\n");
|
||||
}
|
||||
printer->Print(vars, "$grpc$.ClientStream\n");
|
||||
printer->Outdent();
|
||||
printer->Print("}\n\n");
|
||||
|
||||
//Stream Client
|
||||
printer->Print(vars, "type $StreamType$ struct{\n");
|
||||
printer->Indent();
|
||||
printer->Print(vars, "$grpc$.ClientStream\n");
|
||||
printer->Outdent();
|
||||
printer->Print("}\n\n");
|
||||
// Stream Client
|
||||
printer->Print(vars, "type $StreamType$ struct {\n");
|
||||
printer->Indent();
|
||||
printer->Print(vars, "$grpc$.ClientStream\n");
|
||||
printer->Outdent();
|
||||
printer->Print("}\n\n");
|
||||
|
||||
if (genSend) {
|
||||
printer->Print(vars, "func (x *$StreamType$) Send(m *$Request$) error {\n");
|
||||
printer->Indent();
|
||||
printer->Print("return x.ClientStream.SendMsg(m)\n");
|
||||
printer->Outdent();
|
||||
printer->Print("}\n\n");
|
||||
}
|
||||
if (genSend) {
|
||||
printer->Print(vars, "func (x *$StreamType$) Send(m *$Request$) error {\n");
|
||||
printer->Indent();
|
||||
printer->Print("return x.ClientStream.SendMsg(m)\n");
|
||||
printer->Outdent();
|
||||
printer->Print("}\n\n");
|
||||
}
|
||||
|
||||
if (genRecv) {
|
||||
printer->Print(vars, "func (x *$StreamType$) Recv() (*$Response$, error) {\n");
|
||||
printer->Indent();
|
||||
printer->Print(vars, "m := new($Response$)\n");
|
||||
printer->Print("if err := x.ClientStream.RecvMsg(m); err != nil { return nil, err }\n");
|
||||
printer->Print("return m, nil\n");
|
||||
printer->Outdent();
|
||||
printer->Print("}\n\n");
|
||||
}
|
||||
if (genRecv) {
|
||||
printer->Print(vars,
|
||||
"func (x *$StreamType$) Recv() (*$Response$, error) {\n");
|
||||
printer->Indent();
|
||||
printer->Print(vars, "m := new($Response$)\n");
|
||||
vars["Error_Check"] = "err := x.ClientStream.RecvMsg(m); err != nil";
|
||||
GenerateError(printer, vars);
|
||||
printer->Print("return m, nil\n");
|
||||
printer->Outdent();
|
||||
printer->Print("}\n\n");
|
||||
}
|
||||
|
||||
if (genCloseAndRecv) {
|
||||
printer->Print(vars, "func (x *$StreamType$) CloseAndRecv() (*$Response$, error) {\n");
|
||||
printer->Indent();
|
||||
printer->Print("if err := x.ClientStream.CloseSend(); err != nil { return nil, err }\n");
|
||||
printer->Print(vars, "m := new ($Response$)\n");
|
||||
printer->Print("if err := x.ClientStream.RecvMsg(m); err != nil { return nil, err }\n");
|
||||
printer->Print("return m, nil\n");
|
||||
printer->Outdent();
|
||||
printer->Print("}\n\n");
|
||||
}
|
||||
if (genCloseAndRecv) {
|
||||
printer->Print(
|
||||
vars, "func (x *$StreamType$) CloseAndRecv() (*$Response$, error) {\n");
|
||||
printer->Indent();
|
||||
vars["Error_Check"] = "err := x.ClientStream.CloseSend(); err != nil";
|
||||
GenerateError(printer, vars);
|
||||
printer->Print(vars, "m := new($Response$)\n");
|
||||
vars["Error_Check"] = "err := x.ClientStream.RecvMsg(m); err != nil";
|
||||
GenerateError(printer, vars);
|
||||
printer->Print("return m, nil\n");
|
||||
printer->Outdent();
|
||||
printer->Print("}\n\n");
|
||||
}
|
||||
}
|
||||
|
||||
// Generates client API for the service
|
||||
void GenerateService(const grpc_generator::Service *service, grpc_generator::Printer* printer,
|
||||
void GenerateService(const grpc_generator::Service *service,
|
||||
grpc_generator::Printer *printer,
|
||||
std::map<grpc::string, grpc::string> vars) {
|
||||
vars["Service"] = exportName(service->name());
|
||||
// Client Interface
|
||||
printer->Print(vars, "// Client API for $Service$ service\n");
|
||||
printer->Print(vars, "type $Service$Client interface{\n");
|
||||
printer->Indent();
|
||||
for (int i = 0; i < service->method_count(); i++) {
|
||||
GenerateClientMethodSignature(service->method(i).get(), printer, vars);
|
||||
printer->Print("\n");
|
||||
}
|
||||
printer->Outdent();
|
||||
printer->Print("}\n\n");
|
||||
vars["Service"] = exportName(service->name());
|
||||
// Client Interface
|
||||
printer->Print(vars, "// Client API for $Service$ service\n");
|
||||
printer->Print(vars, "type $Service$Client interface {\n");
|
||||
printer->Indent();
|
||||
vars["Ending"] = "\n";
|
||||
for (int i = 0; i < service->method_count(); i++) {
|
||||
GenerateClientMethodSignature(service->method(i).get(), printer, vars);
|
||||
}
|
||||
printer->Outdent();
|
||||
printer->Print("}\n\n");
|
||||
|
||||
// Client structure
|
||||
vars["ServiceUnexported"] = unexportName(vars["Service"]);
|
||||
printer->Print(vars, "type $ServiceUnexported$Client struct {\n");
|
||||
printer->Indent();
|
||||
printer->Print(vars, "cc *$grpc$.ClientConn\n");
|
||||
printer->Outdent();
|
||||
printer->Print("}\n\n");
|
||||
// Client structure
|
||||
vars["ServiceUnexported"] = unexportName(vars["Service"]);
|
||||
printer->Print(vars, "type $ServiceUnexported$Client struct {\n");
|
||||
printer->Indent();
|
||||
printer->Print(vars, "cc $grpc$.ClientConnInterface\n");
|
||||
printer->Outdent();
|
||||
printer->Print("}\n\n");
|
||||
|
||||
// NewClient
|
||||
printer->Print(vars, "func New$Service$Client(cc *$grpc$.ClientConn) $Service$Client {\n");
|
||||
printer->Indent();
|
||||
printer->Print(vars, "return &$ServiceUnexported$Client{cc}");
|
||||
printer->Outdent();
|
||||
printer->Print("\n}\n\n");
|
||||
// NewClient
|
||||
printer->Print(vars,
|
||||
"func New$Service$Client(cc $grpc$.ClientConnInterface) "
|
||||
"$Service$Client {\n");
|
||||
printer->Indent();
|
||||
printer->Print(vars, "return &$ServiceUnexported$Client{cc}");
|
||||
printer->Outdent();
|
||||
printer->Print("\n}\n\n");
|
||||
|
||||
int unary_methods = 0, streaming_methods = 0;
|
||||
vars["ServiceDesc"] = "_" + vars["Service"] + "_serviceDesc";
|
||||
for (int i = 0; i < service->method_count(); i++) {
|
||||
auto method = service->method(i);
|
||||
if (method->NoStreaming()) {
|
||||
vars["MethodDesc"] = vars["ServiceDesc"] + ".Method[" + as_string(unary_methods) + "]";
|
||||
unary_methods++;
|
||||
} else {
|
||||
vars["MethodDesc"] = vars["ServiceDesc"] + ".Streams[" + as_string(streaming_methods) + "]";
|
||||
streaming_methods++;
|
||||
}
|
||||
GenerateClientMethod(method.get(), printer, vars);
|
||||
}
|
||||
int unary_methods = 0, streaming_methods = 0;
|
||||
vars["ServiceDesc"] = "_" + vars["Service"] + "_serviceDesc";
|
||||
for (int i = 0; i < service->method_count(); i++) {
|
||||
auto method = service->method(i);
|
||||
if (method->NoStreaming()) {
|
||||
vars["MethodDesc"] =
|
||||
vars["ServiceDesc"] + ".Method[" + as_string(unary_methods) + "]";
|
||||
unary_methods++;
|
||||
} else {
|
||||
vars["MethodDesc"] = vars["ServiceDesc"] + ".Streams[" +
|
||||
as_string(streaming_methods) + "]";
|
||||
streaming_methods++;
|
||||
}
|
||||
GenerateClientMethod(method.get(), printer, vars);
|
||||
}
|
||||
|
||||
//Server Interface
|
||||
printer->Print(vars, "// Server API for $Service$ service\n");
|
||||
printer->Print(vars, "type $Service$Server interface {\n");
|
||||
printer->Indent();
|
||||
for (int i = 0; i < service->method_count(); i++) {
|
||||
GenerateServerMethodSignature(service->method(i).get(), printer, vars);
|
||||
printer->Print("\n");
|
||||
}
|
||||
printer->Outdent();
|
||||
printer->Print("}\n\n");
|
||||
// Server Interface
|
||||
printer->Print(vars, "// Server API for $Service$ service\n");
|
||||
printer->Print(vars, "type $Service$Server interface {\n");
|
||||
printer->Indent();
|
||||
vars["Ending"] = "\n";
|
||||
for (int i = 0; i < service->method_count(); i++) {
|
||||
GenerateServerMethodSignature(service->method(i).get(), printer, vars);
|
||||
}
|
||||
printer->Print(vars, "mustEmbedUnimplemented$Service$Server()\n");
|
||||
printer->Outdent();
|
||||
printer->Print("}\n\n");
|
||||
|
||||
// Server registration.
|
||||
printer->Print(vars, "func Register$Service$Server(s *$grpc$.Server, srv $Service$Server) {\n");
|
||||
printer->Indent();
|
||||
printer->Print(vars, "s.RegisterService(&$ServiceDesc$, srv)\n");
|
||||
printer->Outdent();
|
||||
printer->Print("}\n\n");
|
||||
printer->Print(vars, "type Unimplemented$Service$Server struct {\n");
|
||||
printer->Print("}\n\n");
|
||||
|
||||
for (int i = 0; i < service->method_count(); i++) {
|
||||
GenerateServerMethod(service->method(i).get(), printer, vars);
|
||||
printer->Print("\n");
|
||||
}
|
||||
vars["Ending"] = " {\n";
|
||||
for (int i = 0; i < service->method_count(); i++) {
|
||||
auto method = service->method(i);
|
||||
vars["Method"] = exportName(method->name());
|
||||
vars["Nil"] = method->NoStreaming() ? "nil, " : "";
|
||||
printer->Print(vars, "func (Unimplemented$Service$Server) ");
|
||||
GenerateServerMethodSignature(method.get(), printer, vars);
|
||||
printer->Indent();
|
||||
printer->Print(vars,
|
||||
"return $Nil$status.Errorf(codes.Unimplemented, \"method "
|
||||
"$Method$ not implemented\")\n");
|
||||
printer->Outdent();
|
||||
printer->Print("}\n");
|
||||
printer->Print("\n");
|
||||
}
|
||||
|
||||
printer->Print(vars,
|
||||
"func (Unimplemented$Service$Server) "
|
||||
"mustEmbedUnimplemented$Service$Server() {}");
|
||||
printer->Print("\n\n");
|
||||
|
||||
//Service Descriptor
|
||||
printer->Print(vars, "var $ServiceDesc$ = $grpc$.ServiceDesc{\n");
|
||||
printer->Indent();
|
||||
printer->Print(vars, "ServiceName: \"$ServicePrefix$.$Service$\",\n");
|
||||
printer->Print(vars, "HandlerType: (*$Service$Server)(nil),\n");
|
||||
printer->Print(vars, "Methods: []$grpc$.MethodDesc{\n");
|
||||
printer->Indent();
|
||||
for (int i = 0; i < service->method_count(); i++) {
|
||||
auto method = service->method(i);
|
||||
vars["Method"] = method->name();
|
||||
vars["Handler"] = "_" + vars["Service"] + "_" + vars["Method"] + "_Handler";
|
||||
if (method->NoStreaming()) {
|
||||
printer->Print("{\n");
|
||||
printer->Indent();
|
||||
printer->Print(vars, "MethodName: \"$Method$\",\n");
|
||||
printer->Print(vars, "Handler: $Handler$, \n");
|
||||
printer->Outdent();
|
||||
printer->Print("},\n");
|
||||
}
|
||||
}
|
||||
printer->Outdent();
|
||||
printer->Print("},\n");
|
||||
printer->Print(vars, "Streams: []$grpc$.StreamDesc{\n");
|
||||
printer->Indent();
|
||||
for (int i = 0; i < service->method_count(); i++) {
|
||||
auto method = service->method(i);
|
||||
vars["Method"] = method->name();
|
||||
vars["Handler"] = "_" + vars["Service"] + "_" + vars["Method"] + "_Handler";
|
||||
if (!method->NoStreaming()) {
|
||||
printer->Print("{\n");
|
||||
printer->Indent();
|
||||
printer->Print(vars, "StreamName: \"$Method$\",\n");
|
||||
printer->Print(vars, "Handler: $Handler$, \n");
|
||||
if (ClientOnlyStreaming(method.get())) {
|
||||
printer->Print("ClientStreams: true,\n");
|
||||
} else if (ServerOnlyStreaming(method.get())) {
|
||||
printer->Print("ServerStreams: true,\n");
|
||||
} else {
|
||||
printer->Print("ServerStreams: true,\n");
|
||||
printer->Print("ClientStreams: true,\n");
|
||||
}
|
||||
printer->Outdent();
|
||||
printer->Print("},\n");
|
||||
}
|
||||
}
|
||||
printer->Outdent();
|
||||
printer->Print("},\n");
|
||||
printer->Outdent();
|
||||
printer->Print("}\n\n");
|
||||
printer->Print(vars, "type Unsafe$Service$Server interface {\n");
|
||||
printer->Indent();
|
||||
printer->Print(vars, "mustEmbedUnimplemented$Service$Server()\n");
|
||||
printer->Outdent();
|
||||
printer->Print("}\n\n");
|
||||
// Server registration.
|
||||
printer->Print(vars,
|
||||
"func Register$Service$Server(s $grpc$.ServiceRegistrar, srv "
|
||||
"$Service$Server) {\n");
|
||||
printer->Indent();
|
||||
printer->Print(vars, "s.RegisterService(&$ServiceDesc$, srv)\n");
|
||||
printer->Outdent();
|
||||
printer->Print("}\n\n");
|
||||
|
||||
for (int i = 0; i < service->method_count(); i++) {
|
||||
GenerateServerMethod(service->method(i).get(), printer, vars);
|
||||
}
|
||||
|
||||
// Service Descriptor
|
||||
printer->Print(vars, "var $ServiceDesc$ = $grpc$.ServiceDesc{\n");
|
||||
printer->Indent();
|
||||
printer->Print(vars, "ServiceName: \"$ServicePrefix$$Service$\",\n");
|
||||
printer->Print(vars, "HandlerType: (*$Service$Server)(nil),\n");
|
||||
printer->Print(vars, "Methods: []$grpc$.MethodDesc{\n");
|
||||
printer->Indent();
|
||||
for (int i = 0; i < service->method_count(); i++) {
|
||||
auto method = service->method(i);
|
||||
vars["Method"] = exportName(method->name());
|
||||
vars["Handler"] = "_" + vars["Service"] + "_" + vars["Method"] + "_Handler";
|
||||
if (method->NoStreaming()) {
|
||||
printer->Print("{\n");
|
||||
printer->Indent();
|
||||
printer->Print(vars, "MethodName: \"$Method$\",\n");
|
||||
printer->Print(vars, "Handler: $Handler$,\n");
|
||||
printer->Outdent();
|
||||
printer->Print("},\n");
|
||||
}
|
||||
}
|
||||
printer->Outdent();
|
||||
printer->Print("},\n");
|
||||
printer->Print(vars, "Streams: []$grpc$.StreamDesc{\n");
|
||||
printer->Indent();
|
||||
for (int i = 0; i < service->method_count(); i++) {
|
||||
auto method = service->method(i);
|
||||
vars["Method"] = exportName(method->name());
|
||||
vars["Handler"] = "_" + vars["Service"] + "_" + vars["Method"] + "_Handler";
|
||||
if (!method->NoStreaming()) {
|
||||
printer->Print("{\n");
|
||||
printer->Indent();
|
||||
printer->Print(vars, "StreamName: \"$Method$\",\n");
|
||||
printer->Print(vars, "Handler: $Handler$,\n");
|
||||
if (ClientOnlyStreaming(method.get())) {
|
||||
printer->Print("ClientStreams: true,\n");
|
||||
} else if (ServerOnlyStreaming(method.get())) {
|
||||
printer->Print("ServerStreams: true,\n");
|
||||
} else {
|
||||
printer->Print("ServerStreams: true,\n");
|
||||
printer->Print("ClientStreams: true,\n");
|
||||
}
|
||||
printer->Outdent();
|
||||
printer->Print("},\n");
|
||||
}
|
||||
}
|
||||
printer->Outdent();
|
||||
printer->Print("},\n");
|
||||
printer->Outdent();
|
||||
printer->Print("}\n");
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
// Returns source for the service
|
||||
grpc::string GenerateServiceSource(grpc_generator::File *file,
|
||||
const grpc_generator::Service *service,
|
||||
grpc_go_generator::Parameters *parameters) {
|
||||
grpc::string out;
|
||||
auto p = file->CreatePrinter(&out);
|
||||
auto printer = p.get();
|
||||
std::map<grpc::string, grpc::string> vars;
|
||||
vars["Package"] = parameters->package_name;
|
||||
vars["ServicePrefix"] = parameters->service_prefix;
|
||||
vars["grpc"] = "grpc";
|
||||
vars["context"] = "context";
|
||||
GenerateImports(file, printer, vars);
|
||||
if (parameters->custom_method_io_type != "") {
|
||||
vars["CustomMethodIO"] = parameters->custom_method_io_type;
|
||||
}
|
||||
GenerateService(service, printer, vars);
|
||||
return out;
|
||||
grpc::string out;
|
||||
auto p = file->CreatePrinter(&out, '\t');
|
||||
p->SetIndentationSize(1);
|
||||
auto printer = p.get();
|
||||
std::map<grpc::string, grpc::string> vars;
|
||||
vars["Package"] = parameters->package_name;
|
||||
vars["ServicePrefix"] = parameters->service_prefix;
|
||||
if (!parameters->service_prefix.empty()) vars["ServicePrefix"].append(".");
|
||||
vars["grpc"] = "grpc";
|
||||
vars["context"] = "context";
|
||||
GenerateImports(file, printer, vars);
|
||||
if (parameters->custom_method_io_type != "") {
|
||||
vars["CustomMethodIO"] = parameters->custom_method_io_type;
|
||||
}
|
||||
GenerateService(service, printer, vars);
|
||||
return out;
|
||||
}
|
||||
}// Namespace grpc_go_generator
|
||||
} // Namespace grpc_go_generator
|
||||
|
||||
+8
-39
@@ -1,40 +1,8 @@
|
||||
/*
|
||||
*
|
||||
* Copyright 2015, Google Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are
|
||||
* met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above
|
||||
* copyright notice, this list of conditions and the following disclaimer
|
||||
* in the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
* * Neither the name of Google Inc. nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from
|
||||
* this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef GRPC_INTERNAL_COMPILER_GO_GENERATOR_H
|
||||
#define GRPC_INTERNAL_COMPILER_GO_GENERATOR_H
|
||||
|
||||
//go generator is used to generate GRPC code for serialization system, such as flatbuffers
|
||||
// go generator is used to generate GRPC code for serialization system, such as
|
||||
// flatbuffers
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
@@ -43,14 +11,15 @@
|
||||
namespace grpc_go_generator {
|
||||
|
||||
struct Parameters {
|
||||
//Defines the custom parameter types for methods
|
||||
//eg: flatbuffers uses flatbuffers.Builder as input for the client and output for the server
|
||||
// Defines the custom parameter types for methods
|
||||
// eg: flatbuffers uses flatbuffers.Builder as input for the client and output
|
||||
// for the server
|
||||
grpc::string custom_method_io_type;
|
||||
|
||||
//Package name for the service
|
||||
// Package name for the service
|
||||
grpc::string package_name;
|
||||
|
||||
//Prefix for RPC Calls
|
||||
// Prefix for RPC Calls
|
||||
grpc::string service_prefix;
|
||||
};
|
||||
|
||||
@@ -59,6 +28,6 @@ grpc::string GenerateServiceSource(grpc_generator::File *file,
|
||||
const grpc_generator::Service *service,
|
||||
grpc_go_generator::Parameters *parameters);
|
||||
|
||||
}
|
||||
} // namespace grpc_go_generator
|
||||
|
||||
#endif // GRPC_INTERNAL_COMPILER_GO_GENERATOR_H
|
||||
|
||||
+136
-148
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "java_generator.h"
|
||||
#include "src/compiler/java_generator.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
@@ -23,21 +23,18 @@
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
// just to get flatbuffer_version_string()
|
||||
#include <flatbuffers/flatbuffers.h>
|
||||
#include <flatbuffers/util.h>
|
||||
#include "flatbuffers/util.h"
|
||||
#define to_string flatbuffers::NumToString
|
||||
|
||||
// Stringify helpers used solely to cast GRPC_VERSION
|
||||
#ifndef STR
|
||||
#define STR(s) #s
|
||||
# define STR(s) # s
|
||||
#endif
|
||||
|
||||
#ifndef XSTR
|
||||
#define XSTR(s) STR(s)
|
||||
# define XSTR(s) STR(s)
|
||||
#endif
|
||||
|
||||
|
||||
typedef grpc_generator::Printer Printer;
|
||||
typedef std::map<grpc::string, grpc::string> VARS;
|
||||
typedef grpc_generator::Service ServiceDescriptor;
|
||||
@@ -47,13 +44,13 @@ typedef grpc_generator::Method MethodDescriptor;
|
||||
|
||||
namespace grpc_java_generator {
|
||||
typedef std::string string;
|
||||
namespace {
|
||||
// Generates imports for the service
|
||||
void GenerateImports(grpc_generator::File* file,
|
||||
grpc_generator::Printer* printer, VARS& vars) {
|
||||
static void GenerateImports(grpc_generator::File *file,
|
||||
grpc_generator::Printer *printer, VARS &vars) {
|
||||
vars["filename"] = file->filename();
|
||||
printer->Print(
|
||||
vars,
|
||||
"//Generated by flatc compiler (version $flatc_version$)\n");
|
||||
printer->Print(vars,
|
||||
"//Generated by flatc compiler (version $flatc_version$)\n");
|
||||
printer->Print("//If you make any local changes, they will be lost\n");
|
||||
printer->Print(vars, "//source: $filename$.fbs\n\n");
|
||||
printer->Print(vars, "package $Package$;\n\n");
|
||||
@@ -67,7 +64,7 @@ void GenerateImports(grpc_generator::File* file,
|
||||
// Adjust a method name prefix identifier to follow the JavaBean spec:
|
||||
// - decapitalize the first letter
|
||||
// - remove embedded underscores & capitalize the following letter
|
||||
static string MixedLower(const string& word) {
|
||||
static string MixedLower(const string &word) {
|
||||
string w;
|
||||
w += static_cast<string::value_type>(tolower(word[0]));
|
||||
bool after_underscore = false;
|
||||
@@ -87,7 +84,7 @@ static string MixedLower(const string& word) {
|
||||
// - An underscore is inserted where a lower case letter is followed by an
|
||||
// upper case letter.
|
||||
// - All letters are converted to upper case
|
||||
static string ToAllUpperCase(const string& word) {
|
||||
static string ToAllUpperCase(const string &word) {
|
||||
string w;
|
||||
for (size_t i = 0; i < word.length(); ++i) {
|
||||
w += static_cast<string::value_type>(toupper(word[i]));
|
||||
@@ -98,47 +95,47 @@ static string ToAllUpperCase(const string& word) {
|
||||
return w;
|
||||
}
|
||||
|
||||
static inline string LowerMethodName(const MethodDescriptor* method) {
|
||||
static inline string LowerMethodName(const MethodDescriptor *method) {
|
||||
return MixedLower(method->name());
|
||||
}
|
||||
|
||||
static inline string MethodPropertiesFieldName(const MethodDescriptor* method) {
|
||||
static inline string MethodPropertiesFieldName(const MethodDescriptor *method) {
|
||||
return "METHOD_" + ToAllUpperCase(method->name());
|
||||
}
|
||||
|
||||
static inline string MethodPropertiesGetterName(
|
||||
const MethodDescriptor* method) {
|
||||
const MethodDescriptor *method) {
|
||||
return MixedLower("get_" + method->name() + "_method");
|
||||
}
|
||||
|
||||
static inline string MethodIdFieldName(const MethodDescriptor* method) {
|
||||
static inline string MethodIdFieldName(const MethodDescriptor *method) {
|
||||
return "METHODID_" + ToAllUpperCase(method->name());
|
||||
}
|
||||
|
||||
static inline string JavaClassName(VARS& vars, const string& name) {
|
||||
static inline string JavaClassName(VARS &vars, const string &name) {
|
||||
// string name = google::protobuf::compiler::java::ClassName(desc);
|
||||
return vars["Package"] + name;
|
||||
}
|
||||
|
||||
static inline string ServiceClassName(const string& service_name) {
|
||||
static inline string ServiceClassName(const string &service_name) {
|
||||
return service_name + "Grpc";
|
||||
}
|
||||
|
||||
// TODO(nmittler): Remove once protobuf includes javadoc methods in
|
||||
// distribution.
|
||||
template <typename ITR>
|
||||
static void GrpcSplitStringToIteratorUsing(const string& full,
|
||||
const char* delim, ITR& result) {
|
||||
template<typename ITR>
|
||||
static void GrpcSplitStringToIteratorUsing(const string &full,
|
||||
const char *delim, ITR &result) {
|
||||
// Optimize the common case where delim is a single character.
|
||||
if (delim[0] != '\0' && delim[1] == '\0') {
|
||||
char c = delim[0];
|
||||
const char* p = full.data();
|
||||
const char* end = p + full.size();
|
||||
const char *p = full.data();
|
||||
const char *end = p + full.size();
|
||||
while (p != end) {
|
||||
if (*p == c) {
|
||||
++p;
|
||||
} else {
|
||||
const char* start = p;
|
||||
const char *start = p;
|
||||
while (++p != end && *p != c)
|
||||
;
|
||||
*result++ = string(start, p - start);
|
||||
@@ -160,13 +157,13 @@ static void GrpcSplitStringToIteratorUsing(const string& full,
|
||||
}
|
||||
}
|
||||
|
||||
static void GrpcSplitStringUsing(const string& full, const char* delim,
|
||||
std::vector<string>* result) {
|
||||
static void GrpcSplitStringUsing(const string &full, const char *delim,
|
||||
std::vector<string> *result) {
|
||||
std::back_insert_iterator<std::vector<string>> it(*result);
|
||||
GrpcSplitStringToIteratorUsing(full, delim, it);
|
||||
}
|
||||
|
||||
static std::vector<string> GrpcSplit(const string& full, const char* delim) {
|
||||
static std::vector<string> GrpcSplit(const string &full, const char *delim) {
|
||||
std::vector<string> result;
|
||||
GrpcSplitStringUsing(full, delim, &result);
|
||||
return result;
|
||||
@@ -174,7 +171,7 @@ static std::vector<string> GrpcSplit(const string& full, const char* delim) {
|
||||
|
||||
// TODO(nmittler): Remove once protobuf includes javadoc methods in
|
||||
// distribution.
|
||||
static string GrpcEscapeJavadoc(const string& input) {
|
||||
static string GrpcEscapeJavadoc(const string &input) {
|
||||
string result;
|
||||
result.reserve(input.size() * 2);
|
||||
|
||||
@@ -221,9 +218,7 @@ static string GrpcEscapeJavadoc(const string& input) {
|
||||
// Java interprets Unicode escape sequences anywhere!
|
||||
result.append("\");
|
||||
break;
|
||||
default:
|
||||
result.push_back(c);
|
||||
break;
|
||||
default: result.push_back(c); break;
|
||||
}
|
||||
|
||||
prev = c;
|
||||
@@ -232,7 +227,7 @@ static string GrpcEscapeJavadoc(const string& input) {
|
||||
return result;
|
||||
}
|
||||
|
||||
static std::vector<string> GrpcGetDocLines(const string& comments) {
|
||||
static std::vector<string> GrpcGetDocLines(const string &comments) {
|
||||
if (!comments.empty()) {
|
||||
// TODO(kenton): Ideally we should parse the comment text as Markdown and
|
||||
// write it back as HTML, but this requires a Markdown parser. For now
|
||||
@@ -243,27 +238,23 @@ static std::vector<string> GrpcGetDocLines(const string& comments) {
|
||||
string escapedComments = GrpcEscapeJavadoc(comments);
|
||||
|
||||
std::vector<string> lines = GrpcSplit(escapedComments, "\n");
|
||||
while (!lines.empty() && lines.back().empty()) {
|
||||
lines.pop_back();
|
||||
}
|
||||
while (!lines.empty() && lines.back().empty()) { lines.pop_back(); }
|
||||
return lines;
|
||||
}
|
||||
return std::vector<string>();
|
||||
}
|
||||
|
||||
static std::vector<string> GrpcGetDocLinesForDescriptor(
|
||||
const DescriptorType* descriptor) {
|
||||
const DescriptorType *descriptor) {
|
||||
return descriptor->GetAllComments();
|
||||
// return GrpcGetDocLines(descriptor->GetLeadingComments("///"));
|
||||
}
|
||||
|
||||
static void GrpcWriteDocCommentBody(Printer* printer, VARS& vars,
|
||||
const std::vector<string>& lines,
|
||||
static void GrpcWriteDocCommentBody(Printer *printer, VARS &vars,
|
||||
const std::vector<string> &lines,
|
||||
bool surroundWithPreTag) {
|
||||
if (!lines.empty()) {
|
||||
if (surroundWithPreTag) {
|
||||
printer->Print(" * <pre>\n");
|
||||
}
|
||||
if (surroundWithPreTag) { printer->Print(" * <pre>\n"); }
|
||||
|
||||
for (size_t i = 0; i < lines.size(); i++) {
|
||||
// Most lines should start with a space. Watch out for lines that start
|
||||
@@ -277,73 +268,72 @@ static void GrpcWriteDocCommentBody(Printer* printer, VARS& vars,
|
||||
}
|
||||
}
|
||||
|
||||
if (surroundWithPreTag) {
|
||||
printer->Print(" * </pre>\n");
|
||||
}
|
||||
if (surroundWithPreTag) { printer->Print(" * </pre>\n"); }
|
||||
}
|
||||
}
|
||||
|
||||
static void GrpcWriteDocComment(Printer* printer, VARS& vars,
|
||||
const string& comments) {
|
||||
static void GrpcWriteDocComment(Printer *printer, VARS &vars,
|
||||
const string &comments) {
|
||||
printer->Print("/**\n");
|
||||
std::vector<string> lines = GrpcGetDocLines(comments);
|
||||
GrpcWriteDocCommentBody(printer, vars, lines, false);
|
||||
printer->Print(" */\n");
|
||||
}
|
||||
|
||||
static void GrpcWriteServiceDocComment(Printer* printer, VARS& vars,
|
||||
const ServiceDescriptor* service) {
|
||||
static void GrpcWriteServiceDocComment(Printer *printer, VARS &vars,
|
||||
const ServiceDescriptor *service) {
|
||||
printer->Print("/**\n");
|
||||
std::vector<string> lines = GrpcGetDocLinesForDescriptor(service);
|
||||
GrpcWriteDocCommentBody(printer, vars, lines, true);
|
||||
printer->Print(" */\n");
|
||||
}
|
||||
|
||||
void GrpcWriteMethodDocComment(Printer* printer, VARS& vars,
|
||||
const MethodDescriptor* method) {
|
||||
static void GrpcWriteMethodDocComment(Printer *printer, VARS &vars,
|
||||
const MethodDescriptor *method) {
|
||||
printer->Print("/**\n");
|
||||
std::vector<string> lines = GrpcGetDocLinesForDescriptor(method);
|
||||
GrpcWriteDocCommentBody(printer, vars, lines, true);
|
||||
printer->Print(" */\n");
|
||||
}
|
||||
|
||||
//outputs static singleton extractor for type stored in "extr_type" and "extr_type_name" vars
|
||||
static void PrintTypeExtractor(Printer* p, VARS& vars) {
|
||||
p->Print(
|
||||
vars,
|
||||
"private static volatile FlatbuffersUtils.FBExtactor<$extr_type$> "
|
||||
"extractorOf$extr_type_name$;\n"
|
||||
"private static FlatbuffersUtils.FBExtactor<$extr_type$> "
|
||||
"getExtractorOf$extr_type_name$() {\n"
|
||||
" if (extractorOf$extr_type_name$ != null) return "
|
||||
"extractorOf$extr_type_name$;\n"
|
||||
" synchronized ($service_class_name$.class) {\n"
|
||||
" if (extractorOf$extr_type_name$ != null) return "
|
||||
"extractorOf$extr_type_name$;\n"
|
||||
" extractorOf$extr_type_name$ = new "
|
||||
"FlatbuffersUtils.FBExtactor<$extr_type$>() {\n"
|
||||
" public $extr_type$ extract (ByteBuffer buffer) {\n"
|
||||
" return "
|
||||
"$extr_type$.getRootAs$extr_type_name$(buffer);\n"
|
||||
" }\n"
|
||||
" };\n"
|
||||
" return extractorOf$extr_type_name$;\n"
|
||||
" }\n"
|
||||
"}\n\n");
|
||||
// outputs static singleton extractor for type stored in "extr_type" and
|
||||
// "extr_type_name" vars
|
||||
static void PrintTypeExtractor(Printer *p, VARS &vars) {
|
||||
p->Print(vars,
|
||||
"private static volatile FlatbuffersUtils.FBExtactor<$extr_type$> "
|
||||
"extractorOf$extr_type_name$;\n"
|
||||
"private static FlatbuffersUtils.FBExtactor<$extr_type$> "
|
||||
"getExtractorOf$extr_type_name$() {\n"
|
||||
" if (extractorOf$extr_type_name$ != null) return "
|
||||
"extractorOf$extr_type_name$;\n"
|
||||
" synchronized ($service_class_name$.class) {\n"
|
||||
" if (extractorOf$extr_type_name$ != null) return "
|
||||
"extractorOf$extr_type_name$;\n"
|
||||
" extractorOf$extr_type_name$ = new "
|
||||
"FlatbuffersUtils.FBExtactor<$extr_type$>() {\n"
|
||||
" public $extr_type$ extract (ByteBuffer buffer) {\n"
|
||||
" return "
|
||||
"$extr_type$.getRootAs$extr_type_name$(buffer);\n"
|
||||
" }\n"
|
||||
" };\n"
|
||||
" return extractorOf$extr_type_name$;\n"
|
||||
" }\n"
|
||||
"}\n\n");
|
||||
}
|
||||
static void PrintMethodFields(Printer* p, VARS& vars,
|
||||
const ServiceDescriptor* service) {
|
||||
static void PrintMethodFields(Printer *p, VARS &vars,
|
||||
const ServiceDescriptor *service) {
|
||||
p->Print("// Static method descriptors that strictly reflect the proto.\n");
|
||||
vars["service_name"] = service->name();
|
||||
|
||||
//set of names of rpc input- and output- types that were already encountered.
|
||||
//this is needed to avoid duplicating type extractor since it's possible that
|
||||
//the same type is used as an input or output type of more than a single RPC method
|
||||
// set of names of rpc input- and output- types that were already encountered.
|
||||
// this is needed to avoid duplicating type extractor since it's possible that
|
||||
// the same type is used as an input or output type of more than a single RPC
|
||||
// method
|
||||
std::set<std::string> encounteredTypes;
|
||||
|
||||
for (int i = 0; i < service->method_count(); ++i) {
|
||||
auto method = service->method(i);
|
||||
vars["arg_in_id"] = to_string(2L * i); //trying to make msvc 10 happy
|
||||
vars["arg_in_id"] = to_string(2L * i); // trying to make msvc 10 happy
|
||||
vars["arg_out_id"] = to_string(2L * i + 1);
|
||||
vars["method_name"] = method->name();
|
||||
vars["input_type_name"] = method->get_input_type_name();
|
||||
@@ -353,8 +343,10 @@ static void PrintMethodFields(Printer* p, VARS& vars,
|
||||
vars["method_field_name"] = MethodPropertiesFieldName(method.get());
|
||||
vars["method_new_field_name"] = MethodPropertiesGetterName(method.get());
|
||||
vars["method_method_name"] = MethodPropertiesGetterName(method.get());
|
||||
bool client_streaming = method->ClientStreaming() || method->BidiStreaming();
|
||||
bool server_streaming = method->ServerStreaming() || method->BidiStreaming();
|
||||
bool client_streaming =
|
||||
method->ClientStreaming() || method->BidiStreaming();
|
||||
bool server_streaming =
|
||||
method->ServerStreaming() || method->BidiStreaming();
|
||||
if (client_streaming) {
|
||||
if (server_streaming) {
|
||||
vars["method_type"] = "BIDI_STREAMING";
|
||||
@@ -394,32 +386,32 @@ static void PrintMethodFields(Printer* p, VARS& vars,
|
||||
}
|
||||
|
||||
p->Print(
|
||||
vars,
|
||||
"@$ExperimentalApi$(\"https://github.com/grpc/grpc-java/issues/"
|
||||
"1901\")\n"
|
||||
"public static $MethodDescriptor$<$input_type$,\n"
|
||||
" $output_type$> $method_method_name$() {\n"
|
||||
" $MethodDescriptor$<$input_type$, $output_type$> "
|
||||
"$method_new_field_name$;\n"
|
||||
" if (($method_new_field_name$ = "
|
||||
"$service_class_name$.$method_new_field_name$) == null) {\n"
|
||||
" synchronized ($service_class_name$.class) {\n"
|
||||
" if (($method_new_field_name$ = "
|
||||
"$service_class_name$.$method_new_field_name$) == null) {\n"
|
||||
" $service_class_name$.$method_new_field_name$ = "
|
||||
"$method_new_field_name$ = \n"
|
||||
" $MethodDescriptor$.<$input_type$, "
|
||||
"$output_type$>newBuilder()\n"
|
||||
" .setType($MethodType$.$method_type$)\n"
|
||||
" .setFullMethodName(generateFullMethodName(\n"
|
||||
" \"$Package$$service_name$\", \"$method_name$\"))\n"
|
||||
" .setSampledToLocalTracing(true)\n"
|
||||
" .setRequestMarshaller(FlatbuffersUtils.marshaller(\n"
|
||||
" $input_type$.class, "
|
||||
"getExtractorOf$input_type_name$()))\n"
|
||||
" .setResponseMarshaller(FlatbuffersUtils.marshaller(\n"
|
||||
" $output_type$.class, "
|
||||
"getExtractorOf$output_type_name$()))\n");
|
||||
vars,
|
||||
"@$ExperimentalApi$(\"https://github.com/grpc/grpc-java/issues/"
|
||||
"1901\")\n"
|
||||
"public static $MethodDescriptor$<$input_type$,\n"
|
||||
" $output_type$> $method_method_name$() {\n"
|
||||
" $MethodDescriptor$<$input_type$, $output_type$> "
|
||||
"$method_new_field_name$;\n"
|
||||
" if (($method_new_field_name$ = "
|
||||
"$service_class_name$.$method_new_field_name$) == null) {\n"
|
||||
" synchronized ($service_class_name$.class) {\n"
|
||||
" if (($method_new_field_name$ = "
|
||||
"$service_class_name$.$method_new_field_name$) == null) {\n"
|
||||
" $service_class_name$.$method_new_field_name$ = "
|
||||
"$method_new_field_name$ = \n"
|
||||
" $MethodDescriptor$.<$input_type$, "
|
||||
"$output_type$>newBuilder()\n"
|
||||
" .setType($MethodType$.$method_type$)\n"
|
||||
" .setFullMethodName(generateFullMethodName(\n"
|
||||
" \"$Package$$service_name$\", \"$method_name$\"))\n"
|
||||
" .setSampledToLocalTracing(true)\n"
|
||||
" .setRequestMarshaller(FlatbuffersUtils.marshaller(\n"
|
||||
" $input_type$.class, "
|
||||
"getExtractorOf$input_type_name$()))\n"
|
||||
" .setResponseMarshaller(FlatbuffersUtils.marshaller(\n"
|
||||
" $output_type$.class, "
|
||||
"getExtractorOf$output_type_name$()))\n");
|
||||
|
||||
// vars["proto_method_descriptor_supplier"] = service->name() +
|
||||
// "MethodDescriptorSupplier";
|
||||
@@ -451,11 +443,11 @@ enum StubType {
|
||||
|
||||
enum CallType { ASYNC_CALL = 0, BLOCKING_CALL = 1, FUTURE_CALL = 2 };
|
||||
|
||||
static void PrintBindServiceMethodBody(Printer* p, VARS& vars,
|
||||
const ServiceDescriptor* service);
|
||||
static void PrintBindServiceMethodBody(Printer *p, VARS &vars,
|
||||
const ServiceDescriptor *service);
|
||||
|
||||
// Prints a client interface or implementation class, or a server interface.
|
||||
static void PrintStub(Printer* p, VARS& vars, const ServiceDescriptor* service,
|
||||
static void PrintStub(Printer *p, VARS &vars, const ServiceDescriptor *service,
|
||||
StubType type) {
|
||||
const string service_name = service->name();
|
||||
vars["service_name"] = service_name;
|
||||
@@ -476,7 +468,7 @@ static void PrintStub(Printer* p, VARS& vars, const ServiceDescriptor* service,
|
||||
break;
|
||||
case BLOCKING_CLIENT_INTERFACE:
|
||||
interface = true;
|
||||
FLATBUFFERS_FALLTHROUGH(); // fall thru
|
||||
FLATBUFFERS_FALLTHROUGH(); // fall thru
|
||||
case BLOCKING_CLIENT_IMPL:
|
||||
call_type = BLOCKING_CALL;
|
||||
stub_name += "BlockingStub";
|
||||
@@ -484,7 +476,7 @@ static void PrintStub(Printer* p, VARS& vars, const ServiceDescriptor* service,
|
||||
break;
|
||||
case FUTURE_CLIENT_INTERFACE:
|
||||
interface = true;
|
||||
FLATBUFFERS_FALLTHROUGH(); // fall thru
|
||||
FLATBUFFERS_FALLTHROUGH(); // fall thru
|
||||
case FUTURE_CLIENT_IMPL:
|
||||
call_type = FUTURE_CALL;
|
||||
stub_name += "FutureStub";
|
||||
@@ -501,9 +493,7 @@ static void PrintStub(Printer* p, VARS& vars, const ServiceDescriptor* service,
|
||||
vars["client_name"] = client_name;
|
||||
|
||||
// Class head
|
||||
if (!interface) {
|
||||
GrpcWriteServiceDocComment(p, vars, service);
|
||||
}
|
||||
if (!interface) { GrpcWriteServiceDocComment(p, vars, service); }
|
||||
if (impl_base) {
|
||||
p->Print(vars,
|
||||
"public static abstract class $abstract_name$ implements "
|
||||
@@ -546,8 +536,10 @@ static void PrintStub(Printer* p, VARS& vars, const ServiceDescriptor* service,
|
||||
vars["output_type"] = JavaClassName(vars, method->get_output_type_name());
|
||||
vars["lower_method_name"] = LowerMethodName(&*method);
|
||||
vars["method_method_name"] = MethodPropertiesGetterName(&*method);
|
||||
bool client_streaming = method->ClientStreaming() || method->BidiStreaming();
|
||||
bool server_streaming = method->ServerStreaming() || method->BidiStreaming();
|
||||
bool client_streaming =
|
||||
method->ClientStreaming() || method->BidiStreaming();
|
||||
bool server_streaming =
|
||||
method->ServerStreaming() || method->BidiStreaming();
|
||||
|
||||
if (call_type == BLOCKING_CALL && client_streaming) {
|
||||
// Blocking client interface with client streaming is not available
|
||||
@@ -563,9 +555,7 @@ static void PrintStub(Printer* p, VARS& vars, const ServiceDescriptor* service,
|
||||
p->Print("\n");
|
||||
// TODO(nmittler): Replace with WriteMethodDocComment once included by the
|
||||
// protobuf distro.
|
||||
if (!interface) {
|
||||
GrpcWriteMethodDocComment(p, vars, &*method);
|
||||
}
|
||||
if (!interface) { GrpcWriteMethodDocComment(p, vars, &*method); }
|
||||
p->Print("public ");
|
||||
switch (call_type) {
|
||||
case BLOCKING_CALL:
|
||||
@@ -630,8 +620,7 @@ static void PrintStub(Printer* p, VARS& vars, const ServiceDescriptor* service,
|
||||
"responseObserver);\n");
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
default: break;
|
||||
}
|
||||
} else if (!interface) {
|
||||
switch (call_type) {
|
||||
@@ -706,15 +695,15 @@ static void PrintStub(Printer* p, VARS& vars, const ServiceDescriptor* service,
|
||||
}
|
||||
|
||||
static bool CompareMethodClientStreaming(
|
||||
const std::unique_ptr<const grpc_generator::Method>& method1,
|
||||
const std::unique_ptr<const grpc_generator::Method>& method2) {
|
||||
const std::unique_ptr<const grpc_generator::Method> &method1,
|
||||
const std::unique_ptr<const grpc_generator::Method> &method2) {
|
||||
return method1->ClientStreaming() < method2->ClientStreaming();
|
||||
}
|
||||
|
||||
// Place all method invocations into a single class to reduce memory footprint
|
||||
// on Android.
|
||||
static void PrintMethodHandlerClass(Printer* p, VARS& vars,
|
||||
const ServiceDescriptor* service) {
|
||||
static void PrintMethodHandlerClass(Printer *p, VARS &vars,
|
||||
const ServiceDescriptor *service) {
|
||||
// Sort method ids based on ClientStreaming() so switch tables are compact.
|
||||
std::vector<std::unique_ptr<const grpc_generator::Method>> sorted_methods(
|
||||
service->method_count());
|
||||
@@ -724,7 +713,7 @@ static void PrintMethodHandlerClass(Printer* p, VARS& vars,
|
||||
stable_sort(sorted_methods.begin(), sorted_methods.end(),
|
||||
CompareMethodClientStreaming);
|
||||
for (size_t i = 0; i < sorted_methods.size(); i++) {
|
||||
auto& method = sorted_methods[i];
|
||||
auto &method = sorted_methods[i];
|
||||
vars["method_id"] = to_string(i);
|
||||
vars["method_id_name"] = MethodIdFieldName(&*method);
|
||||
p->Print(vars,
|
||||
@@ -757,9 +746,7 @@ static void PrintMethodHandlerClass(Printer* p, VARS& vars,
|
||||
|
||||
for (int i = 0; i < service->method_count(); ++i) {
|
||||
auto method = service->method(i);
|
||||
if (method->ClientStreaming() || method->BidiStreaming()) {
|
||||
continue;
|
||||
}
|
||||
if (method->ClientStreaming() || method->BidiStreaming()) { continue; }
|
||||
vars["method_id_name"] = MethodIdFieldName(&*method);
|
||||
vars["lower_method_name"] = LowerMethodName(&*method);
|
||||
vars["input_type"] = JavaClassName(vars, method->get_input_type_name());
|
||||
@@ -791,9 +778,7 @@ static void PrintMethodHandlerClass(Printer* p, VARS& vars,
|
||||
|
||||
for (int i = 0; i < service->method_count(); ++i) {
|
||||
auto method = service->method(i);
|
||||
if (!(method->ClientStreaming() || method->BidiStreaming())) {
|
||||
continue;
|
||||
}
|
||||
if (!(method->ClientStreaming() || method->BidiStreaming())) { continue; }
|
||||
vars["method_id_name"] = MethodIdFieldName(&*method);
|
||||
vars["lower_method_name"] = LowerMethodName(&*method);
|
||||
vars["input_type"] = JavaClassName(vars, method->get_input_type_name());
|
||||
@@ -818,8 +803,8 @@ static void PrintMethodHandlerClass(Printer* p, VARS& vars,
|
||||
p->Print("}\n\n");
|
||||
}
|
||||
|
||||
static void PrintGetServiceDescriptorMethod(Printer* p, VARS& vars,
|
||||
const ServiceDescriptor* service) {
|
||||
static void PrintGetServiceDescriptorMethod(Printer *p, VARS &vars,
|
||||
const ServiceDescriptor *service) {
|
||||
vars["service_name"] = service->name();
|
||||
// vars["proto_base_descriptor_supplier"] = service->name() +
|
||||
// "BaseDescriptorSupplier"; vars["proto_file_descriptor_supplier"] =
|
||||
@@ -911,8 +896,8 @@ static void PrintGetServiceDescriptorMethod(Printer* p, VARS& vars,
|
||||
p->Print("}\n");
|
||||
}
|
||||
|
||||
static void PrintBindServiceMethodBody(Printer* p, VARS& vars,
|
||||
const ServiceDescriptor* service) {
|
||||
static void PrintBindServiceMethodBody(Printer *p, VARS &vars,
|
||||
const ServiceDescriptor *service) {
|
||||
vars["service_name"] = service->name();
|
||||
p->Indent();
|
||||
p->Print(vars,
|
||||
@@ -927,8 +912,10 @@ static void PrintBindServiceMethodBody(Printer* p, VARS& vars,
|
||||
vars["input_type"] = JavaClassName(vars, method->get_input_type_name());
|
||||
vars["output_type"] = JavaClassName(vars, method->get_output_type_name());
|
||||
vars["method_id_name"] = MethodIdFieldName(&*method);
|
||||
bool client_streaming = method->ClientStreaming() || method->BidiStreaming();
|
||||
bool server_streaming = method->ServerStreaming() || method->BidiStreaming();
|
||||
bool client_streaming =
|
||||
method->ClientStreaming() || method->BidiStreaming();
|
||||
bool server_streaming =
|
||||
method->ServerStreaming() || method->BidiStreaming();
|
||||
if (client_streaming) {
|
||||
if (server_streaming) {
|
||||
vars["calls_method"] = "asyncBidiStreamingCall";
|
||||
@@ -962,8 +949,8 @@ static void PrintBindServiceMethodBody(Printer* p, VARS& vars,
|
||||
p->Outdent();
|
||||
}
|
||||
|
||||
static void PrintService(Printer* p, VARS& vars,
|
||||
const ServiceDescriptor* service,
|
||||
static void PrintService(Printer *p, VARS &vars,
|
||||
const ServiceDescriptor *service,
|
||||
bool disable_version) {
|
||||
vars["service_name"] = service->name();
|
||||
vars["service_class_name"] = ServiceClassName(service->name());
|
||||
@@ -1043,7 +1030,7 @@ static void PrintService(Printer* p, VARS& vars,
|
||||
p->Print("}\n");
|
||||
}
|
||||
|
||||
void PrintStaticImports(Printer* p) {
|
||||
static void PrintStaticImports(Printer *p) {
|
||||
p->Print(
|
||||
"import java.nio.ByteBuffer;\n"
|
||||
"import static "
|
||||
@@ -1076,8 +1063,8 @@ void PrintStaticImports(Printer* p) {
|
||||
"io.grpc.stub.ServerCalls.asyncUnimplementedUnaryCall;\n\n");
|
||||
}
|
||||
|
||||
void GenerateService(const grpc_generator::Service* service,
|
||||
grpc_generator::Printer* printer, VARS& vars,
|
||||
static void GenerateService(const grpc_generator::Service *service,
|
||||
grpc_generator::Printer *printer, VARS &vars,
|
||||
bool disable_version) {
|
||||
// All non-generated classes must be referred by fully qualified names to
|
||||
// avoid collision with generated classes.
|
||||
@@ -1111,10 +1098,11 @@ void GenerateService(const grpc_generator::Service* service,
|
||||
|
||||
PrintService(printer, vars, service, disable_version);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
grpc::string GenerateServiceSource(
|
||||
grpc_generator::File* file, const grpc_generator::Service* service,
|
||||
grpc_java_generator::Parameters* parameters) {
|
||||
grpc_generator::File *file, const grpc_generator::Service *service,
|
||||
grpc_java_generator::Parameters *parameters) {
|
||||
grpc::string out;
|
||||
auto printer = file->CreatePrinter(&out);
|
||||
VARS vars;
|
||||
|
||||
@@ -0,0 +1,151 @@
|
||||
/*
|
||||
*
|
||||
* Copyright 2015 gRPC authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <map>
|
||||
#include <sstream>
|
||||
|
||||
#include "flatbuffers/util.h"
|
||||
#include "src/compiler/python_generator.h"
|
||||
|
||||
namespace grpc_python_generator {
|
||||
namespace {
|
||||
|
||||
static grpc::string GenerateMethodType(const grpc_generator::Method *method) {
|
||||
|
||||
if (method->NoStreaming())
|
||||
return "unary_unary";
|
||||
|
||||
if (method->ServerStreaming())
|
||||
return "unary_stream";
|
||||
|
||||
if (method->ClientStreaming())
|
||||
return "stream_unary";
|
||||
|
||||
return "stream_stream";
|
||||
}
|
||||
|
||||
grpc::string GenerateMethodInput(const grpc_generator::Method *method) {
|
||||
|
||||
if (method->NoStreaming() || method->ServerStreaming())
|
||||
return "self, request, context";
|
||||
|
||||
return "self, request_iterator, context";
|
||||
}
|
||||
|
||||
void GenerateStub(const grpc_generator::Service *service,
|
||||
grpc_generator::Printer *printer,
|
||||
std::map<grpc::string, grpc::string> *dictonary) {
|
||||
auto vars = *dictonary;
|
||||
printer->Print(vars, "class $ServiceName$Stub(object):\n");
|
||||
printer->Indent();
|
||||
printer->Print("\"\"\" Interface exported by the server. \"\"\"");
|
||||
printer->Print("\n\n");
|
||||
printer->Print("def __init__(self, channel):\n");
|
||||
printer->Indent();
|
||||
printer->Print("\"\"\" Constructor. \n\n");
|
||||
printer->Print("Args: \n");
|
||||
printer->Print("channel: A grpc.Channel. \n");
|
||||
printer->Print("\"\"\"\n\n");
|
||||
|
||||
for (int j = 0; j < service->method_count(); j++) {
|
||||
auto method = service->method(j);
|
||||
vars["MethodName"] = method->name();
|
||||
vars["MethodType"] = GenerateMethodType(&*method);
|
||||
printer->Print(vars, "self.$MethodName$ = channel.$MethodType$(\n");
|
||||
printer->Indent();
|
||||
printer->Print(vars, "\"/$PATH$$ServiceName$/$MethodName$\"\n");
|
||||
printer->Print(")\n");
|
||||
printer->Outdent();
|
||||
printer->Print("\n");
|
||||
}
|
||||
printer->Outdent();
|
||||
printer->Outdent();
|
||||
printer->Print("\n");
|
||||
}
|
||||
|
||||
void GenerateServicer(const grpc_generator::Service *service,
|
||||
grpc_generator::Printer *printer,
|
||||
std::map<grpc::string, grpc::string> *dictonary) {
|
||||
auto vars = *dictonary;
|
||||
printer->Print(vars, "class $ServiceName$Servicer(object):\n");
|
||||
printer->Indent();
|
||||
printer->Print("\"\"\" Interface exported by the server. \"\"\"");
|
||||
printer->Print("\n\n");
|
||||
|
||||
for (int j = 0; j < service->method_count(); j++) {
|
||||
auto method = service->method(j);
|
||||
vars["MethodName"] = method->name();
|
||||
vars["MethodInput"] = GenerateMethodInput(&*method);
|
||||
printer->Print(vars, "def $MethodName$($MethodInput$):\n");
|
||||
printer->Indent();
|
||||
printer->Print("context.set_code(grpc.StatusCode.UNIMPLEMENTED)\n");
|
||||
printer->Print("context.set_details('Method not implemented!')\n");
|
||||
printer->Print("raise NotImplementedError('Method not implemented!')\n");
|
||||
printer->Outdent();
|
||||
printer->Print("\n\n");
|
||||
}
|
||||
printer->Outdent();
|
||||
printer->Print("\n");
|
||||
|
||||
}
|
||||
|
||||
void GenerateRegister(const grpc_generator::Service *service,
|
||||
grpc_generator::Printer *printer,
|
||||
std::map<grpc::string, grpc::string> *dictonary) {
|
||||
auto vars = *dictonary;
|
||||
printer->Print(vars, "def add_$ServiceName$Servicer_to_server(servicer, server):\n");
|
||||
printer->Indent();
|
||||
printer->Print("rpc_method_handlers = {\n");
|
||||
printer->Indent();
|
||||
for (int j = 0; j < service->method_count(); j++) {
|
||||
auto method = service->method(j);
|
||||
vars["MethodName"] = method->name();
|
||||
vars["MethodType"] = GenerateMethodType(&*method);
|
||||
printer->Print(vars, "'$MethodName$': grpc.$MethodType$_rpc_method_handler(\n");
|
||||
printer->Indent();
|
||||
printer->Print(vars, "servicer.$MethodName$\n");
|
||||
printer->Outdent();
|
||||
printer->Print("),\n");
|
||||
}
|
||||
printer->Outdent();
|
||||
printer->Print("}\n");
|
||||
printer->Print(vars, "generic_handler = grpc.method_handlers_generic_handler(\n");
|
||||
printer->Indent();
|
||||
printer->Print(vars, "'$PATH$$ServiceName$', rpc_method_handlers)\n");
|
||||
printer->Outdent();
|
||||
printer->Print("server.add_generic_rpc_handlers((generic_handler,))");
|
||||
printer->Outdent();
|
||||
printer->Print("\n");
|
||||
}
|
||||
} // namespace
|
||||
|
||||
grpc::string Generate(grpc_generator::File *file,
|
||||
const grpc_generator::Service *service) {
|
||||
grpc::string output;
|
||||
std::map<grpc::string, grpc::string> vars;
|
||||
vars["PATH"] = file->package();
|
||||
if (!file->package().empty()) { vars["PATH"].append("."); }
|
||||
vars["ServiceName"] = service->name();
|
||||
auto printer = file->CreatePrinter(&output);
|
||||
GenerateStub(service, &*printer, &vars);
|
||||
GenerateServicer(service, &*printer, &vars);
|
||||
GenerateRegister(service, &*printer, &vars);
|
||||
return output;
|
||||
}
|
||||
|
||||
} // namespace grpc_python_generator
|
||||
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
*
|
||||
* Copyright 2015 gRPC authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef GRPC_INTERNAL_COMPILER_PYTHON_GENERATOR_H
|
||||
#define GRPC_INTERNAL_COMPILER_PYTHON_GENERATOR_H
|
||||
|
||||
#include <utility>
|
||||
|
||||
#include "src/compiler/schema_interface.h"
|
||||
|
||||
namespace grpc_python_generator {
|
||||
|
||||
grpc::string Generate(grpc_generator::File *file,
|
||||
const grpc_generator::Service *service);
|
||||
} // namespace grpc_python_generator
|
||||
|
||||
#endif // GRPC_INTERNAL_COMPILER_PYTHON_GENERATOR_H
|
||||
+21
-28
@@ -1,41 +1,25 @@
|
||||
/*
|
||||
*
|
||||
* Copyright 2015, Google Inc.
|
||||
* All rights reserved.
|
||||
* Copyright 2015 gRPC authors.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are
|
||||
* met:
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above
|
||||
* copyright notice, this list of conditions and the following disclaimer
|
||||
* in the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
* * Neither the name of Google Inc. nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from
|
||||
* this software without specific prior written permission.
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef GRPC_INTERNAL_COMPILER_SCHEMA_INTERFACE_H
|
||||
#define GRPC_INTERNAL_COMPILER_SCHEMA_INTERFACE_H
|
||||
|
||||
#include "src/compiler/config.h"
|
||||
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
@@ -77,8 +61,13 @@ struct Method : public CommentHolder {
|
||||
grpc::string *str, grpc::string generator_file_name,
|
||||
bool generate_in_pb2_grpc, grpc::string import_prefix) const = 0;
|
||||
|
||||
virtual std::vector<grpc::string> get_input_namespace_parts() const = 0;
|
||||
virtual grpc::string get_input_type_name() const = 0;
|
||||
virtual std::vector<grpc::string> get_output_namespace_parts() const = 0;
|
||||
virtual grpc::string get_output_type_name() const = 0;
|
||||
|
||||
virtual grpc::string get_fb_builder() const = 0;
|
||||
|
||||
virtual bool NoStreaming() const = 0;
|
||||
virtual bool ClientStreaming() const = 0;
|
||||
virtual bool ServerStreaming() const = 0;
|
||||
@@ -89,7 +78,9 @@ struct Method : public CommentHolder {
|
||||
struct Service : public CommentHolder {
|
||||
virtual ~Service() {}
|
||||
|
||||
virtual std::vector<grpc::string> namespace_parts() const = 0;
|
||||
virtual grpc::string name() const = 0;
|
||||
virtual bool is_internal() const = 0;
|
||||
|
||||
virtual int method_count() const = 0;
|
||||
virtual std::unique_ptr<const Method> method(int i) const = 0;
|
||||
@@ -101,6 +92,7 @@ struct Printer {
|
||||
virtual void Print(const std::map<grpc::string, grpc::string> &vars,
|
||||
const char *template_string) = 0;
|
||||
virtual void Print(const char *string) = 0;
|
||||
virtual void SetIndentationSize(const size_t size) = 0;
|
||||
virtual void Indent() = 0;
|
||||
virtual void Outdent() = 0;
|
||||
};
|
||||
@@ -119,7 +111,8 @@ struct File : public CommentHolder {
|
||||
virtual int service_count() const = 0;
|
||||
virtual std::unique_ptr<const Service> service(int i) const = 0;
|
||||
|
||||
virtual std::unique_ptr<Printer> CreatePrinter(grpc::string *str) const = 0;
|
||||
virtual std::unique_ptr<Printer> CreatePrinter(
|
||||
grpc::string *str, const char indentation_type = ' ') const = 0;
|
||||
};
|
||||
} // namespace grpc_generator
|
||||
|
||||
|
||||
@@ -0,0 +1,440 @@
|
||||
/*
|
||||
* Copyright 2020 Google Inc. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/*
|
||||
* NOTE: The following implementation is a translation for the Swift-grpc
|
||||
* generator since flatbuffers doesnt allow plugins for now. if an issue arises
|
||||
* please open an issue in the flatbuffers repository. This file should always
|
||||
* be maintained according to the Swift-grpc repository
|
||||
*/
|
||||
#include <map>
|
||||
#include <sstream>
|
||||
|
||||
#include "flatbuffers/util.h"
|
||||
#include "src/compiler/schema_interface.h"
|
||||
#include "src/compiler/swift_generator.h"
|
||||
|
||||
namespace grpc_swift_generator {
|
||||
namespace {
|
||||
|
||||
static std::string WrapInNameSpace(const std::vector<std::string> &components,
|
||||
const grpc::string &name) {
|
||||
std::string qualified_name;
|
||||
for (auto it = components.begin(); it != components.end(); ++it)
|
||||
qualified_name += *it + "_";
|
||||
return qualified_name + name;
|
||||
}
|
||||
|
||||
static grpc::string GenerateMessage(const std::vector<std::string> &components,
|
||||
const grpc::string &name) {
|
||||
return "Message<" + WrapInNameSpace(components, name) + ">";
|
||||
}
|
||||
|
||||
// MARK: - Client
|
||||
|
||||
static void GenerateClientFuncName(const grpc_generator::Method *method,
|
||||
grpc_generator::Printer *printer,
|
||||
std::map<grpc::string, grpc::string> *dictonary) {
|
||||
auto vars = *dictonary;
|
||||
if (method->NoStreaming()) {
|
||||
printer->Print(vars,
|
||||
" $GenAccess$func $MethodName$(\n"
|
||||
" _ request: $Input$\n"
|
||||
" , callOptions: CallOptions?$isNil$\n"
|
||||
" ) -> UnaryCall<$Input$, $Output$>");
|
||||
return;
|
||||
}
|
||||
|
||||
if (method->ServerStreaming()) {
|
||||
printer->Print(vars,
|
||||
" $GenAccess$func $MethodName$(\n"
|
||||
" _ request: $Input$\n"
|
||||
" , callOptions: CallOptions?$isNil$,\n"
|
||||
" handler: @escaping ($Output$) -> Void\n"
|
||||
" ) -> ServerStreamingCall<$Input$, $Output$>");
|
||||
return;
|
||||
}
|
||||
|
||||
if (method->ClientStreaming()) {
|
||||
printer->Print(vars,
|
||||
" $GenAccess$func $MethodName$(\n"
|
||||
" callOptions: CallOptions?$isNil$\n"
|
||||
" ) -> ClientStreamingCall<$Input$, $Output$>");
|
||||
return;
|
||||
}
|
||||
|
||||
printer->Print(vars,
|
||||
" $GenAccess$func $MethodName$(\n"
|
||||
" callOptions: CallOptions?$isNil$,\n"
|
||||
" handler: @escaping ($Output$ ) -> Void\n"
|
||||
" ) -> BidirectionalStreamingCall<$Input$, $Output$>");
|
||||
}
|
||||
|
||||
static void GenerateClientFuncBody(const grpc_generator::Method *method,
|
||||
grpc_generator::Printer *printer,
|
||||
std::map<grpc::string, grpc::string> *dictonary) {
|
||||
auto vars = *dictonary;
|
||||
vars["Interceptor"] =
|
||||
"interceptors: self.interceptors?.make$MethodName$Interceptors() ?? []";
|
||||
if (method->NoStreaming()) {
|
||||
printer->Print(
|
||||
vars,
|
||||
" return self.makeUnaryCall(\n"
|
||||
" path: \"/$PATH$$ServiceName$/$MethodName$\",\n"
|
||||
" request: request,\n"
|
||||
" callOptions: callOptions ?? self.defaultCallOptions,\n"
|
||||
" $Interceptor$\n"
|
||||
" )\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if (method->ServerStreaming()) {
|
||||
printer->Print(
|
||||
vars,
|
||||
" return self.makeServerStreamingCall(\n"
|
||||
" path: \"/$PATH$$ServiceName$/$MethodName$\",\n"
|
||||
" request: request,\n"
|
||||
" callOptions: callOptions ?? self.defaultCallOptions,\n"
|
||||
" $Interceptor$,\n"
|
||||
" handler: handler\n"
|
||||
" )\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if (method->ClientStreaming()) {
|
||||
printer->Print(
|
||||
vars,
|
||||
" return self.makeClientStreamingCall(\n"
|
||||
" path: \"/$PATH$$ServiceName$/$MethodName$\",\n"
|
||||
" callOptions: callOptions ?? self.defaultCallOptions,\n"
|
||||
" $Interceptor$\n"
|
||||
" )\n");
|
||||
return;
|
||||
}
|
||||
printer->Print(vars,
|
||||
" return self.makeBidirectionalStreamingCall(\n"
|
||||
" path: \"/$PATH$$ServiceName$/$MethodName$\",\n"
|
||||
" callOptions: callOptions ?? self.defaultCallOptions,\n"
|
||||
" $Interceptor$,\n"
|
||||
" handler: handler\n"
|
||||
" )\n");
|
||||
}
|
||||
|
||||
void GenerateClientProtocol(const grpc_generator::Service *service,
|
||||
grpc_generator::Printer *printer,
|
||||
std::map<grpc::string, grpc::string> *dictonary) {
|
||||
auto vars = *dictonary;
|
||||
printer->Print(
|
||||
vars,
|
||||
"$ACCESS$ protocol $ServiceQualifiedName$ClientProtocol: GRPCClient {");
|
||||
printer->Print("\n\n");
|
||||
printer->Print(" var serviceName: String { get }");
|
||||
printer->Print("\n\n");
|
||||
printer->Print(
|
||||
vars,
|
||||
" var interceptors: "
|
||||
"$ServiceQualifiedName$ClientInterceptorFactoryProtocol? { get }");
|
||||
printer->Print("\n\n");
|
||||
|
||||
vars["GenAccess"] = "";
|
||||
for (auto it = 0; it < service->method_count(); it++) {
|
||||
auto method = service->method(it);
|
||||
vars["Input"] = GenerateMessage(method->get_input_namespace_parts(),
|
||||
method->get_input_type_name());
|
||||
vars["Output"] = GenerateMessage(method->get_output_namespace_parts(),
|
||||
method->get_output_type_name());
|
||||
vars["MethodName"] = method->name();
|
||||
vars["isNil"] = "";
|
||||
GenerateClientFuncName(method.get(), &*printer, &vars);
|
||||
printer->Print("\n\n");
|
||||
}
|
||||
printer->Print("}\n\n");
|
||||
|
||||
printer->Print(vars, "extension $ServiceQualifiedName$ClientProtocol {");
|
||||
printer->Print("\n\n");
|
||||
printer->Print(vars,
|
||||
" $ACCESS$ var serviceName: String { "
|
||||
"\"$PATH$$ServiceName$\" }\n");
|
||||
|
||||
vars["GenAccess"] = service->is_internal() ? "internal " : "public ";
|
||||
for (auto it = 0; it < service->method_count(); it++) {
|
||||
auto method = service->method(it);
|
||||
vars["Input"] = GenerateMessage(method->get_input_namespace_parts(),
|
||||
method->get_input_type_name());
|
||||
vars["Output"] = GenerateMessage(method->get_output_namespace_parts(),
|
||||
method->get_output_type_name());
|
||||
vars["MethodName"] = method->name();
|
||||
vars["isNil"] = " = nil";
|
||||
printer->Print("\n");
|
||||
GenerateClientFuncName(method.get(), &*printer, &vars);
|
||||
printer->Print(" {\n");
|
||||
GenerateClientFuncBody(method.get(), &*printer, &vars);
|
||||
printer->Print(" }\n");
|
||||
}
|
||||
printer->Print("}\n\n");
|
||||
|
||||
printer->Print(vars,
|
||||
"$ACCESS$ protocol "
|
||||
"$ServiceQualifiedName$ClientInterceptorFactoryProtocol {\n");
|
||||
|
||||
for (auto it = 0; it < service->method_count(); it++) {
|
||||
auto method = service->method(it);
|
||||
vars["Input"] = GenerateMessage(method->get_input_namespace_parts(),
|
||||
method->get_input_type_name());
|
||||
vars["Output"] = GenerateMessage(method->get_output_namespace_parts(),
|
||||
method->get_output_type_name());
|
||||
vars["MethodName"] = method->name();
|
||||
printer->Print(
|
||||
vars,
|
||||
" /// - Returns: Interceptors to use when invoking '$MethodName$'.\n");
|
||||
printer->Print(vars,
|
||||
" func make$MethodName$Interceptors() -> "
|
||||
"[ClientInterceptor<$Input$, $Output$>]\n\n");
|
||||
}
|
||||
printer->Print("}\n\n");
|
||||
}
|
||||
|
||||
void GenerateClientClass(grpc_generator::Printer *printer,
|
||||
std::map<grpc::string, grpc::string> *dictonary) {
|
||||
auto vars = *dictonary;
|
||||
printer->Print(vars,
|
||||
"$ACCESS$ final class $ServiceQualifiedName$ServiceClient: "
|
||||
"$ServiceQualifiedName$ClientProtocol {\n");
|
||||
printer->Print(vars, " $ACCESS$ let channel: GRPCChannel\n");
|
||||
printer->Print(vars, " $ACCESS$ var defaultCallOptions: CallOptions\n");
|
||||
printer->Print(vars,
|
||||
" $ACCESS$ var interceptors: "
|
||||
"$ServiceQualifiedName$ClientInterceptorFactoryProtocol?\n");
|
||||
printer->Print("\n");
|
||||
printer->Print(
|
||||
vars,
|
||||
" $ACCESS$ init(\n"
|
||||
" channel: GRPCChannel,\n"
|
||||
" defaultCallOptions: CallOptions = CallOptions(),\n"
|
||||
" interceptors: "
|
||||
"$ServiceQualifiedName$ClientInterceptorFactoryProtocol? = nil\n"
|
||||
" ) {\n");
|
||||
printer->Print(" self.channel = channel\n");
|
||||
printer->Print(" self.defaultCallOptions = defaultCallOptions\n");
|
||||
printer->Print(" self.interceptors = interceptors\n");
|
||||
printer->Print(" }");
|
||||
printer->Print("\n");
|
||||
printer->Print("}\n");
|
||||
}
|
||||
|
||||
// MARK: - Server
|
||||
|
||||
grpc::string GenerateServerFuncName(const grpc_generator::Method *method) {
|
||||
if (method->NoStreaming()) {
|
||||
return "func $MethodName$(request: $Input$"
|
||||
", context: StatusOnlyCallContext) -> EventLoopFuture<$Output$>";
|
||||
}
|
||||
|
||||
if (method->ClientStreaming()) {
|
||||
return "func $MethodName$(context: UnaryResponseCallContext<$Output$>) -> "
|
||||
"EventLoopFuture<(StreamEvent<$Input$"
|
||||
">) -> Void>";
|
||||
}
|
||||
|
||||
if (method->ServerStreaming()) {
|
||||
return "func $MethodName$(request: $Input$"
|
||||
", context: StreamingResponseCallContext<$Output$>) -> "
|
||||
"EventLoopFuture<GRPCStatus>";
|
||||
}
|
||||
return "func $MethodName$(context: StreamingResponseCallContext<$Output$>) "
|
||||
"-> EventLoopFuture<(StreamEvent<$Input$>) -> Void>";
|
||||
}
|
||||
|
||||
grpc::string GenerateServerExtensionBody(const grpc_generator::Method *method) {
|
||||
grpc::string start = " case \"$MethodName$\":\n ";
|
||||
grpc::string interceptors =
|
||||
" interceptors: self.interceptors?.make$MethodName$Interceptors() "
|
||||
"?? [],\n";
|
||||
if (method->NoStreaming()) {
|
||||
return start +
|
||||
"return UnaryServerHandler(\n"
|
||||
" context: context,\n"
|
||||
" requestDeserializer: GRPCPayloadDeserializer<$Input$>(),\n"
|
||||
" responseSerializer: GRPCPayloadSerializer<$Output$>(),\n" +
|
||||
interceptors +
|
||||
" userFunction: self.$MethodName$(request:context:))\n";
|
||||
}
|
||||
if (method->ServerStreaming()) {
|
||||
return start +
|
||||
"return ServerStreamingServerHandler(\n"
|
||||
" context: context,\n"
|
||||
" requestDeserializer: GRPCPayloadDeserializer<$Input$>(),\n"
|
||||
" responseSerializer: GRPCPayloadSerializer<$Output$>(),\n" +
|
||||
interceptors +
|
||||
" userFunction: self.$MethodName$(request:context:))\n";
|
||||
}
|
||||
if (method->ClientStreaming()) {
|
||||
return start +
|
||||
"return ClientStreamingServerHandler(\n"
|
||||
" context: context,\n"
|
||||
" requestDeserializer: GRPCPayloadDeserializer<$Input$>(),\n"
|
||||
" responseSerializer: GRPCPayloadSerializer<$Output$>(),\n" +
|
||||
interceptors +
|
||||
" observerFactory: self.$MethodName$(context:))\n";
|
||||
}
|
||||
if (method->BidiStreaming()) {
|
||||
return start +
|
||||
"return BidirectionalStreamingServerHandler(\n"
|
||||
" context: context,\n"
|
||||
" requestDeserializer: GRPCPayloadDeserializer<$Input$>(),\n"
|
||||
" responseSerializer: GRPCPayloadSerializer<$Output$>(),\n" +
|
||||
interceptors +
|
||||
" observerFactory: self.$MethodName$(context:))\n";
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
void GenerateServerProtocol(const grpc_generator::Service *service,
|
||||
grpc_generator::Printer *printer,
|
||||
std::map<grpc::string, grpc::string> *dictonary) {
|
||||
auto vars = *dictonary;
|
||||
printer->Print(vars,
|
||||
"$ACCESS$ protocol $ServiceQualifiedName$Provider: "
|
||||
"CallHandlerProvider {\n");
|
||||
printer->Print(
|
||||
vars,
|
||||
" var interceptors: "
|
||||
"$ServiceQualifiedName$ServerInterceptorFactoryProtocol? { get }\n");
|
||||
for (auto it = 0; it < service->method_count(); it++) {
|
||||
auto method = service->method(it);
|
||||
vars["Input"] = GenerateMessage(method->get_input_namespace_parts(),
|
||||
method->get_input_type_name());
|
||||
vars["Output"] = GenerateMessage(method->get_output_namespace_parts(),
|
||||
method->get_output_type_name());
|
||||
vars["MethodName"] = method->name();
|
||||
printer->Print(" ");
|
||||
auto func = GenerateServerFuncName(method.get());
|
||||
printer->Print(vars, func.c_str());
|
||||
printer->Print("\n");
|
||||
}
|
||||
printer->Print("}\n\n");
|
||||
|
||||
printer->Print(vars, "$ACCESS$ extension $ServiceQualifiedName$Provider {\n");
|
||||
printer->Print("\n");
|
||||
printer->Print(vars,
|
||||
" var serviceName: Substring { return "
|
||||
"\"$PATH$$ServiceName$\" }\n");
|
||||
printer->Print("\n");
|
||||
printer->Print(
|
||||
" func handle(method name: Substring, context: "
|
||||
"CallHandlerContext) -> GRPCServerHandlerProtocol? {\n");
|
||||
printer->Print(" switch name {\n");
|
||||
for (auto it = 0; it < service->method_count(); it++) {
|
||||
auto method = service->method(it);
|
||||
vars["Input"] = GenerateMessage(method->get_input_namespace_parts(),
|
||||
method->get_input_type_name());
|
||||
vars["Output"] = GenerateMessage(method->get_output_namespace_parts(),
|
||||
method->get_output_type_name());
|
||||
vars["MethodName"] = method->name();
|
||||
auto body = GenerateServerExtensionBody(method.get());
|
||||
printer->Print(vars, body.c_str());
|
||||
printer->Print("\n");
|
||||
}
|
||||
printer->Print(" default: return nil;\n");
|
||||
printer->Print(" }\n");
|
||||
printer->Print(" }\n\n");
|
||||
printer->Print("}\n\n");
|
||||
|
||||
printer->Print(vars,
|
||||
"$ACCESS$ protocol "
|
||||
"$ServiceQualifiedName$ServerInterceptorFactoryProtocol {\n");
|
||||
for (auto it = 0; it < service->method_count(); it++) {
|
||||
auto method = service->method(it);
|
||||
vars["Input"] = GenerateMessage(method->get_input_namespace_parts(),
|
||||
method->get_input_type_name());
|
||||
vars["Output"] = GenerateMessage(method->get_output_namespace_parts(),
|
||||
method->get_output_type_name());
|
||||
vars["MethodName"] = method->name();
|
||||
printer->Print(
|
||||
vars,
|
||||
" /// - Returns: Interceptors to use when handling '$MethodName$'.\n"
|
||||
" /// Defaults to calling `self.makeInterceptors()`.\n");
|
||||
printer->Print(vars,
|
||||
" func make$MethodName$Interceptors() -> "
|
||||
"[ServerInterceptor<$Input$, $Output$>]\n\n");
|
||||
}
|
||||
printer->Print("}");
|
||||
}
|
||||
} // namespace
|
||||
|
||||
grpc::string Generate(grpc_generator::File *file,
|
||||
const grpc_generator::Service *service) {
|
||||
grpc::string output;
|
||||
std::map<grpc::string, grpc::string> vars;
|
||||
vars["PATH"] = file->package();
|
||||
if (!file->package().empty()) { vars["PATH"].append("."); }
|
||||
vars["ServiceQualifiedName"] =
|
||||
WrapInNameSpace(service->namespace_parts(), service->name());
|
||||
vars["ServiceName"] = service->name();
|
||||
vars["ACCESS"] = service->is_internal() ? "internal" : "public";
|
||||
auto printer = file->CreatePrinter(&output);
|
||||
printer->Print(
|
||||
vars,
|
||||
"/// Usage: instantiate $ServiceQualifiedName$ServiceClient, then call "
|
||||
"methods of this protocol to make API calls.\n");
|
||||
GenerateClientProtocol(service, &*printer, &vars);
|
||||
GenerateClientClass(&*printer, &vars);
|
||||
printer->Print("\n");
|
||||
GenerateServerProtocol(service, &*printer, &vars);
|
||||
return output;
|
||||
}
|
||||
|
||||
grpc::string GenerateHeader() {
|
||||
grpc::string code;
|
||||
code +=
|
||||
"/// The following code is generated by the Flatbuffers library which "
|
||||
"might not be in sync with grpc-swift\n";
|
||||
code +=
|
||||
"/// in case of an issue please open github issue, though it would be "
|
||||
"maintained\n";
|
||||
code += "\n";
|
||||
code += "// swiftlint:disable all\n";
|
||||
code += "// swiftformat:disable all\n";
|
||||
code += "\n";
|
||||
code += "import Foundation\n";
|
||||
code += "import GRPC\n";
|
||||
code += "import NIO\n";
|
||||
code += "import NIOHTTP1\n";
|
||||
code += "import FlatBuffers\n";
|
||||
code += "\n";
|
||||
code +=
|
||||
"public protocol GRPCFlatBufPayload: GRPCPayload, FlatBufferGRPCMessage "
|
||||
"{}\n";
|
||||
|
||||
code += "public extension GRPCFlatBufPayload {\n";
|
||||
code += " init(serializedByteBuffer: inout NIO.ByteBuffer) throws {\n";
|
||||
code +=
|
||||
" self.init(byteBuffer: FlatBuffers.ByteBuffer(contiguousBytes: "
|
||||
"serializedByteBuffer.readableBytesView, count: "
|
||||
"serializedByteBuffer.readableBytes))\n";
|
||||
code += " }\n";
|
||||
|
||||
code += " func serialize(into buffer: inout NIO.ByteBuffer) throws {\n";
|
||||
code +=
|
||||
" let buf = UnsafeRawBufferPointer(start: self.rawPointer, count: "
|
||||
"Int(self.size))\n";
|
||||
code += " buffer.writeBytes(buf)\n";
|
||||
code += " }\n";
|
||||
code += "}\n";
|
||||
code += "extension Message: GRPCFlatBufPayload {}\n";
|
||||
return code;
|
||||
}
|
||||
} // namespace grpc_swift_generator
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright 2020 Google Inc. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include "src/compiler/schema_interface.h"
|
||||
|
||||
#ifndef GRPC_CUSTOM_STRING
|
||||
# include <string>
|
||||
# define GRPC_CUSTOM_STRING std::string
|
||||
#endif
|
||||
|
||||
namespace grpc {
|
||||
|
||||
typedef GRPC_CUSTOM_STRING string;
|
||||
|
||||
} // namespace grpc
|
||||
|
||||
namespace grpc_swift_generator {
|
||||
grpc::string Generate(grpc_generator::File *file,
|
||||
const grpc_generator::Service *service);
|
||||
grpc::string GenerateHeader();
|
||||
} // namespace grpc_swift_generator
|
||||
@@ -0,0 +1,523 @@
|
||||
/*
|
||||
* Copyright 2020 Google Inc. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/*
|
||||
* NOTE: The following implementation is a translation for the Swift-grpc
|
||||
* generator since flatbuffers doesnt allow plugins for now. if an issue arises
|
||||
* please open an issue in the flatbuffers repository. This file should always
|
||||
* be maintained according to the Swift-grpc repository
|
||||
*/
|
||||
|
||||
#include "src/compiler/ts_generator.h"
|
||||
|
||||
#include <map>
|
||||
#include <sstream>
|
||||
|
||||
#include "flatbuffers/util.h"
|
||||
#include "src/compiler/schema_interface.h"
|
||||
|
||||
namespace grpc_ts_generator {
|
||||
namespace {
|
||||
|
||||
static grpc::string GenerateNamespace(const std::vector<std::string> ns,
|
||||
const std::string filename,
|
||||
const bool include_separator) {
|
||||
grpc::string path = "";
|
||||
if (include_separator) path += ".";
|
||||
|
||||
for (auto it = ns.begin(); it < ns.end(); it++) {
|
||||
if (include_separator) path += "/";
|
||||
path += include_separator
|
||||
? flatbuffers::ConvertCase(*it, flatbuffers::Case::kDasher,
|
||||
flatbuffers::Case::kUpperCamel)
|
||||
: *it + "_";
|
||||
}
|
||||
|
||||
if (include_separator) path += "/";
|
||||
path += include_separator
|
||||
? flatbuffers::ConvertCase(filename, flatbuffers::Case::kDasher,
|
||||
flatbuffers::Case::kUpperCamel)
|
||||
: filename;
|
||||
return path;
|
||||
}
|
||||
|
||||
// MARK: - Shared code
|
||||
|
||||
static void GenerateImports(const grpc_generator::Service *service,
|
||||
grpc_generator::Printer *printer,
|
||||
std::map<grpc::string, grpc::string> *dictonary,
|
||||
const bool grpc_var_import) {
|
||||
auto vars = *dictonary;
|
||||
printer->Print(
|
||||
"// Generated GRPC code for FlatBuffers TS *** DO NOT EDIT ***\n");
|
||||
printer->Print("import * as flatbuffers from 'flatbuffers';\n");
|
||||
|
||||
std::set<grpc::string> generated_imports;
|
||||
|
||||
for (auto it = 0; it < service->method_count(); it++) {
|
||||
auto method = service->method(it);
|
||||
auto output = method->get_output_type_name();
|
||||
auto input = method->get_input_type_name();
|
||||
auto input_namespace = method->get_input_namespace_parts();
|
||||
|
||||
vars["OUTPUT"] = output;
|
||||
vars["INPUT"] = input;
|
||||
|
||||
if (generated_imports.find(output) == generated_imports.end()) {
|
||||
generated_imports.insert(output);
|
||||
vars["OUTPUT_DIR"] =
|
||||
GenerateNamespace(method->get_output_namespace_parts(), output, true);
|
||||
vars["Output_alias"] = GenerateNamespace(
|
||||
method->get_output_namespace_parts(), output, false);
|
||||
printer->Print(
|
||||
vars, "import { $OUTPUT$ as $Output_alias$ } from '$OUTPUT_DIR$';\n");
|
||||
}
|
||||
if (generated_imports.find(input) == generated_imports.end()) {
|
||||
generated_imports.insert(input);
|
||||
vars["INPUT_DIR"] =
|
||||
GenerateNamespace(method->get_output_namespace_parts(), input, true);
|
||||
vars["Input_alias"] =
|
||||
GenerateNamespace(method->get_output_namespace_parts(), input, false);
|
||||
printer->Print(
|
||||
vars, "import { $INPUT$ as $Input_alias$ } from '$INPUT_DIR$';\n");
|
||||
}
|
||||
}
|
||||
printer->Print("\n");
|
||||
if (grpc_var_import)
|
||||
printer->Print("var grpc = require('@grpc/grpc-js');\n");
|
||||
else
|
||||
printer->Print("import * as grpc from '@grpc/grpc-js';\n");
|
||||
printer->Print("\n");
|
||||
}
|
||||
|
||||
// MARK: - Generate Main GRPC Code
|
||||
|
||||
static void GetStreamType(grpc_generator::Printer *printer,
|
||||
const grpc_generator::Method *method,
|
||||
std::map<grpc::string, grpc::string> *dictonary) {
|
||||
auto vars = *dictonary;
|
||||
auto client_streaming = method->ClientStreaming() || method->BidiStreaming();
|
||||
auto server_streaming = method->ServerStreaming() || method->BidiStreaming();
|
||||
vars["ClientStreaming"] = client_streaming ? "true" : "false";
|
||||
vars["ServerStreaming"] = server_streaming ? "true" : "false";
|
||||
printer->Print(vars, "requestStream: $ClientStreaming$,\n");
|
||||
printer->Print(vars, "responseStream: $ServerStreaming$,\n");
|
||||
}
|
||||
|
||||
static void GenerateSerializeMethod(grpc_generator::Printer *printer,
|
||||
std::map<grpc::string, grpc::string> *dictonary) {
|
||||
auto vars = *dictonary;
|
||||
printer->Print(vars, "function serialize_$Type$(buffer_args) {\n");
|
||||
printer->Indent();
|
||||
printer->Print(vars, "if (!(buffer_args instanceof $Type$)) {\n");
|
||||
printer->Indent();
|
||||
printer->Print(vars,
|
||||
"throw new Error('Expected argument of type $VALUE$');\n");
|
||||
printer->Outdent();
|
||||
printer->Print("}\n");
|
||||
printer->Print(vars, "return Buffer.from(buffer_args.serialize());\n");
|
||||
printer->Outdent();
|
||||
printer->Print("}\n\n");
|
||||
}
|
||||
|
||||
static void GenerateDeserializeMethod(
|
||||
grpc_generator::Printer *printer,
|
||||
std::map<grpc::string, grpc::string> *dictonary) {
|
||||
auto vars = *dictonary;
|
||||
printer->Print(vars, "function deserialize_$Type$(buffer) {\n");
|
||||
printer->Indent();
|
||||
printer->Print(vars,
|
||||
"return $Type$.getRootAs$VALUE$(new "
|
||||
"flatbuffers.ByteBuffer(buffer))\n");
|
||||
printer->Outdent();
|
||||
printer->Print("}\n\n");
|
||||
}
|
||||
|
||||
static void GenerateMethods(const grpc_generator::Service *service,
|
||||
grpc_generator::Printer *printer,
|
||||
std::map<grpc::string, grpc::string> *dictonary) {
|
||||
auto vars = *dictonary;
|
||||
|
||||
std::set<grpc::string> generated_functions;
|
||||
|
||||
for (auto it = 0; it < service->method_count(); it++) {
|
||||
auto method = service->method(it);
|
||||
auto output = method->get_output_type_name();
|
||||
auto input = method->get_input_type_name();
|
||||
|
||||
if (generated_functions.find(output) == generated_functions.end()) {
|
||||
generated_functions.insert(output);
|
||||
vars["VALUE"] = output;
|
||||
vars["Type"] = GenerateNamespace(method->get_output_namespace_parts(),
|
||||
output, false);
|
||||
GenerateSerializeMethod(printer, &vars);
|
||||
GenerateDeserializeMethod(printer, &vars);
|
||||
}
|
||||
printer->Print("\n");
|
||||
if (generated_functions.find(input) == generated_functions.end()) {
|
||||
generated_functions.insert(input);
|
||||
vars["VALUE"] = input;
|
||||
vars["Type"] =
|
||||
GenerateNamespace(method->get_input_namespace_parts(), input, false);
|
||||
GenerateSerializeMethod(printer, &vars);
|
||||
GenerateDeserializeMethod(printer, &vars);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void GenerateService(const grpc_generator::Service *service,
|
||||
grpc_generator::Printer *printer,
|
||||
std::map<grpc::string, grpc::string> *dictonary) {
|
||||
auto vars = *dictonary;
|
||||
vars["NAME"] = service->name() + "Service";
|
||||
|
||||
printer->Print(vars, "var $NAME$ = exports.$NAME$ = {\n");
|
||||
printer->Indent();
|
||||
for (auto it = 0; it < service->method_count(); it++) {
|
||||
auto method = service->method(it);
|
||||
vars["MethodName"] = method->name();
|
||||
vars["OUTPUT"] = GenerateNamespace(method->get_output_namespace_parts(),
|
||||
method->get_output_type_name(), false);
|
||||
vars["INPUT"] = GenerateNamespace(method->get_input_namespace_parts(),
|
||||
method->get_input_type_name(), false);
|
||||
printer->Print(vars, "$MethodName$: {\n");
|
||||
printer->Indent();
|
||||
printer->Print(vars, "path: '/$PATH$$ServiceName$/$MethodName$',\n");
|
||||
GetStreamType(printer, &*method, &vars);
|
||||
printer->Print(vars, "requestType: flatbuffers.ByteBuffer,\n");
|
||||
printer->Print(vars, "responseType: $OUTPUT$,\n");
|
||||
printer->Print(vars, "requestSerialize: serialize_$INPUT$,\n");
|
||||
printer->Print(vars, "requestDeserialize: deserialize_$INPUT$,\n");
|
||||
printer->Print(vars, "responseSerialize: serialize_$OUTPUT$,\n");
|
||||
printer->Print(vars, "responseDeserialize: deserialize_$OUTPUT$,\n");
|
||||
printer->Outdent();
|
||||
printer->Print("},\n");
|
||||
}
|
||||
printer->Outdent();
|
||||
printer->Print("};\n");
|
||||
printer->Print(vars,
|
||||
"exports.$ServiceName$Client = "
|
||||
"grpc.makeGenericClientConstructor($NAME$);");
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
grpc::string Generate(grpc_generator::File *file,
|
||||
const grpc_generator::Service *service,
|
||||
const grpc::string &filename) {
|
||||
grpc::string output;
|
||||
std::map<grpc::string, grpc::string> vars;
|
||||
|
||||
vars["PATH"] = file->package();
|
||||
|
||||
if (!file->package().empty()) { vars["PATH"].append("."); }
|
||||
|
||||
vars["ServiceName"] = service->name();
|
||||
vars["FBSFile"] = service->name() + "_fbs";
|
||||
vars["Filename"] = filename;
|
||||
auto printer = file->CreatePrinter(&output);
|
||||
|
||||
GenerateImports(service, &*printer, &vars, true);
|
||||
GenerateMethods(service, &*printer, &vars);
|
||||
GenerateService(service, &*printer, &vars);
|
||||
return output;
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
// MARK: - Generate Interface
|
||||
|
||||
static void FillInterface(grpc_generator::Printer *printer,
|
||||
std::map<grpc::string, grpc::string> *dictonary) {
|
||||
auto vars = *dictonary;
|
||||
printer->Print(vars,
|
||||
"interface I$ServiceName$Service_I$MethodName$ extends "
|
||||
"grpc.MethodDefinition<$INPUT$, $OUTPUT$> {\n");
|
||||
printer->Indent();
|
||||
printer->Print(vars, "path: string; // /$PATH$$ServiceName$/$MethodName$\n");
|
||||
printer->Print(vars, "requestStream: boolean; // $ClientStreaming$\n");
|
||||
printer->Print(vars, "responseStream: boolean; // $ServerStreaming$\n");
|
||||
printer->Print(vars, "requestSerialize: grpc.serialize<$INPUT$>;\n");
|
||||
printer->Print(vars, "requestDeserialize: grpc.deserialize<$INPUT$>;\n");
|
||||
printer->Print(vars, "responseSerialize: grpc.serialize<$OUTPUT$>;\n");
|
||||
printer->Print(vars, "responseDeserialize: grpc.deserialize<$OUTPUT$>;\n");
|
||||
printer->Outdent();
|
||||
printer->Print("}\n");
|
||||
}
|
||||
|
||||
static void GenerateInterfaces(const grpc_generator::Service *service,
|
||||
grpc_generator::Printer *printer,
|
||||
std::map<grpc::string, grpc::string> *dictonary) {
|
||||
auto vars = *dictonary;
|
||||
for (auto it = 0; it < service->method_count(); it++) {
|
||||
auto method = service->method(it);
|
||||
auto client_streaming =
|
||||
method->ClientStreaming() || method->BidiStreaming();
|
||||
auto server_streaming =
|
||||
method->ServerStreaming() || method->BidiStreaming();
|
||||
vars["ClientStreaming"] = client_streaming ? "true" : "false";
|
||||
vars["ServerStreaming"] = server_streaming ? "true" : "false";
|
||||
vars["MethodName"] = method->name();
|
||||
vars["OUTPUT"] = GenerateNamespace(method->get_output_namespace_parts(),
|
||||
method->get_output_type_name(), false);
|
||||
vars["INPUT"] = GenerateNamespace(method->get_input_namespace_parts(),
|
||||
method->get_input_type_name(), false);
|
||||
FillInterface(printer, &vars);
|
||||
printer->Print("\n");
|
||||
}
|
||||
}
|
||||
|
||||
static void GenerateExportedInterface(
|
||||
const grpc_generator::Service *service, grpc_generator::Printer *printer,
|
||||
std::map<grpc::string, grpc::string> *dictonary) {
|
||||
auto vars = *dictonary;
|
||||
printer->Print(vars,
|
||||
"export interface I$ServiceName$Server extends "
|
||||
"grpc.UntypedServiceImplementation {\n");
|
||||
printer->Indent();
|
||||
for (auto it = 0; it < service->method_count(); it++) {
|
||||
auto method = service->method(it);
|
||||
vars["Name"] = method->name();
|
||||
vars["OUTPUT"] = GenerateNamespace(method->get_output_namespace_parts(),
|
||||
method->get_output_type_name(), false);
|
||||
vars["INPUT"] = GenerateNamespace(method->get_input_namespace_parts(),
|
||||
method->get_input_type_name(), false);
|
||||
if (method->BidiStreaming()) {
|
||||
printer->Print(vars,
|
||||
"$Name$: grpc.handleBidiStreamingCall<$INPUT$, "
|
||||
"$OUTPUT$>;\n");
|
||||
continue;
|
||||
}
|
||||
if (method->NoStreaming()) {
|
||||
printer->Print(vars,
|
||||
"$Name$: grpc.handleUnaryCall<$INPUT$, "
|
||||
"$OUTPUT$>;\n");
|
||||
continue;
|
||||
}
|
||||
if (method->ClientStreaming()) {
|
||||
printer->Print(vars,
|
||||
"$Name$: grpc.handleClientStreamingCall<$INPUT$, "
|
||||
"$OUTPUT$>;\n");
|
||||
continue;
|
||||
}
|
||||
if (method->ServerStreaming()) {
|
||||
printer->Print(vars,
|
||||
"$Name$: grpc.handleServerStreamingCall<$INPUT$, "
|
||||
"$OUTPUT$>;\n");
|
||||
continue;
|
||||
}
|
||||
}
|
||||
printer->Outdent();
|
||||
printer->Print("}\n");
|
||||
}
|
||||
|
||||
static void GenerateMainInterface(const grpc_generator::Service *service,
|
||||
grpc_generator::Printer *printer,
|
||||
std::map<grpc::string, grpc::string> *dictonary) {
|
||||
auto vars = *dictonary;
|
||||
printer->Print(
|
||||
vars,
|
||||
"interface I$ServiceName$Service extends "
|
||||
"grpc.ServiceDefinition<grpc.UntypedServiceImplementation> {\n");
|
||||
printer->Indent();
|
||||
for (auto it = 0; it < service->method_count(); it++) {
|
||||
auto method = service->method(it);
|
||||
vars["MethodName"] = method->name();
|
||||
printer->Print(vars,
|
||||
"$MethodName$: I$ServiceName$Service_I$MethodName$;\n");
|
||||
}
|
||||
printer->Outdent();
|
||||
printer->Print("}\n");
|
||||
GenerateInterfaces(service, printer, &vars);
|
||||
printer->Print("\n");
|
||||
printer->Print(vars,
|
||||
"export const $ServiceName$Service: I$ServiceName$Service;\n");
|
||||
printer->Print("\n");
|
||||
GenerateExportedInterface(service, printer, &vars);
|
||||
}
|
||||
|
||||
static grpc::string GenerateMetaData() { return "metadata: grpc.Metadata"; }
|
||||
|
||||
static grpc::string GenerateOptions() { return "options: Partial<grpc.CallOptions>"; }
|
||||
|
||||
static void GenerateUnaryClientInterface(
|
||||
grpc_generator::Printer *printer,
|
||||
std::map<grpc::string, grpc::string> *dictonary) {
|
||||
auto vars = *dictonary;
|
||||
grpc::string main = "$ISPUBLIC$$MethodName$(request: $INPUT$, ";
|
||||
grpc::string callback =
|
||||
"callback: (error: grpc.ServiceError | null, response: "
|
||||
"$OUTPUT$) => void): grpc.ClientUnaryCall;\n";
|
||||
auto meta_data = GenerateMetaData() + ", ";
|
||||
auto options = GenerateOptions() + ", ";
|
||||
printer->Print(vars, (main + callback).c_str());
|
||||
printer->Print(vars, (main + meta_data + callback).c_str());
|
||||
printer->Print(vars, (main + meta_data + options + callback).c_str());
|
||||
}
|
||||
|
||||
static void GenerateClientWriteStreamInterface(
|
||||
grpc_generator::Printer *printer,
|
||||
std::map<grpc::string, grpc::string> *dictonary) {
|
||||
auto vars = *dictonary;
|
||||
grpc::string main = "$ISPUBLIC$$MethodName$(";
|
||||
grpc::string callback =
|
||||
"callback: (error: grpc.ServiceError | null, response: "
|
||||
"$INPUT$) => void): "
|
||||
"grpc.ClientWritableStream<$OUTPUT$>;\n";
|
||||
auto meta_data = GenerateMetaData() + ", ";
|
||||
auto options = GenerateOptions() + ", ";
|
||||
printer->Print(vars, (main + callback).c_str());
|
||||
printer->Print(vars, (main + meta_data + callback).c_str());
|
||||
printer->Print(vars, (main + options + callback).c_str());
|
||||
printer->Print(vars, (main + meta_data + options + callback).c_str());
|
||||
}
|
||||
|
||||
static void GenerateClientReadableStreamInterface(
|
||||
grpc_generator::Printer *printer,
|
||||
std::map<grpc::string, grpc::string> *dictonary) {
|
||||
auto vars = *dictonary;
|
||||
grpc::string main = "$ISPUBLIC$$MethodName$(request: $INPUT$, ";
|
||||
grpc::string end_function = "): grpc.ClientReadableStream<$OUTPUT$>;\n";
|
||||
auto meta_data = GenerateMetaData();
|
||||
auto options = GenerateOptions();
|
||||
printer->Print(vars, (main + meta_data + end_function).c_str());
|
||||
printer->Print(vars, (main + options + end_function).c_str());
|
||||
}
|
||||
|
||||
static void GenerateDepluxStreamInterface(
|
||||
grpc_generator::Printer *printer,
|
||||
std::map<grpc::string, grpc::string> *dictonary) {
|
||||
auto vars = *dictonary;
|
||||
grpc::string main = "$ISPUBLIC$$MethodName$(";
|
||||
grpc::string end_function =
|
||||
"): grpc.ClientDuplexStream<$INPUT$, $OUTPUT$>;\n";
|
||||
auto meta_data = GenerateMetaData();
|
||||
auto options = GenerateOptions();
|
||||
printer->Print(vars, (main + end_function).c_str());
|
||||
printer->Print(vars, (main + options + end_function).c_str());
|
||||
printer->Print(vars, (main + meta_data +
|
||||
", options?: Partial<grpc.CallOptions>" + end_function)
|
||||
.c_str());
|
||||
}
|
||||
|
||||
static void GenerateClientInterface(const grpc_generator::Service *service,
|
||||
grpc_generator::Printer *printer,
|
||||
std::map<grpc::string, grpc::string> *dictonary) {
|
||||
auto vars = *dictonary;
|
||||
printer->Print(vars, "export interface I$ServiceName$Client {\n");
|
||||
printer->Indent();
|
||||
for (auto it = 0; it < service->method_count(); it++) {
|
||||
auto method = service->method(it);
|
||||
vars["MethodName"] = method->name();
|
||||
vars["OUTPUT"] = GenerateNamespace(method->get_output_namespace_parts(),
|
||||
method->get_output_type_name(), false);
|
||||
vars["INPUT"] = GenerateNamespace(method->get_input_namespace_parts(),
|
||||
method->get_input_type_name(), false);
|
||||
vars["ISPUBLIC"] = "";
|
||||
|
||||
if (method->NoStreaming()) {
|
||||
GenerateUnaryClientInterface(printer, &vars);
|
||||
continue;
|
||||
}
|
||||
if (method->BidiStreaming()) {
|
||||
GenerateDepluxStreamInterface(printer, &vars);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (method->ClientStreaming()) {
|
||||
GenerateClientWriteStreamInterface(printer, &vars);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (method->ServerStreaming()) {
|
||||
GenerateClientReadableStreamInterface(printer, &vars);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
printer->Outdent();
|
||||
printer->Print("}\n");
|
||||
}
|
||||
|
||||
static void GenerateClientClassInterface(
|
||||
const grpc_generator::Service *service, grpc_generator::Printer *printer,
|
||||
std::map<grpc::string, grpc::string> *dictonary) {
|
||||
auto vars = *dictonary;
|
||||
printer->Print(vars,
|
||||
"export class $ServiceName$Client extends grpc.Client "
|
||||
"implements I$ServiceName$Client {\n");
|
||||
printer->Indent();
|
||||
printer->Print(
|
||||
"constructor(address: string, credentials: grpc.ChannelCredentials, "
|
||||
"options?: object);\n");
|
||||
for (auto it = 0; it < service->method_count(); it++) {
|
||||
auto method = service->method(it);
|
||||
vars["MethodName"] = method->name();
|
||||
vars["OUTPUT"] = GenerateNamespace(method->get_output_namespace_parts(),
|
||||
method->get_output_type_name(), false);
|
||||
vars["INPUT"] = GenerateNamespace(method->get_input_namespace_parts(),
|
||||
method->get_input_type_name(), false);
|
||||
vars["ISPUBLIC"] = "public ";
|
||||
if (method->NoStreaming()) {
|
||||
GenerateUnaryClientInterface(printer, &vars);
|
||||
continue;
|
||||
}
|
||||
if (method->BidiStreaming()) {
|
||||
GenerateDepluxStreamInterface(printer, &vars);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (method->ClientStreaming()) {
|
||||
GenerateClientWriteStreamInterface(printer, &vars);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (method->ServerStreaming()) {
|
||||
GenerateClientReadableStreamInterface(printer, &vars);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
printer->Outdent();
|
||||
printer->Print("}\n");
|
||||
}
|
||||
} // namespace
|
||||
|
||||
|
||||
grpc::string GenerateInterface(grpc_generator::File *file,
|
||||
const grpc_generator::Service *service,
|
||||
const grpc::string &filename) {
|
||||
grpc::string output;
|
||||
|
||||
std::set<grpc::string> generated_functions;
|
||||
std::map<grpc::string, grpc::string> vars;
|
||||
|
||||
vars["PATH"] = file->package();
|
||||
|
||||
if (!file->package().empty()) { vars["PATH"].append("."); }
|
||||
|
||||
vars["ServiceName"] = service->name();
|
||||
vars["FBSFile"] = service->name() + "_fbs";
|
||||
vars["Filename"] = filename;
|
||||
auto printer = file->CreatePrinter(&output);
|
||||
|
||||
GenerateImports(service, &*printer, &vars, false);
|
||||
GenerateMainInterface(service, &*printer, &vars);
|
||||
printer->Print("\n");
|
||||
GenerateClientInterface(service, &*printer, &vars);
|
||||
printer->Print("\n");
|
||||
GenerateClientClassInterface(service, &*printer, &vars);
|
||||
return output;
|
||||
}
|
||||
} // namespace grpc_ts_generator
|
||||
@@ -0,0 +1,26 @@
|
||||
#include <memory>
|
||||
#include <set>
|
||||
#include <vector>
|
||||
|
||||
#include "src/compiler/schema_interface.h"
|
||||
|
||||
#ifndef GRPC_CUSTOM_STRING
|
||||
# include <string>
|
||||
# define GRPC_CUSTOM_STRING std::string
|
||||
#endif
|
||||
|
||||
namespace grpc {
|
||||
|
||||
typedef GRPC_CUSTOM_STRING string;
|
||||
|
||||
} // namespace grpc
|
||||
|
||||
namespace grpc_ts_generator {
|
||||
grpc::string Generate(grpc_generator::File *file,
|
||||
const grpc_generator::Service *service,
|
||||
const grpc::string &filename);
|
||||
|
||||
grpc::string GenerateInterface(grpc_generator::File *file,
|
||||
const grpc_generator::Service *service,
|
||||
const grpc::string &filename);
|
||||
} // namespace grpc_ts_generator
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
cc_test(
|
||||
name = "grpc_test",
|
||||
srcs = [
|
||||
"grpctest.cpp",
|
||||
"message_builder_test.cpp",
|
||||
],
|
||||
copts = ["-Itests"],
|
||||
# This is required.
|
||||
linkstatic = 1,
|
||||
deps = [
|
||||
"//tests:monster_test_cc_fbs",
|
||||
"//tests:monster_test_grpc",
|
||||
"//tests:test_assert",
|
||||
"//tests:test_builder",
|
||||
"@com_github_grpc_grpc//:grpc++",
|
||||
],
|
||||
)
|
||||
+3
-3
@@ -96,7 +96,7 @@ public class JavaGrpcTest {
|
||||
if (monster.hp() > maxHp.get()) {
|
||||
// Found a monster of higher hit points.
|
||||
maxHp.set(monster.hp());
|
||||
maxHpMonsterName.set(monster.name());
|
||||
maxHpMonsterName.set(monster.name());
|
||||
maxHpCount.set(1);
|
||||
}
|
||||
else if (monster.hp() == maxHp.get()) {
|
||||
@@ -141,7 +141,7 @@ public class JavaGrpcTest {
|
||||
channel = ManagedChannelBuilder.forAddress("localhost", port)
|
||||
// Channels are secure by default (via SSL/TLS). For the example we disable TLS to avoid
|
||||
// needing certificates.
|
||||
.usePlaintext(true)
|
||||
.usePlaintext()
|
||||
.directExecutor()
|
||||
.build();
|
||||
blockingStub = MonsterStorageGrpc.newBlockingStub(channel);
|
||||
@@ -177,7 +177,7 @@ public class JavaGrpcTest {
|
||||
final CountDownLatch streamAlive = new CountDownLatch(1);
|
||||
|
||||
StreamObserver<Stat> statObserver = new StreamObserver<Stat>() {
|
||||
public void onCompleted() {
|
||||
public void onCompleted() {
|
||||
streamAlive.countDown();
|
||||
}
|
||||
public void onError(Throwable ex) { }
|
||||
|
||||
+14
-5
@@ -1,16 +1,20 @@
|
||||
package testing
|
||||
|
||||
import (
|
||||
"../../tests/MyGame/Example"
|
||||
flatbuffers "github.com/google/flatbuffers/go"
|
||||
"github.com/google/flatbuffers/tests/MyGame/Example"
|
||||
|
||||
"context"
|
||||
"net"
|
||||
"testing"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/encoding"
|
||||
)
|
||||
|
||||
type server struct{}
|
||||
type server struct {
|
||||
Example.UnimplementedMonsterStorageServer
|
||||
}
|
||||
|
||||
// test used to send and receive in grpc methods
|
||||
var test = "Flatbuffers"
|
||||
@@ -63,8 +67,12 @@ func RetrieveClient(c Example.MonsterStorageClient, t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("Retrieve client failed: %v", err)
|
||||
}
|
||||
if string(out.Name()) != test {
|
||||
t.Errorf("RetrieveClient failed: expected=%s, got=%s\n", test, out.Name())
|
||||
monster, err := out.Recv()
|
||||
if err != nil {
|
||||
t.Fatalf("Recv failed: %v", err)
|
||||
}
|
||||
if string(monster.Name()) != test {
|
||||
t.Errorf("RetrieveClient failed: expected=%s, got=%s\n", test, monster.Name())
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
@@ -74,7 +82,8 @@ func TestGRPC(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to listen: %v", err)
|
||||
}
|
||||
ser := grpc.NewServer(grpc.CustomCodec(flatbuffers.FlatbuffersCodec{}))
|
||||
ser := grpc.NewServer()
|
||||
encoding.RegisterCodec(flatbuffers.FlatbuffersCodec{})
|
||||
Example.RegisterMonsterStorageServer(ser, &server{})
|
||||
go func() {
|
||||
if err := ser.Serve(lis); err != nil {
|
||||
|
||||
+7
-10
@@ -14,17 +14,18 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <thread>
|
||||
#include <grpcpp/grpcpp.h>
|
||||
|
||||
#include <grpc++/grpc++.h>
|
||||
#include <condition_variable>
|
||||
#include <thread>
|
||||
|
||||
#include "monster_test.grpc.fb.h"
|
||||
#include "monster_test_generated.h"
|
||||
#include "test_assert.h"
|
||||
|
||||
using namespace MyGame::Example;
|
||||
using flatbuffers::grpc::MessageBuilder;
|
||||
using flatbuffers::FlatBufferBuilder;
|
||||
using flatbuffers::grpc::MessageBuilder;
|
||||
|
||||
void message_builder_tests();
|
||||
|
||||
@@ -97,8 +98,7 @@ void RunServer() {
|
||||
server_instance->Wait();
|
||||
}
|
||||
|
||||
template <class Builder>
|
||||
void StoreRPC(MonsterStorage::Stub *stub) {
|
||||
template<class Builder> void StoreRPC(MonsterStorage::Stub *stub) {
|
||||
Builder fbb;
|
||||
grpc::ClientContext context;
|
||||
// Build a request with the name set.
|
||||
@@ -119,8 +119,7 @@ void StoreRPC(MonsterStorage::Stub *stub) {
|
||||
}
|
||||
}
|
||||
|
||||
template <class Builder>
|
||||
void RetrieveRPC(MonsterStorage::Stub *stub) {
|
||||
template<class Builder> void RetrieveRPC(MonsterStorage::Stub *stub) {
|
||||
Builder fbb;
|
||||
grpc::ClientContext context;
|
||||
fbb.Clear();
|
||||
@@ -155,7 +154,6 @@ int grpc_server_test() {
|
||||
RetrieveRPC<MessageBuilder>(stub.get());
|
||||
RetrieveRPC<FlatBufferBuilder>(stub.get());
|
||||
|
||||
|
||||
#if !FLATBUFFERS_GRPC_DISABLE_AUTO_VERIFICATION
|
||||
{
|
||||
// Test that an invalid request errors out correctly
|
||||
@@ -181,7 +179,7 @@ int grpc_server_test() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int /*argc*/, const char * /*argv*/ []) {
|
||||
int main(int /*argc*/, const char * /*argv*/[]) {
|
||||
message_builder_tests();
|
||||
grpc_server_test();
|
||||
|
||||
@@ -193,4 +191,3 @@ int main(int /*argc*/, const char * /*argv*/ []) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+174
@@ -0,0 +1,174 @@
|
||||
from __future__ import print_function
|
||||
|
||||
import os
|
||||
import sys
|
||||
import grpc
|
||||
import flatbuffers
|
||||
|
||||
from concurrent import futures
|
||||
|
||||
sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', 'tests'))
|
||||
import MyGame.Example.Monster as Monster
|
||||
import MyGame.Example.Stat as Stat
|
||||
import MyGame.Example.Vec3 as Vec3
|
||||
import MyGame.Example.Test as Test
|
||||
import MyGame.Example.monster_test_grpc_fb as monster_grpc_fb
|
||||
|
||||
|
||||
test_stat_id = "test_stat_id"
|
||||
test_stat_val = 8
|
||||
test_stat_count = 1
|
||||
|
||||
test_monster_name1 = "test_monster_name1"
|
||||
test_monster_name2 = "test_monster_name2"
|
||||
test_string = "test_string"
|
||||
test_color = 2
|
||||
test_X = 3.0
|
||||
test_Y = 2.0
|
||||
test_Z = 6.0
|
||||
test_test1 = 4.0
|
||||
test_a = 8
|
||||
test_b = 5
|
||||
test_hp = 67
|
||||
test_inventory = [1, 1, 2, 3, 5, 8]
|
||||
test_testtype = 4
|
||||
|
||||
test_monsters_name_retrieve = ["big_monster", "small_monster"]
|
||||
test_no_of_monsters = 2
|
||||
|
||||
|
||||
class MonsterStorage(monster_grpc_fb.MonsterStorageServicer):
|
||||
|
||||
def Store(self, request, context):
|
||||
|
||||
m = Monster.Monster().GetRootAsMonster(request, 0)
|
||||
|
||||
assert m.Name().decode("utf-8") == test_monster_name1
|
||||
|
||||
assert m.Pos().X() == test_X
|
||||
assert m.Pos().Y() == test_Y
|
||||
assert m.Pos().Z() == test_Z
|
||||
assert m.Pos().Test1() == test_test1
|
||||
assert m.Pos().Test2() == test_color
|
||||
test3 = Test.Test()
|
||||
assert m.Pos().Test3(test3).A() == test_a
|
||||
assert m.Pos().Test3(test3).B() == test_b
|
||||
|
||||
assert m.Hp() == test_hp
|
||||
|
||||
assert m.Color() == test_color
|
||||
|
||||
assert m.InventoryLength() == len(test_inventory)
|
||||
for i in range(0, len(test_inventory)):
|
||||
assert m.Inventory(i) == test_inventory[len(test_inventory)-i -1]
|
||||
|
||||
assert m.TestType() == test_testtype
|
||||
|
||||
assert m.Test() is not None
|
||||
table = m.Test()
|
||||
|
||||
m2 = Monster.Monster()
|
||||
m2.Init(table.Bytes, table.Pos)
|
||||
assert m2.Name().decode("utf-8") == test_monster_name2
|
||||
|
||||
m3 = m.Enemy()
|
||||
assert m3.Name().decode("utf-8") == test_monster_name2
|
||||
|
||||
assert m.Testarrayofstring(0).decode("utf-8") == test_string
|
||||
|
||||
b = flatbuffers.Builder(0)
|
||||
i = b.CreateString(test_stat_id)
|
||||
Stat.StatStart(b)
|
||||
Stat.StatAddId(b, i)
|
||||
Stat.StatAddVal(b, test_stat_val)
|
||||
Stat.StatAddCount(b, test_stat_count)
|
||||
b.Finish(Stat.StatEnd(b))
|
||||
return bytes(b.Output())
|
||||
|
||||
def Retrieve(self, request, context):
|
||||
|
||||
s = Stat.Stat().GetRootAsStat(request, 0)
|
||||
|
||||
no_of_monsters = test_no_of_monsters
|
||||
for i in range(0, no_of_monsters):
|
||||
b = flatbuffers.Builder(0)
|
||||
i = b.CreateString(test_monsters_name_retrieve[i])
|
||||
Monster.MonsterStart(b)
|
||||
Monster.MonsterAddName(b, i)
|
||||
b.Finish(Monster.MonsterEnd(b))
|
||||
yield bytes(b.Output())
|
||||
|
||||
|
||||
def serve():
|
||||
|
||||
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
|
||||
monster_grpc_fb.add_MonsterStorageServicer_to_server(MonsterStorage(), server)
|
||||
server.add_insecure_port('[::]:50051')
|
||||
|
||||
server.start()
|
||||
|
||||
run()
|
||||
|
||||
|
||||
def run():
|
||||
|
||||
channel = grpc.insecure_channel('127.0.0.1:50051')
|
||||
stub = monster_grpc_fb.MonsterStorageStub(channel)
|
||||
|
||||
b = flatbuffers.Builder(0)
|
||||
name2 = b.CreateString(test_monster_name2)
|
||||
name1 = b.CreateString(test_monster_name1)
|
||||
Monster.MonsterStart(b)
|
||||
Monster.MonsterAddName(b, name2)
|
||||
monster2 = Monster.MonsterEnd(b)
|
||||
test1 = b.CreateString(test_string)
|
||||
|
||||
Monster.MonsterStartInventoryVector(b, len(test_inventory))
|
||||
for i in range(0, len(test_inventory)):
|
||||
b.PrependByte(test_inventory[i])
|
||||
inv = b.EndVector()
|
||||
|
||||
Monster.MonsterStartTest4Vector(b, 2)
|
||||
Test.CreateTest(b, 10, 20)
|
||||
Test.CreateTest(b, 30, 40)
|
||||
test4 = b.EndVector()
|
||||
|
||||
Monster.MonsterStartTestarrayofstringVector(b, 1)
|
||||
b.PrependUOffsetTRelative(test1)
|
||||
test_array_of_string = b.EndVector()
|
||||
|
||||
Monster.MonsterStart(b)
|
||||
|
||||
Monster.MonsterAddHp(b, test_hp)
|
||||
Monster.MonsterAddName(b, name1)
|
||||
Monster.MonsterAddColor(b, test_color)
|
||||
pos = Vec3.CreateVec3(b, test_X, test_Y, test_Z, test_test1, test_color, test_a, test_b)
|
||||
Monster.MonsterAddPos(b, pos)
|
||||
Monster.MonsterAddInventory(b, inv)
|
||||
Monster.MonsterAddTestType(b, test_testtype)
|
||||
Monster.MonsterAddTest(b, monster2)
|
||||
Monster.MonsterAddTest4(b, test4)
|
||||
Monster.MonsterAddEnemy(b, monster2)
|
||||
Monster.MonsterAddTestarrayofstring(b, test_array_of_string)
|
||||
monster = Monster.MonsterEnd(b)
|
||||
|
||||
b.Finish(monster)
|
||||
|
||||
stat_response = stub.Store(bytes(b.Output()))
|
||||
|
||||
s = Stat.Stat().GetRootAsStat(stat_response, 0)
|
||||
|
||||
assert s.Id().decode("utf-8") == test_stat_id
|
||||
assert s.Val() == test_stat_val
|
||||
assert s.Count() == test_stat_count
|
||||
|
||||
monster_reponses = stub.Retrieve(stat_response)
|
||||
count = 0
|
||||
for monster_reponse in monster_reponses:
|
||||
m = Monster.Monster().GetRootAsMonster(monster_reponse, 0)
|
||||
assert m.Name().decode("utf-8") == test_monsters_name_retrieve[count]
|
||||
count = count + 1
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
serve()
|
||||
+124
-96
@@ -3,26 +3,32 @@
|
||||
#include "test_assert.h"
|
||||
#include "test_builder.h"
|
||||
|
||||
using MyGame::Example::Vec3;
|
||||
using MyGame::Example::CreateStat;
|
||||
using MyGame::Example::Any_NONE;
|
||||
using MyGame::Example::CreateStat;
|
||||
using MyGame::Example::Vec3;
|
||||
|
||||
bool verify(flatbuffers::grpc::Message<Monster> &msg, const std::string &expected_name, Color color) {
|
||||
bool verify(flatbuffers::grpc::Message<Monster> &msg,
|
||||
const std::string &expected_name, Color expected_color) {
|
||||
const Monster *monster = msg.GetRoot();
|
||||
return (monster->name()->str() == expected_name) && (monster->color() == color);
|
||||
const auto name = monster->name()->str();
|
||||
const auto color = monster->color();
|
||||
TEST_EQ(name, expected_name);
|
||||
TEST_EQ(color, expected_color);
|
||||
return (name == expected_name) && (color == expected_color);
|
||||
}
|
||||
|
||||
bool release_n_verify(flatbuffers::grpc::MessageBuilder &mbb, const std::string &expected_name, Color color) {
|
||||
bool release_n_verify(flatbuffers::grpc::MessageBuilder &mbb,
|
||||
const std::string &expected_name, Color expected_color) {
|
||||
flatbuffers::grpc::Message<Monster> msg = mbb.ReleaseMessage<Monster>();
|
||||
const Monster *monster = msg.GetRoot();
|
||||
return (monster->name()->str() == expected_name) && (monster->color() == color);
|
||||
return verify(msg, expected_name, expected_color);
|
||||
}
|
||||
|
||||
void builder_move_assign_after_releaseraw_test(flatbuffers::grpc::MessageBuilder dst) {
|
||||
void builder_move_assign_after_releaseraw_test(
|
||||
flatbuffers::grpc::MessageBuilder dst) {
|
||||
auto root_offset1 = populate1(dst);
|
||||
dst.Finish(root_offset1);
|
||||
size_t size, offset;
|
||||
grpc_slice slice;
|
||||
::grpc::Slice slice;
|
||||
dst.ReleaseRaw(size, offset, slice);
|
||||
flatbuffers::FlatBufferBuilder src;
|
||||
auto root_offset2 = populate2(src);
|
||||
@@ -31,17 +37,15 @@ void builder_move_assign_after_releaseraw_test(flatbuffers::grpc::MessageBuilder
|
||||
// Move into a released builder.
|
||||
dst = std::move(src);
|
||||
TEST_EQ(dst.GetSize(), src_size);
|
||||
TEST_ASSERT(release_n_verify(dst, m2_name, m2_color));
|
||||
TEST_ASSERT(release_n_verify(dst, m2_name(), m2_color()));
|
||||
TEST_EQ(src.GetSize(), 0);
|
||||
grpc_slice_unref(slice);
|
||||
}
|
||||
|
||||
template <class SrcBuilder>
|
||||
template<class SrcBuilder>
|
||||
struct BuilderReuseTests<flatbuffers::grpc::MessageBuilder, SrcBuilder> {
|
||||
static void builder_reusable_after_release_message_test(TestSelector selector) {
|
||||
if (!selector.count(REUSABLE_AFTER_RELEASE_MESSAGE)) {
|
||||
return;
|
||||
}
|
||||
static void builder_reusable_after_release_message_test(
|
||||
TestSelector selector) {
|
||||
if (!selector.count(REUSABLE_AFTER_RELEASE_MESSAGE)) { return; }
|
||||
|
||||
flatbuffers::grpc::MessageBuilder mb;
|
||||
std::vector<flatbuffers::grpc::Message<Monster>> buffers;
|
||||
@@ -49,17 +53,15 @@ struct BuilderReuseTests<flatbuffers::grpc::MessageBuilder, SrcBuilder> {
|
||||
auto root_offset1 = populate1(mb);
|
||||
mb.Finish(root_offset1);
|
||||
buffers.push_back(mb.ReleaseMessage<Monster>());
|
||||
TEST_ASSERT_FUNC(verify(buffers[i], m1_name, m1_color));
|
||||
TEST_ASSERT_FUNC(verify(buffers[i], m1_name(), m1_color()));
|
||||
}
|
||||
}
|
||||
|
||||
static void builder_reusable_after_release_test(TestSelector selector) {
|
||||
if (!selector.count(REUSABLE_AFTER_RELEASE)) {
|
||||
return;
|
||||
}
|
||||
if (!selector.count(REUSABLE_AFTER_RELEASE)) { return; }
|
||||
|
||||
// FIXME: Populate-Release loop fails assert(GRPC_SLICE_IS_EMPTY(slice_)) in SliceAllocator::allocate
|
||||
// in the second iteration.
|
||||
// FIXME: Populate-Release loop fails assert(GRPC_SLICE_IS_EMPTY(slice_)) in
|
||||
// SliceAllocator::allocate in the second iteration.
|
||||
|
||||
flatbuffers::grpc::MessageBuilder mb;
|
||||
std::vector<flatbuffers::DetachedBuffer> buffers;
|
||||
@@ -67,34 +69,31 @@ struct BuilderReuseTests<flatbuffers::grpc::MessageBuilder, SrcBuilder> {
|
||||
auto root_offset1 = populate1(mb);
|
||||
mb.Finish(root_offset1);
|
||||
buffers.push_back(mb.Release());
|
||||
TEST_ASSERT_FUNC(verify(buffers[i], m1_name, m1_color));
|
||||
TEST_ASSERT_FUNC(verify(buffers[i], m1_name(), m1_color()));
|
||||
}
|
||||
}
|
||||
|
||||
static void builder_reusable_after_releaseraw_test(TestSelector selector) {
|
||||
if (!selector.count(REUSABLE_AFTER_RELEASE_RAW)) {
|
||||
return;
|
||||
}
|
||||
if (!selector.count(REUSABLE_AFTER_RELEASE_RAW)) { return; }
|
||||
|
||||
flatbuffers::grpc::MessageBuilder mb;
|
||||
for (int i = 0; i < 5; ++i) {
|
||||
auto root_offset1 = populate1(mb);
|
||||
mb.Finish(root_offset1);
|
||||
size_t size, offset;
|
||||
grpc_slice slice;
|
||||
::grpc::Slice slice;
|
||||
const uint8_t *buf = mb.ReleaseRaw(size, offset, slice);
|
||||
TEST_ASSERT_FUNC(verify(buf, offset, m1_name, m1_color));
|
||||
grpc_slice_unref(slice);
|
||||
TEST_ASSERT_FUNC(verify(buf, offset, m1_name(), m1_color()));
|
||||
}
|
||||
}
|
||||
|
||||
static void builder_reusable_after_release_and_move_assign_test(TestSelector selector) {
|
||||
if (!selector.count(REUSABLE_AFTER_RELEASE_AND_MOVE_ASSIGN)) {
|
||||
return;
|
||||
}
|
||||
static void builder_reusable_after_release_and_move_assign_test(
|
||||
TestSelector selector) {
|
||||
if (!selector.count(REUSABLE_AFTER_RELEASE_AND_MOVE_ASSIGN)) { return; }
|
||||
|
||||
// FIXME: Release-move_assign loop fails assert(p == GRPC_SLICE_START_PTR(slice_))
|
||||
// in DetachedBuffer destructor after all the iterations
|
||||
// FIXME: Release-move_assign loop fails assert(p ==
|
||||
// GRPC_SLICE_START_PTR(slice_)) in DetachedBuffer destructor after all the
|
||||
// iterations
|
||||
|
||||
flatbuffers::grpc::MessageBuilder dst;
|
||||
std::vector<flatbuffers::DetachedBuffer> buffers;
|
||||
@@ -103,7 +102,7 @@ struct BuilderReuseTests<flatbuffers::grpc::MessageBuilder, SrcBuilder> {
|
||||
auto root_offset1 = populate1(dst);
|
||||
dst.Finish(root_offset1);
|
||||
buffers.push_back(dst.Release());
|
||||
TEST_ASSERT_FUNC(verify(buffers[i], m1_name, m1_color));
|
||||
TEST_ASSERT_FUNC(verify(buffers[i], m1_name(), m1_color()));
|
||||
|
||||
// bring dst back to life.
|
||||
SrcBuilder src;
|
||||
@@ -113,7 +112,8 @@ struct BuilderReuseTests<flatbuffers::grpc::MessageBuilder, SrcBuilder> {
|
||||
}
|
||||
}
|
||||
|
||||
static void builder_reusable_after_release_message_and_move_assign_test(TestSelector selector) {
|
||||
static void builder_reusable_after_release_message_and_move_assign_test(
|
||||
TestSelector selector) {
|
||||
if (!selector.count(REUSABLE_AFTER_RELEASE_MESSAGE_AND_MOVE_ASSIGN)) {
|
||||
return;
|
||||
}
|
||||
@@ -125,7 +125,7 @@ struct BuilderReuseTests<flatbuffers::grpc::MessageBuilder, SrcBuilder> {
|
||||
auto root_offset1 = populate1(dst);
|
||||
dst.Finish(root_offset1);
|
||||
buffers.push_back(dst.ReleaseMessage<Monster>());
|
||||
TEST_ASSERT_FUNC(verify(buffers[i], m1_name, m1_color));
|
||||
TEST_ASSERT_FUNC(verify(buffers[i], m1_name(), m1_color()));
|
||||
|
||||
// bring dst back to life.
|
||||
SrcBuilder src;
|
||||
@@ -135,20 +135,18 @@ struct BuilderReuseTests<flatbuffers::grpc::MessageBuilder, SrcBuilder> {
|
||||
}
|
||||
}
|
||||
|
||||
static void builder_reusable_after_releaseraw_and_move_assign_test(TestSelector selector) {
|
||||
if (!selector.count(REUSABLE_AFTER_RELEASE_RAW_AND_MOVE_ASSIGN)) {
|
||||
return;
|
||||
}
|
||||
static void builder_reusable_after_releaseraw_and_move_assign_test(
|
||||
TestSelector selector) {
|
||||
if (!selector.count(REUSABLE_AFTER_RELEASE_RAW_AND_MOVE_ASSIGN)) { return; }
|
||||
|
||||
flatbuffers::grpc::MessageBuilder dst;
|
||||
for (int i = 0; i < 5; ++i) {
|
||||
auto root_offset1 = populate1(dst);
|
||||
dst.Finish(root_offset1);
|
||||
size_t size, offset;
|
||||
grpc_slice slice = grpc_empty_slice();
|
||||
::grpc::Slice slice;
|
||||
const uint8_t *buf = dst.ReleaseRaw(size, offset, slice);
|
||||
TEST_ASSERT_FUNC(verify(buf, offset, m1_name, m1_color));
|
||||
grpc_slice_unref(slice);
|
||||
TEST_ASSERT_FUNC(verify(buf, offset, m1_name(), m1_color()));
|
||||
|
||||
SrcBuilder src;
|
||||
dst = std::move(src);
|
||||
@@ -175,11 +173,11 @@ void slice_allocator_tests() {
|
||||
uint8_t *buf = sa1.allocate(size);
|
||||
TEST_ASSERT_FUNC(buf != 0);
|
||||
buf[0] = 100;
|
||||
buf[size-1] = 200;
|
||||
buf[size - 1] = 200;
|
||||
flatbuffers::grpc::SliceAllocator sa2(std::move(sa1));
|
||||
// buf should not be deleted after move-construct
|
||||
TEST_EQ_FUNC(buf[0], 100);
|
||||
TEST_EQ_FUNC(buf[size-1], 200);
|
||||
TEST_EQ_FUNC(buf[size - 1], 200);
|
||||
// buf is freed here
|
||||
}
|
||||
|
||||
@@ -194,13 +192,16 @@ void slice_allocator_tests() {
|
||||
}
|
||||
}
|
||||
|
||||
/// This function does not populate exactly the first half of the table. But it could.
|
||||
void populate_first_half(MyGame::Example::MonsterBuilder &wrapper, flatbuffers::Offset<flatbuffers::String> name_offset) {
|
||||
/// This function does not populate exactly the first half of the table. But it
|
||||
/// could.
|
||||
void populate_first_half(MyGame::Example::MonsterBuilder &wrapper,
|
||||
flatbuffers::Offset<flatbuffers::String> name_offset) {
|
||||
wrapper.add_name(name_offset);
|
||||
wrapper.add_color(m1_color);
|
||||
wrapper.add_color(m1_color());
|
||||
}
|
||||
|
||||
/// This function does not populate exactly the second half of the table. But it could.
|
||||
/// This function does not populate exactly the second half of the table. But it
|
||||
/// could.
|
||||
void populate_second_half(MyGame::Example::MonsterBuilder &wrapper) {
|
||||
wrapper.add_hp(77);
|
||||
wrapper.add_mana(88);
|
||||
@@ -208,114 +209,138 @@ void populate_second_half(MyGame::Example::MonsterBuilder &wrapper) {
|
||||
wrapper.add_pos(&vec3);
|
||||
}
|
||||
|
||||
/// This function is a hack to update the FlatBufferBuilder reference (fbb_) in the MonsterBuilder object.
|
||||
/// This function will break if fbb_ is not the first member in MonsterBuilder. In that case, some offset must be added.
|
||||
/// This function is used exclusively for testing correctness of move operations between FlatBufferBuilders.
|
||||
/// If MonsterBuilder had a fbb_ pointer, this hack would be unnecessary. That involves a code-generator change though.
|
||||
void test_only_hack_update_fbb_reference(MyGame::Example::MonsterBuilder &monsterBuilder,
|
||||
flatbuffers::grpc::MessageBuilder &mb) {
|
||||
/// This function is a hack to update the FlatBufferBuilder reference (fbb_) in
|
||||
/// the MonsterBuilder object. This function will break if fbb_ is not the first
|
||||
/// member in MonsterBuilder. In that case, some offset must be added. This
|
||||
/// function is used exclusively for testing correctness of move operations
|
||||
/// between FlatBufferBuilders. If MonsterBuilder had a fbb_ pointer, this hack
|
||||
/// would be unnecessary. That involves a code-generator change though.
|
||||
void test_only_hack_update_fbb_reference(
|
||||
MyGame::Example::MonsterBuilder &monsterBuilder,
|
||||
flatbuffers::grpc::MessageBuilder &mb) {
|
||||
*reinterpret_cast<flatbuffers::FlatBufferBuilder **>(&monsterBuilder) = &mb;
|
||||
}
|
||||
|
||||
/// This test validates correctness of move conversion of FlatBufferBuilder to a MessageBuilder DURING
|
||||
/// a table construction. Half of the table is constructed using FlatBufferBuilder and the other half
|
||||
/// of the table is constructed using a MessageBuilder.
|
||||
/// This test validates correctness of move conversion of FlatBufferBuilder to a
|
||||
/// MessageBuilder DURING a table construction. Half of the table is constructed
|
||||
/// using FlatBufferBuilder and the other half of the table is constructed using
|
||||
/// a MessageBuilder.
|
||||
void builder_move_ctor_conversion_before_finish_half_n_half_table_test() {
|
||||
for (size_t initial_size = 4 ; initial_size <= 2048; initial_size *= 2) {
|
||||
for (size_t initial_size = 4; initial_size <= 2048; initial_size *= 2) {
|
||||
flatbuffers::FlatBufferBuilder fbb(initial_size);
|
||||
auto name_offset = fbb.CreateString(m1_name);
|
||||
MyGame::Example::MonsterBuilder monsterBuilder(fbb); // starts a table in FlatBufferBuilder
|
||||
auto name_offset = fbb.CreateString(m1_name());
|
||||
MyGame::Example::MonsterBuilder monsterBuilder(
|
||||
fbb); // starts a table in FlatBufferBuilder
|
||||
populate_first_half(monsterBuilder, name_offset);
|
||||
flatbuffers::grpc::MessageBuilder mb(std::move(fbb));
|
||||
test_only_hack_update_fbb_reference(monsterBuilder, mb); // hack
|
||||
test_only_hack_update_fbb_reference(monsterBuilder, mb); // hack
|
||||
populate_second_half(monsterBuilder);
|
||||
mb.Finish(monsterBuilder.Finish()); // ends the table in MessageBuilder
|
||||
TEST_ASSERT_FUNC(release_n_verify(mb, m1_name, m1_color));
|
||||
mb.Finish(monsterBuilder.Finish()); // ends the table in MessageBuilder
|
||||
TEST_ASSERT_FUNC(release_n_verify(mb, m1_name(), m1_color()));
|
||||
TEST_EQ_FUNC(fbb.GetSize(), 0);
|
||||
}
|
||||
}
|
||||
|
||||
/// This test populates a COMPLETE inner table before move conversion and later populates more members in the outer table.
|
||||
/// This test populates a COMPLETE inner table before move conversion and later
|
||||
/// populates more members in the outer table.
|
||||
void builder_move_ctor_conversion_before_finish_test() {
|
||||
for (size_t initial_size = 4 ; initial_size <= 2048; initial_size *= 2) {
|
||||
for (size_t initial_size = 1; initial_size <= 2048; initial_size += 1) {
|
||||
flatbuffers::FlatBufferBuilder fbb(initial_size);
|
||||
auto stat_offset = CreateStat(fbb, fbb.CreateString("SomeId"), 0, 0);
|
||||
flatbuffers::grpc::MessageBuilder mb(std::move(fbb));
|
||||
auto monster_offset = CreateMonster(mb, 0, 150, 100, mb.CreateString(m1_name), 0, m1_color, Any_NONE, 0, 0, 0, 0, 0, 0, stat_offset);
|
||||
auto monster_offset =
|
||||
CreateMonster(mb, 0, 150, 100, mb.CreateString(m1_name()), 0,
|
||||
m1_color(), Any_NONE, 0, 0, 0, 0, 0, 0, stat_offset);
|
||||
mb.Finish(monster_offset);
|
||||
TEST_ASSERT_FUNC(release_n_verify(mb, m1_name, m1_color));
|
||||
{
|
||||
auto mon = flatbuffers::GetRoot<Monster>(mb.GetBufferPointer());
|
||||
TEST_NOTNULL(mon);
|
||||
TEST_NOTNULL(mon->name());
|
||||
TEST_EQ_STR(mon->name()->c_str(), m1_name().c_str());
|
||||
TEST_EQ(mon->color(), m1_color());
|
||||
}
|
||||
TEST_EQ(1, MyGame::Example::Color_Red);
|
||||
TEST_EQ(1, m1_color());
|
||||
TEST_ASSERT_FUNC(release_n_verify(mb, m1_name(), m1_color()));
|
||||
TEST_EQ_FUNC(fbb.GetSize(), 0);
|
||||
}
|
||||
}
|
||||
|
||||
/// This test validates correctness of move conversion of FlatBufferBuilder to a MessageBuilder DURING
|
||||
/// a table construction. Half of the table is constructed using FlatBufferBuilder and the other half
|
||||
/// of the table is constructed using a MessageBuilder.
|
||||
/// This test validates correctness of move conversion of FlatBufferBuilder to a
|
||||
/// MessageBuilder DURING a table construction. Half of the table is constructed
|
||||
/// using FlatBufferBuilder and the other half of the table is constructed using
|
||||
/// a MessageBuilder.
|
||||
void builder_move_assign_conversion_before_finish_half_n_half_table_test() {
|
||||
flatbuffers::FlatBufferBuilder fbb;
|
||||
flatbuffers::grpc::MessageBuilder mb;
|
||||
|
||||
for (int i = 0;i < 5; ++i) {
|
||||
for (int i = 0; i < 5; ++i) {
|
||||
flatbuffers::FlatBufferBuilder fbb;
|
||||
auto name_offset = fbb.CreateString(m1_name);
|
||||
MyGame::Example::MonsterBuilder monsterBuilder(fbb); // starts a table in FlatBufferBuilder
|
||||
auto name_offset = fbb.CreateString(m1_name());
|
||||
MyGame::Example::MonsterBuilder monsterBuilder(
|
||||
fbb); // starts a table in FlatBufferBuilder
|
||||
populate_first_half(monsterBuilder, name_offset);
|
||||
mb = std::move(fbb);
|
||||
test_only_hack_update_fbb_reference(monsterBuilder, mb); // hack
|
||||
test_only_hack_update_fbb_reference(monsterBuilder, mb); // hack
|
||||
populate_second_half(monsterBuilder);
|
||||
mb.Finish(monsterBuilder.Finish()); // ends the table in MessageBuilder
|
||||
TEST_ASSERT_FUNC(release_n_verify(mb, m1_name, m1_color));
|
||||
mb.Finish(monsterBuilder.Finish()); // ends the table in MessageBuilder
|
||||
TEST_ASSERT_FUNC(release_n_verify(mb, m1_name(), m1_color()));
|
||||
TEST_EQ_FUNC(fbb.GetSize(), 0);
|
||||
}
|
||||
}
|
||||
|
||||
/// This test populates a COMPLETE inner table before move conversion and later populates more members in the outer table.
|
||||
/// This test populates a COMPLETE inner table before move conversion and later
|
||||
/// populates more members in the outer table.
|
||||
void builder_move_assign_conversion_before_finish_test() {
|
||||
flatbuffers::FlatBufferBuilder fbb;
|
||||
flatbuffers::grpc::MessageBuilder mb;
|
||||
|
||||
for (int i = 0;i < 5; ++i) {
|
||||
for (int i = 0; i < 5; ++i) {
|
||||
auto stat_offset = CreateStat(fbb, fbb.CreateString("SomeId"), 0, 0);
|
||||
mb = std::move(fbb);
|
||||
auto monster_offset = CreateMonster(mb, 0, 150, 100, mb.CreateString(m1_name), 0, m1_color, Any_NONE, 0, 0, 0, 0, 0, 0, stat_offset);
|
||||
auto monster_offset =
|
||||
CreateMonster(mb, 0, 150, 100, mb.CreateString(m1_name()), 0,
|
||||
m1_color(), Any_NONE, 0, 0, 0, 0, 0, 0, stat_offset);
|
||||
mb.Finish(monster_offset);
|
||||
TEST_ASSERT_FUNC(release_n_verify(mb, m1_name, m1_color));
|
||||
TEST_ASSERT_FUNC(release_n_verify(mb, m1_name(), m1_color()));
|
||||
TEST_EQ_FUNC(fbb.GetSize(), 0);
|
||||
}
|
||||
}
|
||||
|
||||
/// This test populates data, finishes the buffer, and does move conversion after.
|
||||
/// This test populates data, finishes the buffer, and does move conversion
|
||||
/// after.
|
||||
void builder_move_ctor_conversion_after_finish_test() {
|
||||
flatbuffers::FlatBufferBuilder fbb;
|
||||
fbb.Finish(populate1(fbb));
|
||||
flatbuffers::grpc::MessageBuilder mb(std::move(fbb));
|
||||
TEST_ASSERT_FUNC(release_n_verify(mb, m1_name, m1_color));
|
||||
TEST_ASSERT_FUNC(release_n_verify(mb, m1_name(), m1_color()));
|
||||
TEST_EQ_FUNC(fbb.GetSize(), 0);
|
||||
}
|
||||
|
||||
/// This test populates data, finishes the buffer, and does move conversion after.
|
||||
/// This test populates data, finishes the buffer, and does move conversion
|
||||
/// after.
|
||||
void builder_move_assign_conversion_after_finish_test() {
|
||||
flatbuffers::FlatBufferBuilder fbb;
|
||||
flatbuffers::grpc::MessageBuilder mb;
|
||||
|
||||
for (int i = 0;i < 5; ++i) {
|
||||
for (int i = 0; i < 5; ++i) {
|
||||
fbb.Finish(populate1(fbb));
|
||||
mb = std::move(fbb);
|
||||
TEST_ASSERT_FUNC(release_n_verify(mb, m1_name, m1_color));
|
||||
TEST_ASSERT_FUNC(release_n_verify(mb, m1_name(), m1_color()));
|
||||
TEST_EQ_FUNC(fbb.GetSize(), 0);
|
||||
}
|
||||
}
|
||||
|
||||
void message_builder_tests() {
|
||||
using flatbuffers::grpc::MessageBuilder;
|
||||
using flatbuffers::FlatBufferBuilder;
|
||||
using flatbuffers::grpc::MessageBuilder;
|
||||
|
||||
slice_allocator_tests();
|
||||
|
||||
#ifndef __APPLE__
|
||||
builder_move_ctor_conversion_before_finish_half_n_half_table_test();
|
||||
builder_move_assign_conversion_before_finish_half_n_half_table_test();
|
||||
#endif // __APPLE__
|
||||
#endif // __APPLE__
|
||||
builder_move_ctor_conversion_before_finish_test();
|
||||
builder_move_assign_conversion_before_finish_test();
|
||||
|
||||
@@ -326,15 +351,18 @@ void message_builder_tests() {
|
||||
BuilderTests<MessageBuilder, FlatBufferBuilder>::all_tests();
|
||||
|
||||
BuilderReuseTestSelector tests[6] = {
|
||||
//REUSABLE_AFTER_RELEASE, // Assertion failed: (GRPC_SLICE_IS_EMPTY(slice_))
|
||||
//REUSABLE_AFTER_RELEASE_AND_MOVE_ASSIGN, // Assertion failed: (p == GRPC_SLICE_START_PTR(slice_)
|
||||
// REUSABLE_AFTER_RELEASE, // Assertion failed:
|
||||
// (GRPC_SLICE_IS_EMPTY(slice_))
|
||||
// REUSABLE_AFTER_RELEASE_AND_MOVE_ASSIGN, // Assertion failed: (p ==
|
||||
// GRPC_SLICE_START_PTR(slice_)
|
||||
|
||||
REUSABLE_AFTER_RELEASE_RAW,
|
||||
REUSABLE_AFTER_RELEASE_MESSAGE,
|
||||
REUSABLE_AFTER_RELEASE_RAW, REUSABLE_AFTER_RELEASE_MESSAGE,
|
||||
REUSABLE_AFTER_RELEASE_MESSAGE_AND_MOVE_ASSIGN,
|
||||
REUSABLE_AFTER_RELEASE_RAW_AND_MOVE_ASSIGN
|
||||
};
|
||||
|
||||
BuilderReuseTests<MessageBuilder, MessageBuilder>::run_tests(TestSelector(tests, tests+6));
|
||||
BuilderReuseTests<MessageBuilder, FlatBufferBuilder>::run_tests(TestSelector(tests, tests+6));
|
||||
BuilderReuseTests<MessageBuilder, MessageBuilder>::run_tests(
|
||||
TestSelector(tests, tests + 6));
|
||||
BuilderReuseTests<MessageBuilder, FlatBufferBuilder>::run_tests(
|
||||
TestSelector(tests, tests + 6));
|
||||
}
|
||||
|
||||
+3
-3
@@ -4,13 +4,13 @@
|
||||
<parent>
|
||||
<groupId>com.google.flatbuffers</groupId>
|
||||
<artifactId>flatbuffers-parent</artifactId>
|
||||
<version>1.11.1</version>
|
||||
<version>2.0.3</version>
|
||||
</parent>
|
||||
<artifactId>grpc-test</artifactId>
|
||||
<description>Example/Test project demonstrating usage of flatbuffers with GRPC-Java instead of protobufs
|
||||
</description>
|
||||
<properties>
|
||||
<gRPC.version>1.11.1</gRPC.version>
|
||||
<gRPC.version>2.0.3</gRPC.version>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
@@ -40,7 +40,7 @@
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<version>4.13.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
Reference in New Issue
Block a user