protoc-gen-go-adapter

command
v1.0.14 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 1, 2024 License: MIT Imports: 7 Imported by: 0

README

protoc-gen-go-adapter

This is a plugin for the Google Protocol Buffers compiler protoc that generates adapter class for gRPC server to adapt into a gRPC client interface.

This will be useful when a local sigleton program have many service implementing the protobuf service, and prefer to call each other directly in memory without marshalling and unmarshalling the message.

Options

gen_gateway_client_adapter

If true, it will generate the adapter for GatewayClient generated by protoc-gen-grpc-gateway-client . The adapter will adapt the GatewayClient into gRPC Client interface generated by protoc-gen-go-grpc .

not_gen_grpc_adapter

If true, it will not generate the adapter for gRPC Server interface into gRPC Client interface.

Usage

protoc \
    --go_out=. --go_opt=paths=source_relative \
    --go-grpc_out=. --go-grpc_opt=paths=source_relative \
    --grpc-gateway_out=. --grpc-gateway_opt=paths=source_relative \
    --grpc-gateway-client_out=. --grpc-gateway-client_opt=paths=source_relative \
    --go-adapter_out=. --go-adapter_opt=paths=source_relative,gen_gateway_client_adapter=true,logtostderr=true \
    e2e.proto

Gen Codes

It will generate codes like bellow, if gen_gateway_client_adapter is set to true:

// GreeterAdapter  is the adapter for  Greeter  server into  Greeter  client interface
type GreeterAdapter struct {
	in GreeterServer
}

var _ GreeterClient = (*GreeterAdapter)(nil)

func NewGreeterAdapter(in GreeterServer) GreeterClient {
	return &GreeterAdapter{in: in}
}

func (a *GreeterAdapter) SayHello(ctx context.Context, req *HelloRequest, opts ...grpc.CallOption) (*HelloReply, error) {
	return a.in.SayHello(ctx, req)
}
func (a *GreeterAdapter) SayHelloPost(ctx context.Context, req *HelloRequest, opts ...grpc.CallOption) (*HelloReply, error) {
	return a.in.SayHelloPost(ctx, req)
}
func (a *GreeterAdapter) SayHttp(ctx context.Context, req *HelloRequest, opts ...grpc.CallOption) (*annotations.Http, error) {
	return a.in.SayHttp(ctx, req)
}

// GreeterGatewayClientAdapter  is the adapter for  Greeter  server into  Greeter  client interface
type GreeterGatewayClientAdapter struct {
	in GreeterGatewayClient
}

var _ GreeterClient = (*GreeterGatewayClientAdapter)(nil)

func NewGreeterGatewayClientAdapter(in GreeterGatewayClient) GreeterClient {
	return &GreeterGatewayClientAdapter{in: in}
}

func (a *GreeterGatewayClientAdapter) SayHello(ctx context.Context, req *HelloRequest, opts ...grpc.CallOption) (*HelloReply, error) {
	return a.in.SayHello(ctx, req)
}
func (a *GreeterGatewayClientAdapter) SayHelloPost(ctx context.Context, req *HelloRequest, opts ...grpc.CallOption) (*HelloReply, error) {
	return a.in.SayHelloPost(ctx, req)
}
func (a *GreeterGatewayClientAdapter) SayHttp(ctx context.Context, req *HelloRequest, opts ...grpc.CallOption) (*annotations.Http, error) {
	return a.in.SayHttp(ctx, req)
}

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL