protoc-gen-xctrl

command module
v0.0.0-...-5987cce Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2023 License: MIT Imports: 5 Imported by: 0

README

protoc-gen-stack

This is protobuf code generation for go-stack. We use protoc-gen-stack to reduce boilerplate code.

Install

go get git.xswitch.cn/xswitch/xctrl/stack/cmd/protoc-gen-stack/v3

Also required:

Usage

Define your service as greeter.proto

syntax = "proto3";

service Greeter {
	rpc Hello(Request) returns (Response) {}
}

message Request {
	string name = 1;
}

message Response {
	string msg = 1;
}

Generate the code

protoc --proto_path=$GOPATH/src:. --micro_out=. --go_out=. greeter.proto

Your output result should be:

./
    greeter.proto	# original protobuf file
    greeter.pb.go	# auto-generated by protoc-gen-go
    greeter.stack.go	# auto-generated by protoc-gen-stack

The stack generated code includes clients and handlers which reduce boiler plate code

Server

Register the handler with your stack server

type Greeter struct{}

func (g *Greeter) Hello(ctx context.Context, req *proto.Request, rsp *proto.Response) error {
	rsp.Msg = "Hello " + req.Name
	return nil
}

proto.RegisterGreeterHandler(service.Server(), &Greeter{})
Client

Create a service client with your stack client

client := proto.NewGreeterService("greeter", service.Client())
Errors

If you see an error about protoc-gen-stack not being found or executable, it's likely your environment may not be configured correctly. If you've already installed protoc, protoc-gen-go, and protoc-gen-stack ensure you've included $GOPATH/bin in your PATH.

Alternative specify the Go plugin paths as arguments to the protoc command

protoc --plugin=protoc-gen-go=$GOPATH/bin/protoc-gen-go --plugin=protoc-gen-stack=$GOPATH/bin/protoc-gen-stack --proto_path=$GOPATH/src:. --micro_out=. --go_out=. greeter.proto
Endpoint

Add a stack API endpoint which routes directly to an RPC method

Usage:

  1. Clone github.com/googleapis/googleapis to use this feature as it requires http annotations.
  2. The protoc command must include -I$GOPATH/src/github.com/googleapis/googleapis for the annotations import.
syntax = "proto3";

import "google/api/annotations.proto";

service Greeter {
	rpc Hello(Request) returns (Response) {
		option (google.api.http) = { post: "/hello"; body: "*"; };
	}
}

message Request {
	string name = 1;
}

message Response {
	string msg = 1;
}

The proto generates a RegisterGreeterHandler function with a api.Endpoint.

func RegisterGreeterHandler(s server.Server, hdlr GreeterHandler, opts ...server.HandlerOption) error {
	type greeter interface {
		Hello(ctx context.Context, in *Request, out *Response) error
	}
	type Greeter struct {
		greeter
	}
	h := &greeterHandler{hdlr}
	opts = append(opts, api.WithEndpoint(&api.Endpoint{
		Name:    "Greeter.Hello",
		Path:    []string{"/hello"},
		Method:  []string{"POST"},
		Handler: "rpc",
	}))
	return s.Handle(s.NewHandler(&Greeter{h}, opts...))
}

LICENSE

protoc-gen-stack is a liberal reuse of protoc-gen-go hence we maintain the original license

Documentation

Overview

protoc-gen-stack is a plugin for the Google protocol buffer compiler to generate Go code. Run it by building this program and putting it in your path with the name

protoc-gen-stack

That word 'stack' at the end becomes part of the option string set for the protocol compiler, so once the protocol compiler (protoc) is installed you can run

protoc --stack_out=output_directory --go_out=output_directory input_directory/file.proto

to generate go-stack code for the protocol defined by file.proto. With that input, the output will be written to

output_directory/file.stack.go

The generated code is documented in the package comment for the library.

See the README and documentation for protocol buffers to learn more:

https://developers.google.com/protocol-buffers/

Directories

Path Synopsis
The code generator for the plugin for the Google protocol buffer compiler.
The code generator for the plugin for the Google protocol buffer compiler.
plugin

Jump to

Keyboard shortcuts

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